2011-04-28 118 views
6

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

Java机器人类允许我找到某个像素的颜色。但是我需要与之相反,我希望我的程序能够扫描我的屏幕,然后告诉我这个小图像的坐标。那么我可以通过所有的像素与机器人,但它应该比这更快。快多了。

有什么建议吗?

回答

4

那么我可以通过与机器人的所有像素,但它应该比这更快。快多了。

恐怕这正是你必须要做的。

如果所有像素都应该是白色,则可以先采用30像素宽的步骤,如果您找到白色像素,则采取5像素级,然后如果这些像素也是白色,则检查方块中剩余的像素。

事情是这样的:

.  .  .  .  .  . 



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

     . . . . 
.  .  .  ..........  . 
          .......... 
          .......... 
          .......... 
          .......... 
.  .  .  .......... 
+0

但它会是多快呢?编号喜欢用它来在游戏中获得好分数..但通过所有这些..不是它会永远处理它吗? – Jaanus 2011-04-28 20:13:00

+1

试一试。我曾经为一个俄罗斯方块机器人做过一次......它工作得很好(即使扫描一个俄罗斯方块领域似乎比你的情况要容易一些)。 – aioobe 2011-04-28 20:20:00

+1

取决于你可以有多聪明。可能有办法做一些二进制搜索的形式,或者可能有各种捷径可供选择。例如,您想要查找30x30像素的白色正方形,这意味着如果当前像素不是白色,则可以安全地跳过30个像素,而不会冒丢失正方形的风险。这一切都取决于你期望在屏幕上。 – Doug 2011-04-28 20:21:16

4

其实,还有一个更simpliler或更可靠的解决方案这一点。您可以在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()); 
    } 
} 

另见How to use Sikuli inside your Java programs进行安装。

+0

我有点困惑。您正在使用我在sikuli-standalone.jar中找到的类,该类是从code.google的网站下载的,并且有关于code.google的文档....但是,您发布的链接似乎指的是另一个sikuli,它有不同的类。我下载了这两个库。您使用的那个工作正常,但很难找到HOWTO。另一个有更好的文档(你发布了链接),但它根本不起作用。我真的很困惑,有两种类型的sikuli,或者是什么?谢谢。 – Marko 2014-03-19 08:28:48

+0

我不太记得,但我认为我只在底部共享链接,以便安装/设置Sikuli以用于Java项目。这可能只是在步骤5之前才有意义,我不会遵循那里演示的示例代码。您应该遵循code.google的文档。什么不工作?说实话,我还没有尝试过直接使用Java的Sikuli,只使用JRuby。但是如果它适用于JRuby,我不明白为什么它不适用于Java。 – Noz 2014-03-19 20:04:55

+0

sikuli-standalone **。jar对我来说很好(我从code.google下载的那个),我也发现了很多关于如何使用它的类的例子。所以现在一切都很好。我只是好奇,如果有两种类型的sikuli。 – Marko 2014-03-20 21:59:52