Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中比较两个图像_Java_Image - Fatal编程技术网

如何在Java中比较两个图像

如何在Java中比较两个图像,java,image,Java,Image,我喜欢比较两张图片。imagemagic工具使用cmd提示符为我完成此操作。它比较两个图像(例如按钮的位置不同),并以新的gif图像输出结果图像,突出显示差异。然而,我想要一些工具,生成这样的新图像,但只有在有差异的情况下。请建议我如何使用任何工具,或者即使可以使用selenium和java,也可以这样做。imagemagic生成新的结果图像,即使它们之间没有差异 编辑: 我已经完成了RnD,并得出结论,imagemagic+iam4java可以通过selenium来比较图像,但仍然无法找到如何

我喜欢比较两张图片。imagemagic工具使用cmd提示符为我完成此操作。它比较两个图像(例如按钮的位置不同),并以新的gif图像输出结果图像,突出显示差异。然而,我想要一些工具,生成这样的新图像,但只有在有差异的情况下。请建议我如何使用任何工具,或者即使可以使用selenium和java,也可以这样做。imagemagic生成新的结果图像,即使它们之间没有差异

编辑:
我已经完成了RnD,并得出结论,imagemagic+iam4java可以通过selenium来比较图像,但仍然无法找到如何设置生成输出图像的条件,只有在存在差异时才能使用java来比较两幅图像:

    BufferedImage imgA = ImageIO.read(new File("C:/img/picA.jpg")); 
    BufferedImage imgB = ImageIO.read(new File("C:/img/picB.jpg"));   

    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;
    }
BufferedImage imgA=ImageIO.read(新文件(“C:/img/picA.jpg”);
BufferedImage imgB=ImageIO.read(新文件(“C:/img/picB.jpg”);
布尔BuffereImage Sequal(BuffereImage img1、BuffereImage img2){
if(img1.getWidth()==img2.getWidth()&&img1.getHeight()==img2.getHeight()){
对于(int x=0;x
要生成差异图像,可以执行以下操作:

    private static void subtractImages(BufferedImage image1, BufferedImage image2) throws IOException {
    BufferedImage image3 = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
    int color;
    for(int x = 0; x < image1.getWidth(); x++)
        for(int y = 0; y < image1.getHeight(); y++) {
            color = Math.abs(image2.getRGB(x, y) - image1.getRGB(x, y));                
            image3.setRGB(x, y, color);
        }
    ImageIO.write(image3, "bmp",  new File("image.bmp"));
 }
私有静态图像(BuffereImage image1、BuffereImage image2)引发IOException{
BuffereImage image3=新的BuffereImage(image1.getWidth(),image1.getHeight(),image1.getType());
内色;
对于(int x=0;x


使用java比较两幅图像:

    BufferedImage imgA = ImageIO.read(new File("C:/img/picA.jpg")); 
    BufferedImage imgB = ImageIO.read(new File("C:/img/picB.jpg"));   

    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;
    }
BufferedImage imgA=ImageIO.read(新文件(“C:/img/picA.jpg”);
BufferedImage imgB=ImageIO.read(新文件(“C:/img/picB.jpg”);
布尔BuffereImage Sequal(BuffereImage img1、BuffereImage img2){
if(img1.getWidth()==img2.getWidth()&&img1.getHeight()==img2.getHeight()){
对于(int x=0;x
要生成差异图像,可以执行以下操作:

    private static void subtractImages(BufferedImage image1, BufferedImage image2) throws IOException {
    BufferedImage image3 = new BufferedImage(image1.getWidth(), image1.getHeight(), image1.getType());
    int color;
    for(int x = 0; x < image1.getWidth(); x++)
        for(int y = 0; y < image1.getHeight(); y++) {
            color = Math.abs(image2.getRGB(x, y) - image1.getRGB(x, y));                
            image3.setRGB(x, y, color);
        }
    ImageIO.write(image3, "bmp",  new File("image.bmp"));
 }
私有静态图像(BuffereImage image1、BuffereImage image2)引发IOException{
BuffereImage image3=新的BuffereImage(image1.getWidth(),image1.getHeight(),image1.getType());
内色;
对于(int x=0;x


您可以尝试使用applitools()进行图像比较测试。它将图像保存在服务器上,并仅在出现故障时在日志中打印到图像的链接。因此,如果您的测试通过(图像相同),您将看到一个绿色测试。如果图像之间存在差异-测试将失败,您将在日志中看到指向applitools的链接,在该链接中,您将获得具有突出显示的差异区域的图像,并将能够看到基线图像(与自己的眼睛进行比较:)。

您可以尝试使用applitools()进行图像比较测试。它将图像保存在服务器上,并仅在出现故障时在日志中打印到图像的链接。因此,如果您的测试通过(图像相同),您将看到一个绿色测试。如果图像之间存在差异-测试将失败,您将在日志中看到指向applitools的链接,在该链接中,您将获得具有突出显示的差异区域的图像,并将能够看到基线图像(与自己的眼睛进行比较:)。

以下是我所做的。。。 打开图像并将图像存储为Java IO文件

NewImage= new File(NewImagePath) //gets newImage from path NewImagePath
Original= new File(OriginalPath) // gets Original from pathOriginalPath
创建了一个函数“getImagePercentage”,用于比较映像和retruns机器的百分比百分比,如果两个映像相同,则为100%,否则为任何其他值

将此方法调用为:
getImagePercentage(原始、新图像)

getImagePercentage(文件fileA,文件fileB){//函数getImagePercentage的开始
def百分比=0;
BuffereImage biA=ImageIO.read(fileA);//将fileA读入BuffereImage
DataBuffer dbA=biA.getData().getDataBuffer();
int sizeA=dbA.getSize();
BuffereImage biB=ImageIO.read(fileB);//将fileA读入BuffereImage
DataBuffer dbB=biB.getData().getDataBuffer();
int sizeB=dbB.getSize();
整数计数=0;
//比较数据缓冲区对象//
if(sizeA==sizeB){//检查两个buffereImage的大小
对于(int i=0;i
以下是我所做的。。。 打开图像并将图像存储为Java IO文件

NewImage= new File(NewImagePath) //gets newImage from path NewImagePath
Original= new File(OriginalPath) // gets Original from pathOriginalPath
创建了一个函数“getImagePercentage”,用于比较映像和retruns机器的百分比百分比,如果两个映像相同,则为100%,否则为任何其他值

将此方法调用为:
getImagePercentage(原始、新图像)

getImagePercentage(文件fileA,文件fileB){//函数ge的开始