Skip to content

Instantly share code, notes, and snippets.

@sgallese
Created February 27, 2010 20:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgallese/316917 to your computer and use it in GitHub Desktop.
Save sgallese/316917 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)
{
MHImageHash mh = mhImageHash(files[i].toString());
if(mh != null)
hashes[i] = mh;
}
}
return hashes;
}
public static void main(String args[])
{
int i = 0;
if(args[i].equals("-mvp"))
{
MVPTree mvp = new MVPTree("mvp");
System.out.println("Instantiated new MVP tree!");
MHImageHash[] hashes = getMHImageHashes(args[1]);
System.out.println("Created array of MHImageHash!");
boolean result = mvp.create(hashes);
System.out.println("The result of the .create method was "+result);
if(result)
{
System.out.println("Successfully created MVP tree");
Hash[] results = mvp.query(hashes[0], 100, 20);
if(results != null && results.length > 0)
{
System.out.println("Query found " + results.length + " results");
for(int j = 0; j < results.length; ++j)
System.out.println("File: " + results[j].filename);
}
MHImageHash[] newHashes = getMHImageHashes(args[2]);
boolean added = mvp.add(newHashes);
if(added)
{
System.out.println("Hashes added successfully.");
Hash[] foundHashes = mvp.query(newHashes[0], 100, 20);
if(foundHashes != null && foundHashes.length > 0)
{
System.out.println("Found newly added hash.");
}
}
}
}
else if(args[i].equals("-a"))
{
AudioHash audioHash1 = audioHash(args[1]);
AudioHash audioHash2 = audioHash(args[2]);
System.out.println("cs = " + audioDistance(audioHash1,audioHash2));
}
else if(args[i].equals("-dct"))
{
DCTImageHash imHash = dctImageHash(args[1]);
DCTImageHash imHash2 = dctImageHash(args[2]);
System.out.println("File 1: " + imHash.filename);
System.out.println("File 2: " + imHash2.filename);
System.out.println(imageDistance(imHash,imHash2));
}
else if(args[i].equals("-mh"))
{
MHImageHash imHash = mhImageHash(args[1]);
MHImageHash imHash2 = mhImageHash(args[2]);
System.out.println("File 1: " + imHash.filename);
System.out.println("File 2: " + imHash2.filename);
System.out.println(imageDistance(imHash,imHash2));
}
else if(args[i].equals("-v"))
{
VideoHash vHash = videoHash(args[1]);
VideoHash vHash2 = videoHash(args[2]);
System.out.println(videoDistance(vHash,vHash2, 21));
}
else if(args[i].equals("-t"))
{
TextHash txtHash = textHash(args[1]);
TextHash txtHash2 = textHash(args[2]);
System.out.println(textDistance(txtHash,txtHash2));
}
pHash.cleanup();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment