【问题标题】:Compare image to actual screen将图像与实际屏幕进行比较
【发布时间】:2019-08-12 17:44:56
【问题描述】:

我想让我的 Java 程序将实际屏幕与图片(屏幕截图)进行比较。

我不知道这是否可能,但我在 Jitbit(宏记录器)中看到了它,我想自己实现它。 (也许通过这个例子你明白我的意思)。

谢谢

----编辑----- 换句话说,是否可以检查图像是否正在显示?要查找和比较屏幕中的像素?

【问题讨论】:

  • 这不是你想做的。我从未见过基于位图图像工作的测试工具。
  • 我已经编辑了这个问题,也许现在更清楚了

标签: java image


【解决方案1】:

你可以试试aShot:documentation link

1) aShot 可以忽略您用特殊颜色标记的区域。

2) aShot 可以提供显示图像之间差异的图像。

private void compareTowImages(BufferedImage expectedImage, BufferedImage actualImage) {
    ImageDiffer imageDiffer = new ImageDiffer();
    ImageDiff diff = imageDiffer
            .withDiffMarkupPolicy(new PointsMarkupPolicy()
                    .withDiffColor(Color.YELLOW))
            .withIgnoredColor(Color.MAGENTA)
            .makeDiff(expectedImage, actualImage);

    // areImagesDifferent will be true if images are different, false - images the same
    boolean areImagesDifferent = diff.hasDiff();
    if (areImagesDifferent) {
        // code in case of failure 
    } else {
        // Code in case of success
    }
}

保存有差异的图像:

private void saveImage(BufferedImage image, String imageName) {
    // Path where you are going to save image
    String outputFilePath = String.format("target/%s.png", imageName);
    File outputFile = new File(outputFilePath);
    try {
        ImageIO.write(image, "png", outputFile);
    } catch (IOException e) {
        // Some code in case of failure
    }
}

【讨论】:

    【解决方案2】:

    您可以分两步完成:

    1. 使用 awt.Robot 创建屏幕截图

      BufferedImage image = new Robot().createScreenCapture(new Rctangle(Toolkit.getDefaultToolkit().getScreenSize()));
      ImageIO.write(image, "png", new File("/screenshot.png"));
      
    2. 使用类似的方式比较屏幕截图:How to check if two images are similar or not using openCV in java?

    【讨论】:

    • 重点是我不只是想比较它们。我想知道截取的屏幕截图是否包含第一个。
    【解决方案3】:

    看看Sikuli 项目。他们的自动化引擎基于图像比较。

    我猜,他们内部仍在使用 OpenCV 来计算图像相似度,但是有很多 OpenCV Java 绑定,例如 this,允许从 Java 中这样做。

    项目源码位于:https://github.com/sikuli/sikuli

    【讨论】:

    • 这就是我一直在寻找的东西,我将看看如何获​​取代码或向我的程序添加功能。
    【解决方案4】:

    好吧,几天后我找到了答案。

    此方法截图:

    public static void takeScreenshot() {
            try {
                BufferedImage image = new Robot().createScreenCapture(new Rectangle(490,490,30,30));
    /* this two first parameters are the initial X and Y coordinates. And the last ones are the increment of each axis*/
                ImageIO.write(image, "png", new File("C:\\Example\\Folder\\capture.png"));
    
            } catch (IOException e) {
                e.printStackTrace();
            } catch (HeadlessException e) {
                e.printStackTrace();
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    

    另一个会比较图像

    public static String compareImage() throws Exception {
    
    // savedImage is the image we want to look for in the new screenshot.
    // Both must have the same width and height
    
        String c1 = "savedImage";
        String c2 = "capture";
    
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(c1
                + ".png"));
        BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(
                c2 + ".png"));
        int i, j;
        int k = 1;
        while (((i = in.read()) != -1) && ((j = in1.read()) != -1)) {
            if (i != j) {
                k = 0;
                break;
            }
        }
    
        in.close();
        in1.close();
        if (k == 1) {
    
            System.out.println("Ok...");
            return "Ok";            
    
        } else {
            System.out.println("Fail ...");
            return "Fail";
        }
    }
    

    【讨论】:

    • 这与我在回答中提出的解决方案相同。仅当两个图像相同时,该方法才会返回“OK”,但如果第二个图像包含第一个图像和其他内容,则该方法将不起作用。
    • 在您的回答中,我看不到如何从特定区域截取屏幕截图并稍后比较该区域。
    猜你喜欢
    • 2017-11-02
    • 2021-03-03
    • 2017-08-07
    • 2016-09-05
    • 2012-12-02
    • 1970-01-01
    • 2015-11-01
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多