Java BufferedImage Operation generateImageHash(BufferedImage image)

Here you can find the source of generateImageHash(BufferedImage image)

Description

Generates blocks representing an image by dividing the image up in 16x16 pixel blocks and calculating a mean value of the color in each block.

License

Open Source License

Parameter

Parameter Description
image the image

Return

the block representation of the image

Declaration

public static String generateImageHash(BufferedImage image) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.image.BufferedImage;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**//from   www  . ja v  a 2s  .  com
     * Generates blocks representing an image by dividing the image up in 16x16
     * pixel blocks and calculating a mean value of the color in each block.
     * 
     * @param image
     *            the image
     * @return the block representation of the image
     */
    public static String generateImageHash(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();

        byte[] data = new byte[width * height * 3];

        int idx = 0;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int rgb = image.getRGB(x, y);
                rgb &= 0x00FCFCFC;

                // Skip the two last bits for fuzzy comparison
                data[idx++] = (byte) ((rgb >> 16));
                data[idx++] = (byte) ((rgb >> 8));
                data[idx++] = (byte) (rgb);
            }
        }

        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(data);
            String hash = byteToHex(md5.digest());
            return hash;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm provider not found", e);
        }
    }

    private static String byteToHex(byte[] bytes) {
        String hex = "";
        for (int i = 0; i < bytes.length; i++) {
            hex += Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1);
        }
        return hex;
    }
}

Related

  1. fuzzyCompare(final BufferedImage img1, final BufferedImage img2, final double colorTolerance, final double pixelTolerance, final int fuzzyBlockDimension)
  2. fuzzyEquals(BufferedImage a, BufferedImage b, int threshold)
  3. genBlackAndWhiteImage(BufferedImage image)
  4. generate(int w, int h, BufferedImage img)
  5. generateBufferedImageFromComponent(Component component)
  6. generateOutline(BufferedImage source, Color color, boolean alpha)
  7. gradientMask(BufferedImage img, float start, float stop)
  8. greyScale(BufferedImage image)
  9. grid(BufferedImage image, int between)