Java Compare one BufferedImage to Another

前端 未结 3 1809
-上瘾入骨i
-上瘾入骨i 2020-12-21 07:57

I need to compare two buffered images to see if they are the exact same. Simply saying if that equals that doesn\'t work. My current method is

                       


        
3条回答
  •  醉酒成梦
    2020-12-21 08:36

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

提交回复
热议问题