Java将一个BufferedImage与另一个进行比较 [英] Java Compare one BufferedImage to Another

查看:94
本文介绍了Java将一个BufferedImage与另一个进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要比较两个缓冲的图像,看看它们是否完全相同。简单地说,如果等于那不起作用。我当前的方法是

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

                 { 
                 Raster var1 = Img1.getData();    
                 Raster var2 = Img2.getData();

                 int Data1 = (var1.getDataBuffer()).getSize();
                 int Data2 = (var2.getDataBuffer()).getSize();

                 if (Data1 == Data2)
                         {
                         return true;
                         }
                 else 
                           {
                           return false;
                           }
                 }

但这不起作用。还有什么更可靠的方法?

But that doesn't really work. What other more reliable way is there?

推荐答案

显而易见的解决方案是逐像素地比较它们是否相同。

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

这篇关于Java将一个BufferedImage与另一个进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆