Image comparison

14 / Aug / 2010 by Anshul Sharma 0 comments

In one of our projects, we need to compare images which comes from various sources and remove the duplicate one.

The below algo helps us to remove the duplicate images.

 boolean compareImage(String firstPhotoUrl, String secondPhotoUrl) {
        boolean result = true;
        BufferedImage bi1 = ImageIO.read(new URL(firstPhotoUrl))
        BufferedImage bi2 = ImageIO.read(new URL(secondPhotoUrl))
        int size1 = bi1.getData().getDataBuffer().size;
        int size2 = bi2.getData().getDataBuffer().size;
        if ((size1 == size2) && (bi1.width == bi2.width) && (bi1.height == bi2.height)) {
            Raster r1 = bi1.getData();
            Raster r2 = bi2.getData();
            DataBuffer db1 = r1.getDataBuffer();
            DataBuffer db2 = r2.getDataBuffer();
            int size = db1.getSize();
            for (int i = 0; i < size; i++) {
                int px1 = db1.getElem(i);
                int px2 = db2.getElem(i);
                if (px1 != px2) {
                    result = false;
                    break;
                }
            }
        } else {
            result = false;
        }
    }
    return result;
}

Hope this helped!

Cheers!
Anshul Sharma

Tag -

FOUND THIS USEFUL? SHARE IT

Leave a comment -