我想让我的 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/