DCTImageHashAlgorithm implements ImageHashAlgorithm { @Override public OpenBitSet getHash(BufferedImage image) { final BufferedImage smallSize; if (image.getWidth() == INTERNAL_SIZE && image.getHeight() == INTERNAL_SIZE && image.getType() == INTERNAL_IMAGE_TYPE) { smallSize = image; } else { smallSize = createResizedCopy(image); } DenseMatrix64F imageMatrix = getImageMatrix(smallSize); return doHash(imageMatrix); }  @Override BufferedImage createResizedCopy(BufferedImage originalImage); @Override OpenBitSet getHash(BufferedImage image); @Override OpenBitSet getHash(double[][] imageMatrix); static final int INTERNAL_SIZE; static final int INTERNAL_IMAGE_TYPE; }
@Test public void testGetHash() throws Exception { URL url = getClass().getResource("/sample/"); File dir = new File(url.toURI()); ImageHashAlgorithm algorithm = new DCTImageHashAlgorithm(); List<OpenBitSet> results = new ArrayList<OpenBitSet>(); for (File file : dir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.toLowerCase().endsWith("jpg"); } })) { BufferedImage bufferedImage = ImageIO.read(file); OpenBitSet hash = algorithm.getHash(bufferedImage); System.out.printf("%s\t%x\n", file.toString(), hash.getBits()[0]); results.add(hash); } double min = Double.MAX_VALUE; for (int i = 0; i < results.size(); i++) { for (int j = i + 1; j < results.size(); j++) { OpenBitSet hash1 = results.get(i); OpenBitSet hash2 = results.get(j); hash1.xor(hash2); double distance = hash1.cardinality(); if (distance < min) min = distance; assertThat(distance, greaterThan(DEFAULT_THRESHOLD)); } } System.out.println("Minimum distance:" + Double.valueOf(min)); }
@Test public void testEqual() throws IOException { BufferedImage png = ImageIO.read(getClass().getResource("/sample/IMG_1548.png")); String[] resources = new String[]{ "/sample/IMG_1548.jpg", "/sample/IMG_1548_crayon.jpe", "/sample/IMG_1548_scaled.jpe", "/sample/IMG_1548_cropped.jpe", }; ImageHashAlgorithm algorithm = new DCTImageHashAlgorithm(); OpenBitSet pnghash = algorithm.getHash(png); double max = 0; for (String resource : resources) { BufferedImage jpg = ImageIO.read(getClass().getResource(resource)); OpenBitSet jpghash = algorithm.getHash(jpg); jpghash.xor(pnghash); double distance = jpghash.cardinality(); System.out.printf("Distance for %s = %f\n", resource, distance); if (distance > max) max = distance; assertThat(distance, lessThanOrEqualTo(DEFAULT_THRESHOLD)); } System.out.println("Maxiumum distance:" + Double.valueOf(max)); }
@Test public void testReferenceHash() { ImageHashAlgorithm algorithm = new DCTImageHashAlgorithm(); OpenBitSet actual = algorithm.getHash(Data.reference_imgdata); OpenBitSet exp = new OpenBitSet(new long[]{Data.reference_hash}, 1); System.out.println(Long.toBinaryString(Data.reference_hash)); System.out.println(Long.toBinaryString(actual.getBits()[0])); assertEquals(exp, actual); }