Skip to content

Instantly share code, notes, and snippets.

@sgallese
Created February 26, 2010 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgallese/316013 to your computer and use it in GitHub Desktop.
Save sgallese/316013 to your computer and use it in GitHub Desktop.
import java.io.*;
public class pHash {
native static VideoHash videoHash(String file);
native static AudioHash audioHash(String file);
native static DCTImageHash dctImageHash(String file);
native static MHImageHash mhImageHash(String file);
native static TextHash textHash(String file);
native static double imageDistance(ImageHash hash1, ImageHash hash2);
native static double audioDistance(AudioHash hash1, AudioHash hash2);
native static double videoDistance(VideoHash hash1, VideoHash hash2,
int threshold);
native static int textDistance(TextHash txtHash1, TextHash txtHash2);
private native static void pHashInit();
private native static void cleanup();
static {
System.loadLibrary("pHash-jni");
pHashInit();
}
public static MHImageHash[] getMHImageHashes(String d) {
File dir = new File(d);
MHImageHash[] hashes = null;
if (dir.isDirectory()) {
File[] files = dir.listFiles();
hashes = new MHImageHash[files.length];
for (int i = 0; i < files.length; ++i) {
System.out.println("Hasing " + i + " of " + files.length + " "
+ files[i].toString());
MHImageHash mh = mhImageHash(files[i].toString());
if (mh != null)
hashes[i] = mh;
}
}
return hashes;
}
public static File[] getDirectoryFiles(String d) {
File dir = new File(d);
File[] files = dir.listFiles();
return files;
}
public static void main(String args[]) {
FileWriter myOutput;
try {
myOutput = new FileWriter("order.txt", true);
BufferedWriter writer = new BufferedWriter(myOutput);
MHImageHash[] hashes = getMHImageHashes("/home/kdge/donaldduckflickr/");
System.out.println("Finished making Hash array");
File[] files = getDirectoryFiles("/home/kdge/donaldduck/");
for (int j = 0; j < files.length; j++) {
String searchImage = files[j].toString();
MHImageHash mh = mhImageHash(searchImage);
double bestDistance = 999999999;
String bestImage = "";
// System.out
// .println("Looking to match " + mh.filename.toString());
for (int i = 0; i < hashes.length; i++) {
//System.out.println("Comapring search "+i+" of "+hashes.length);
double imgDistance = imageDistance(mh, hashes[i]);
if (imgDistance < bestDistance) {
bestDistance = imgDistance;
bestImage = hashes[i].filename.toString();
// System.out.println(bestImage+" is best match at "+
// bestDistance);
}
}
System.out.println(bestImage + " MATCH " + searchImage
+ " AT " + bestDistance);
writer.write(bestImage + "\n");
}
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pHash.cleanup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment