Java fast image comparing -
hi formatted phone , uploaded photos pc, when wanted add photos phone saw have multiple duplicates of images. wanted merge photos 1 folder upload phone wrote java code.
public class main { public static int imgctr = 1; public static file dest = new file("d:\\finalfinal"); public static void main(string[] args) throws exception { getcontent("d:\\restorefinal"); getcontent("d:\\restore1"); getcontent("d:\\restore2"); } public static string getextension(string filename) { string extension = ""; int = filename.lastindexof('.'); if (i > 0) { extension = filename.substring(i + 1); } return extension; } public static boolean isimage(string extension) { if (extension.equalsignorecase("jpg") || extension.equalsignorecase("jpeg") || extension.equalsignorecase("png")) return true; return false; } public static boolean compareimages(file a, file b) throws exception { fileinputstream fisa = new fileinputstream(a); fileinputstream fisb = new fileinputstream(b); byte contenta[] = new byte[(int) a.length()]; byte contentb[] = new byte[(int) b.length()]; fisa.read(contenta); fisb.read(contentb); string stra = new string(contenta); string strb = new string(contentb); fisa.close(); fisb.close(); return stra.equals(strb); } public static void getcontent(string path) throws exception { file source = new file(path); arraylist<file> content = new arraylist<file>(arrays.aslist(source.listfiles())); while (!content.isempty()) { file f = content.get(0); if (isimage(getextension(f.getname()))) { if (dest.listfiles().length == 0) { path p = paths.get(dest + "\\i" + imgctr + "." + getextension(f.getname())); imgctr++; files.move(f.topath(), p); system.out.println(imgctr); } else { file[] alreadythere = dest.listfiles(); boolean match = false; (file cmp : alreadythere) { if (compareimages(f, cmp)) { match = true; break; } } if (!match) { path p = paths.get(dest + "\\i" + imgctr + "." + getextension(f.getname())); imgctr++; files.move(f.topath(), p); system.out.println(imgctr); } } } content.remove(0); } }
}
i wrote image compare string compares because pixel comparing took long (had around 2k photos). problem somehow copies photo multiple times without difference can see. , searched source folders copies photos arbitrarily, photos didn't have duplicates had duplicates in destination folder. doubt compare method, couldn't find mistake.
so can me find fault or suggest fast , more reliable way compare images?
comparing pixels fine if images haven't been resaved or haven't passed through lossy file format such jpeg. if haven't start off checksum comparison , if checksums don't more extensive pixel comparison, though lossy algorithms require different approach.
Comments
Post a Comment