Created
January 20, 2013 06:41
-
-
Save dag05ru/4577000 to your computer and use it in GitHub Desktop.
Finds all the similar images in the directory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Graphics2D; | |
import java.awt.image.BufferedImage; | |
import javax.imageio.ImageIO; | |
import java.io.*; | |
import java.util.*; | |
public class FindClone { | |
static final int SIZE = 20; | |
BufferedImage changeSize(String file_name) throws IOException{ | |
BufferedImage originalImage = ImageIO.read(new File(file_name)); | |
BufferedImage resizedImage = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_BYTE_GRAY ); | |
Graphics2D g = resizedImage.createGraphics(); | |
g.drawImage(originalImage, 0, 0, SIZE, SIZE, null); | |
g.dispose(); | |
return resizedImage; | |
} | |
int getImgSum(BufferedImage img){ | |
int sum = 0; | |
for(int i = 0; i < SIZE; i++){ | |
for(int j = 0; j < SIZE; j++){ | |
int rgb = img.getRGB(j, i); | |
sum += (rgb & 0xff) + ((rgb >> 8) & 0xff) + ((rgb >> 16) & 0xff); | |
} | |
} | |
return sum; | |
} | |
TreeMap<Integer,String> genSumArray(String path_name) throws IOException{ | |
TreeMap<Integer,String> sum_list = new TreeMap<Integer, String>(); | |
File path = new File(path_name); | |
if(path.isDirectory()){ | |
String path_list[] = path.list(); | |
for(String s: path_list){ | |
String name = path_name+"/"+s; | |
int sum = getImgSum(changeSize(path_name+"/"+s)); | |
sum_list.put(sum, name); | |
} | |
} | |
return sum_list; | |
} | |
public static void main(String[] args) { | |
try{ | |
System.out.println("<html><body>"); | |
FindClone fc = new FindClone(); | |
TreeMap<Integer,String> tm = fc.genSumArray(args[0]); | |
int tmp = 0; | |
for(Integer i:tm.keySet()){ | |
if(i - tmp > 500) System.out.println("<hr>"+(i - tmp)); | |
tmp = i; | |
System.out.println("<img src=\""+tm.get(i)+"\" width=200 />"); | |
} | |
System.out.println("</body></html>"); | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment