← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #242405]: Get the BufferedImage reference when calling Region.findAll()

 

Question #242405 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/242405

    Status: Open => Answered

RaiMan proposed the following answer:
since I do not know your exact scenario and overall approach, I made an
example that works with version 1.0.1

It works on this question site on Mac in Safari and was setup and run as normal Java app in Netbeans.
It shows an implementation allowing to sort the matches returned by findAll topdown.
The images are in the project folder in images.sikuli, which in parallel is opened in the Sikuli IDE to capture needed images.

This example shows, how to use findAll() on a region and on the
ScreenImage of the same region, both yielding the same result.

With version 1.1.0 there will be even more convenience features to
support such a scenario.

the stuff zipped can be downloaded:
https://dl.dropboxusercontent.com/u/42895525/Test.zip

package test;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
//a ref to sikuli-java.jar is in the projects class path
import org.sikuli.script.*;
import org.sikuli.basics.*;

public class Test {
  
  private static List<Found> matches = new ArrayList<Found>();

  public static void main(String[] args) throws FindFailed {
    // set the imagepath 
    ImageLocator.setBundlePath(new File(System.getProperty("user.dir"), "images.sikuli").getAbsolutePath());
    p("ImagePath %s", ImageLocator.getBundlePath());

    Screen s = new Screen();
    
    // the site as in images.sikuli/ScreenShot.png must be visible
    App.focus("Safari");
    Region win = App.focusedWindow().grow(-20);
    
    // to eat up primary Sikuli initialization
    s.find("person.png");
    
    int debugLight = 0; // set to 0 to switch off highlighting 
    
    // restrict to a region that makes sense
    Region reg = win.find("head.png").below().intersection(win).highlight(debugLight);
    
    long start = (new Date()).getTime();
    // we look on the screen
    Iterator<Match> persons = reg.findAll("person.png");
    
    // copy the matches to a more flexible list
    while (persons.hasNext()) {
      matches.add(new Found(persons.next()));
    }
    // sort the matches top down
    Collections.sort(matches);
    
    p("ScreenMatches %d", matches.size());
    for (Found m : matches) {
      // show the matches
      m.getMatch().highlight(debugLight);
    }
    p("elapsed: %d", (new Date()).getTime() - start);
    matches.clear();
    
    start = (new Date()).getTime();
    // we look in the image taken from reg
    Finder fndr = new Finder(s.capture(reg));
    fndr.findAll("person.png");
    while (fndr.hasNext()) {
      matches.add(new Found(fndr.next(), reg));
    }
    Collections.sort(matches);

    p("ImageMatches %d", matches.size());
    for (Found m : matches) {
      m.getMatch().highlight(debugLight);
    }
    p("elapsed: %d", (new Date()).getTime() - start);
   
    // to get back to the dev IDE at normal and
    App.focus("NetBeans 7.3.1.app");
  }
  
  private static void p(String m, Object... args) {
    System.out.println(String.format("[user] " + m, args));
  }
}

class Found implements Comparable<Found> {
  Match match;
  
  public Found() {
    match = null;
  }
  
  public Found(Match m) {
    match = m; 
  }
  
  public Found(Match m, Region r) {
    match = m;
    m.x += r.x;
    m.y += r.y;
  }
  
  public Match getMatch() {
    return match;
  }
  
  @Override
  public int compareTo(Found f) {
    Match m1 = getMatch();
    Match m2 = f.getMatch();
    if (m1.y < m2.y) {
      return -1;
    }
    if (m1.y > m2.y) {
      return 1;
    }
    return 0;
  }  
}

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.