我之前测试过我的代码,似乎它工作得很好,但现在我的功能在我的Windows屏幕上找不到图像坐标(返回-1,-1)。
JDK版本有所作为吗?或者下面的代码中是否有错误?
public int[] foundImageCoord(String imageName) {
try {
// current screen capture
BufferedImage current = createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
// image to search
BufferedImage icon = ImageIO.read(new File(imageName));
// container
for (int x = 0; x < current.getWidth() - icon.getWidth(); x++) {
for (int y = 0; y < current.getHeight() - icon.getHeight(); y++) {
// image moving in the container
boolean matches = true;
overloop:
for (int x2 = 0; x2 < icon.getWidth() ; x2++) {
for (int y2 = 0; y2 < icon.getHeight() ; y2++) {
if (icon.getRGB(x2, y2) != current.getRGB(x + x2, y + y2)) {
matches = false;
break overloop;
}
}
}
if (matches) { // image found
return new int[] { x, y };
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return new int[] { -1, -1 };
}
在我的函数main中:
WindowsBot bot;
try {
bot = new WindowsBot();
while(true){
int[] iconLocation = bot.foundImageCoord("skype/writeMessage.png");
System.out.println("x : " + iconLocation[0] + " y : " + iconLocation[1]);
//bot.click(iconLocation);
}
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是控制台日志:
x : -1 y : -1
x : -1 y : -1
x : -1 y : -1
x : -1 y : -1
x : -1 y : -1