File: pHash.java

package info (click to toggle)
libphash 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,984 kB
  • ctags: 389
  • sloc: sh: 10,093; cpp: 5,063; java: 140; makefile: 98; ansic: 44
file content (120 lines) | stat: -rw-r--r-- 3,583 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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");
					MHImageHash[] hashes = getMHImageHashes(args[1]);
					boolean result = mvp.create(hashes);
					if(result)
					{
						System.out.println("Successfully created MVP tree");									
						Hash[] results = mvp.query(hashes[0], 100, 20, 30);
						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, 30);
							if(foundHashes != null && foundHashes.length > 0)
							{
								System.out.println("Found newly added hash.");
							}
						}
					} else
						System.out.println("Creating tree failed");
				
			}
			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();

	}


}