Java在屏幕上找到图像

你能给我一些关于如何在屏幕上找到图像的提示吗? 我的意思是,一个简单的像素组合。 例如,它找到30×30像素白色方块的坐标。

Java机器人类允许我找到某些像素的颜色。 但我需要反对,我希望我的程序扫描我的屏幕,然后告诉我这个小图像的坐标。 好吧,我可以通过机器人检查所有像素,但它应该比这更快。 快多了。

有什么建议么?

好吧,我可以通过机器人检查所有像素,但它应该比这更快。 快多了。

我担心这正是你必须要做的。

如果所有像素都应为白色,则可以先取30个像素宽的步长,如果找到白色像素,则说5个像素步长,然后如果这些像素也是白色,则检查方形中的剩余像素。

像这样的东西:

. . . . . . . .......... . . . ...... . . . . . . . . . . . .......... . .......... .......... .......... .......... . . . .......... 

实际上,对此有一个更简单或更可靠的解决方案。 您可以在Java应用程序中实现Sikuli库,以在屏幕上发现图像元素并与它们进行交互。 它的目的是自动化UI测试,但我认为它可以很容易地满足您的需求。

示例应用程序( 来源 ):

 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://sofzh.miximages.com/java/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()); } } 

另请参阅如何在Java程序中使用Sikuli进行设置。