@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)); }
@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); }
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); } }
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); }  }
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); }
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; }