【发布时间】:2011-04-28 20:05:20
【问题描述】:
你们能给我一些关于如何在屏幕上找到图像的提示吗?我的意思是,一个简单的像素组合。例如,它会找到 30x30 像素的白色正方形的坐标。
Java 机器人类允许我找到某个像素的颜色。但我需要相反,我希望我的程序扫描我的屏幕,然后告诉我这个小图像的坐标。好吧,我可以用 Robot 遍历所有像素,但它应该比这更快。更快。
有什么建议吗?
【问题讨论】:
标签: java image-processing
你们能给我一些关于如何在屏幕上找到图像的提示吗?我的意思是,一个简单的像素组合。例如,它会找到 30x30 像素的白色正方形的坐标。
Java 机器人类允许我找到某个像素的颜色。但我需要相反,我希望我的程序扫描我的屏幕,然后告诉我这个小图像的坐标。好吧,我可以用 Robot 遍历所有像素,但它应该比这更快。更快。
有什么建议吗?
【问题讨论】:
标签: java image-processing
实际上,有一个更简单或更可靠的解决方案。您可以在 Java 应用程序中实现 Sikuli 库来发现屏幕上的图像元素并与之交互。它旨在自动化 UI 测试,但我认为它可以很容易地满足您的需求。
示例应用程序 (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());
}
}
【讨论】:
好吧,我可以用 Robot 遍历所有像素,但它应该比这更快。更快。
恐怕这正是你必须要做的。
如果所有像素都应该是白色的,你可以先走 30 像素宽的步长,如果你找到一个白色像素,就走 5 像素步长,然后如果这些像素也是白色的,检查正方形中剩余的像素。
类似这样的:
. . . . . .
. .......... . . .
......
. . . .
. . . .
. . . .......... .
..........
..........
..........
..........
. . . ..........
【讨论】: