Use java to find an image on the screen

0

How can I make java read an image on the screen, the intention is to make a bot, once you see the image click on it, img is not in a browser, website, or something on the web, but in a program .

    
asked by anonymous 09.06.2016 / 15:37

1 answer

0

In fact there is a solution for this. You can deploy the Sikuli libraries into your Java application to detect and interact with them on the screen.

This library is designed to automate user interface testing, but can accommodate your needs quite easily!

Here is an example of a small code that does exactly what you intend:

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());
 }
}

You can also consult :

How to use Sikuli in your Java projects

    
09.06.2016 / 15:52