java - 将图像与实际屏幕进行比较

标签 java image

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

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

谢谢

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

最佳答案

您可以尝试一下: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
    }
}

关于java - 将图像与实际屏幕进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20661250/

相关文章:

java - 如何将 JAXB 转换为 JSON?

嵌套 HashMap 中的java泛型/继承

java - 如何强制 HtmlUnit 在 GWT 测试用例中解析 UTF-8 中的 Javascript 文件?

linux - 从命令行 Gimp

Java Jackson 序列化抽象类列表

java - 内容的最大长度?

html - CSS如何使DIV边框在宽度上动态变化

java - 图像未使用 Graphics.drawImage() 缩放

java - 突出显示两个图像之间的差异

c++ - 灰度图像到单色度?