简体   繁体   English

Java 在屏幕上查找图像

[英]Java find image on screen

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. Java 机器人 class 允许我找到某个像素的颜色。 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.好吧,我可以用机器人通过所有像素 go ,但它应该比这更快。 Much faster.快多了。

Any suggestions?有什么建议么?

Actually, there's a much simpliler or more reliable solution to this.实际上,有一个更简单或更可靠的解决方案。 You can implement the Sikuli libraries inside your Java application to spot image elements on your screen and interact with them.您可以在 Java 应用程序中实现Sikuli库,以发现屏幕上的图像元素并与之交互。 It was meant to automate UI testing, but I think it can accommodate your needs pretty easily.它旨在自动化 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 programs for setup.另请参阅如何在 Java 程序中使用 Sikuli进行设置。

Well I could go through all pixels with Robot, but it should be faster than that.好吧,我可以用机器人通过所有像素 go ,但它应该比这更快。 Much faster.快多了。

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:像这样的东西:

.        .        .        .        .        .



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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM