Creating an image library/patterns in java

Asked by Simon K

I'm pretty new to Java so please bear with me.

I've got a folder where I store all the screenshots that I use for my tests. Rather than defining the patterns one by one like this:

saveLocation = new Pattern("save_location.png") [similarity and offset go here, removed for readability]

I'd like to create all the patterns in one go, so my friend came up with this, though it doesnt work as intended:

   File folder = new File("D:/SIKULI/library.sikuli/");
   File[] listOfFiles = folder.listFiles();

   Pattern pattern[] = new Pattern[listOfFiles.length];
   for(int i = 0; i < listOfFiles.length; ++i){
    pattern[i] = new Pattern( listOfFiles[i].getName() );
   }

So what I get for e.g. first image
pattern[0] = new Pattern("D:/SIKULI/library.sikuli/firstimage.png");

What I'd like to get is something like this:
pattern(firstimage) = new Pattern ("D:/SIKULI/library.sikuli/firstimage.png");

____________________________________________________________________________

So I decided to use the HashMap to create:
key = file name
value = path

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ImageMap {

  public static void main(String[] args) {

   File folder = new File("D:/SIKULI/library.sikuli/");
   Map<String,String> imageMap = toMap(folder.listFiles());
  }

  private static Map<String,String> toMap(File[] table){
   Map<String,String> imageMap = new HashMap<String, String>();
   for(int i = 0; i < table.length; i++){
    imageMap.put(table[i].getName(), table[i].getAbsolutePath());
   }
   return imageMap;
  }
}

The output here is:
Key = back.png
Value = D:\SIKULI\library.sikuli\back.png

And the problem here is - how do i go about creating patterns that will take the key as part of their name
Pattern Pattern(Key) = new Pattern(value);

e.g.
Pattern Pattern(Back.png) = new Pattern( "D:\SIKULI\library.sikuli\back.png");

This example obviously wont work but I hope you get what I mean and can help me with this issue! :)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

I do not understand this big effort ;-)

As I understand the challenge:

You want to say something like
myPattern("back")
and this be a pattern object created as:
new Pattern( "D:\SIKULI\library.sikuli\back.png");

So make yourself a function, that returns a new Pattern object.

static String images = "path-to-image-repository";

static Pattern myPattern(String image) {
    Pattern p = new Pattern(images+image+".png");
    return p;
}

so taking your first example:

saveLocation = myPattern("save_location")

Revision history for this message
RaiMan (raimund-hocke) said :
#2

Sorry, it is Java written down ;-)

Pattern saveLocation = myPattern("save_location");

Revision history for this message
Simon K (szymonkaz) said :
#3

lol. I'm trying to figure out why have I been working around the issue rather than solving it so simply.

Cheers for the solution, its brutally simple :D

Revision history for this message
Simon K (szymonkaz) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Thanks for feedback ;-)