Advertisement
Cinster

Untitled

Nov 14th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. import java.awt.List;
  2. import java.awt.image.BufferedImage;
  3. import java.awt.image.DataBuffer;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.ArrayList;
  10. import java.util.Scanner;
  11. import java.util.stream.Stream;
  12. import javax.imageio.ImageIO;
  13.  
  14. public class ImageComparisson {
  15.  
  16.  
  17. public static int number;
  18. public static String[] S = new String[120001];
  19. public static ArrayList<String> list = new ArrayList<String>();
  20.  
  21. public static void print ()
  22. {
  23. for (int i = 0 ; i < number ; i++ ) {
  24. System.out.println((i+1) +". "+S[i]);
  25. }
  26. System.out.println();
  27.  
  28.  
  29.  
  30. }
  31.  
  32.  
  33. public static double compareImage(File fileA, File fileB, int percent) {
  34.  
  35. double percentage = 0.0;
  36. try {
  37. // take buffer data from both image files //
  38. BufferedImage biA = ImageIO.read(fileA);
  39. DataBuffer dbA = biA.getData().getDataBuffer();
  40. double sizeA = dbA.getSize();
  41.  
  42. BufferedImage biB = ImageIO.read(fileB);
  43. DataBuffer dbB = biB.getData().getDataBuffer();
  44. double sizeB = dbB.getSize();
  45.  
  46. double count = 0;
  47.  
  48. // compare data-buffer objects //
  49. if (sizeA == sizeB) {
  50.  
  51. for (int i = 0; i < sizeA; i++) {
  52. //check if pixel values are close enough (acceptable range of 15)
  53. if (Math.abs( ( dbA.getElem(i) - dbB.getElem(i))) <= 15 ) {
  54. count = count + 1;
  55. }
  56.  
  57. }
  58.  
  59. //calculate the percentage based on the similar pixels
  60. percentage = (count * 100) / sizeA;
  61.  
  62. }
  63. else {
  64.  
  65. }
  66.  
  67. }
  68. catch (Exception e) {
  69. System.out.println("Failed to compare image files ... " ) ;
  70. System.out.println(fileB.getPath());
  71. e.printStackTrace();
  72. }
  73. if (percentage >= percent)
  74. {
  75.  
  76. System.out.println("Image file "+ fileB.getPath()+" is similar to reference file" );
  77. System.out.println("These images are "+ percentage + "% similar" );
  78. System.out.println();
  79. list.add(fileB.getPath());
  80. S[number] = fileB.getPath();
  81. number++;
  82. }
  83. return 0;
  84. }
  85.  
  86.  
  87.  
  88.  
  89. public static void main(String[] args) throws IOException {
  90. Scanner sc = new Scanner (System.in);
  91. Scanner sc2 = new Scanner (System.in);
  92. Scanner sc3 = new Scanner (System.in);
  93. System.out.println();
  94. File fileA = new File("Project_1_datasets\\2\\40694.jpg");
  95. System.out.println("The reference file is: "+ fileA.getPath());
  96. System.out.println();
  97. System.out.print("Enter percent of similarity to search for (0 to 100, recomended > 70 ) : ");
  98. int percent = sc.nextInt();
  99. while (percent < 0 || percent > 100)
  100. {
  101. System.out.println("Percentage entered not in range, choose a number between 0 to 100");
  102. percent = sc.nextInt();
  103.  
  104. }
  105. System.out.println();
  106. int percents = percent;
  107. System.out.print("Searching for files "+ percents+"% similar to the file "+fileA.getPath());
  108. System.out.print(" , start search? (Y/N) : ");
  109. String S = sc2.nextLine();
  110. while (!S.equals("Y") && !S.equals("y"))
  111. {
  112. System.out.println("Please enter Y");
  113. S = sc2.nextLine();
  114. }
  115. System.out.println();
  116. System.out.println("Starting the search ...");
  117. System.out.println();
  118. try (Stream<Path> filePathStream=Files.walk(Paths.get("Project_1_datasets\\"))) {
  119. filePathStream.forEach(filePath -> {
  120. if (Files.isRegularFile(filePath)) {
  121. File fileB = new File(filePath.toString());
  122. compareImage(fileA ,fileB, percents);
  123. }
  124. });
  125. System.out.println("Done searching");
  126. System.out.println("Number of matches : "+ number);
  127. System.out.println();
  128. System.out.print("Print out the list of the " +number+" matching files (Y/N) : ");
  129. String D = sc3.nextLine();
  130. while (!D.equals("Y") && !D.equals("N") && !D.equals("n") && !D.equals("y")) {
  131. System.out.println("Please enter Y or N: ");
  132. D = sc3.nextLine();
  133.  
  134. }
  135. System.out.println();
  136. if (D.equals("Y") || D.equals("y")){
  137. print();
  138.  
  139. }
  140.  
  141. if (D.equals("N") || D.equals("n")){
  142.  
  143.  
  144. }
  145. System.out.println("Closing the program ...");
  146. sc.close();
  147. sc2.close();
  148. sc3.close();
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement