Java 在屏幕上查找图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5824314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java find image on screen
提问by Jaanus
Can you guys give me hints on how to find a image on screen. I mean, a simple pixel combination. For exmaple, it finds coordinates of 30x30 pixel white square.
你们能给我一些关于如何在屏幕上找到图像的提示吗?我的意思是,一个简单的像素组合。例如,它找到 30x30 像素白色方块的坐标。
Java robot class allows me to find color of certain pixel. But i need to opposite, I want my program to scan my screen and then tell me the coords of this little image. Well I could go through all pixels with Robot, but it should be faster than that. Much faster.
Java 机器人类允许我找到某个像素的颜色。但我需要相反,我希望我的程序扫描我的屏幕,然后告诉我这个小图像的坐标。好吧,我可以用 Robot 遍历所有像素,但它应该比这更快。快多了。
Any suggestions?
有什么建议?
采纳答案by aioobe
Well I could go through all pixels with Robot, but it should be faster than that. Much faster.
好吧,我可以用 Robot 遍历所有像素,但它应该比这更快。快多了。
I'm afraid that that's precisely what you'll have to do.
恐怕这正是你必须做的。
If all pixels should be white, you could first take 30 pixel wide steps and if you find a white pixel, take say, 5 pixel steps, and then if these pixels are white too, examine the remaining pixels in the square.
如果所有像素都应该是白色的,您可以先采取 30 像素宽的步长,如果找到一个白色像素,则采取 5 像素步长,然后如果这些像素也是白色的,则检查正方形中的剩余像素。
Something like this:
像这样的东西:
. . . . . .
. .......... . . .
......
. . . .
. . . .
. . . .......... .
..........
..........
..........
..........
. . . ..........
回答by Noz
Actually, there's a much simpliler or more reliable solution to this. You can implement the Sikulilibraries inside your Java application to spot image elements on your screen and interact with them. It was meant to automate UI testing, but I think it can accommodate your needs pretty easily.
实际上,对此有一个更简单或更可靠的解决方案。您可以在 Java 应用程序中实现Sikuli库,以发现屏幕上的图像元素并与之交互。它旨在自动化 UI 测试,但我认为它可以很容易地满足您的需求。
Sample application (source):
示例应用程序(来源):
import java.net.MalformedURLException;
import java.net.URL;
import org.sikuli.api.*;
import org.sikuli.api.robot.Mouse;
import org.sikuli.api.robot.desktop.DesktopMouse;
import org.sikuli.api.visual.Canvas;
import org.sikuli.api.visual.DesktopCanvas;
import static org.sikuli.api.API.*;
public class HelloWorldExample {
public static void main(String[] args) throws MalformedURLException {
// Open the main page of Google Code in the default web browser
browse(new URL("http://code.google.com"));
// Create a screen region object that corresponds to the default monitor in full screen
ScreenRegion s = new DesktopScreenRegion();
// Specify an image as the target to find on the screen
URL imageURL = new URL("http://code.google.com/images/code_logo.gif");
Target imageTarget = new ImageTarget(imageURL);
// Wait for the target to become visible on the screen for at most 5 seconds
// Once the target is visible, it returns a screen region object corresponding
// to the region occupied by this target
ScreenRegion r = s.wait(imageTarget,5000);
// Display "Hello World" next to the found target for 3 seconds
Canvas canvas = new DesktopCanvas();
canvas.addLabel(r, "Hello World").display(3);
// Click the center of the found target
Mouse mouse = new DesktopMouse();
mouse.click(r.getCenter());
}
}
Also see How to use Sikuli inside your Java programsfor setup.
另请参阅如何在 Java 程序中使用 Sikuli进行设置。