Java Compare one BufferedImage to Another

霸气de小男生 提交于 2019-11-29 15:04:05
devrobf

The obvious solution would be to compare, pixel by pixel, that they are the same.

boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
        for (int x = 0; x < img1.getWidth(); x++) {
            for (int y = 0; y < img1.getHeight(); y++) {
                if (img1.getRGB(x, y) != img2.getRGB(x, y))
                    return false;
            }
        }
    } else {
        return false;
    }
    return true;
}

Yeah, assuming they are both in the same format read them as byte strings and compare the bit strings. If one is a jpg and the other a png this won't work. But I'm assuming equality implies they are the same.

here's an example on how to do the file reading;

http://www.java-examples.com/read-file-byte-array-using-fileinputstream

What about hash codes?

img1.getData().hashCode().equals(img2.getData().hashCode())
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!