将图像与实际屏幕进行比较 [英] Compare image to actual screen
本文介绍了将图像与实际屏幕进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让我的Java程序将实际屏幕与图片进行比较(截图)。
I'd like to make my Java program compare the actual screen with a picture (screenshot).
我不知道是否可能,但我有在Jitbit(宏录制器)中看到它,我想自己实现它。 (也许用这个例子你明白我的意思)。
I don't know if it's possible, but I have seen it in Jitbit (a macro recorder) and I would like to implement it myself. (Maybe with that example you understand what I mean).
谢谢
----编辑 - ---
换句话说,是否可以检查图像是否显示?要查找并比较屏幕中的像素?
----edit----- In other words, is it possible to check if an image is showing in? To find and compare that pixels in the screen?
推荐答案
好的,所以几天后我找到了答案。
Ok then, so I found an answer after a few days.
此方法采用屏幕截图:
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";
}
}
这篇关于将图像与实际屏幕进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文