[Java] How to check wether images exist in screen or not

Asked by RAGHUBANSH

Hello,

i am using sikuli with eclipse and using java as language.

i want to add one if condition to my code in which i want to check where particular images exist in screen or not.

i made one object of screen s.

then i am trying like

if(s.exist(file name.ext))

but this returns a match typr value so please let me know how to use match variable in this.

Question information

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

yes, s.exists() returns a match object if found - otherwise "Java nothing", that can be used in further actions and find's (subclass of Region). see docs.

So i guess this should work (meta code)

if(s.exist(file name.ext))
    do something if image is found
else
    do something is image is not found
endif

Now it is your turn to make it Java ;-)

Revision history for this message
RAGHUBANSH (raghubanshp) said :
#2

Thanks RaiMan sir,

I have done with my java coding.

here is the code...

public static void launchApplication()
  {
   Screen s=new Screen();
   try
   {
    Pattern p = Pattern.compile("\\d");
    Matcher m = p.matcher("1296729896349.png");
    if (m.find())
    {
     s.click("1296729896349.png",0);
     s.wait("1296729896349.png");
     System.out.print ("hello world");
     s.click("Browse.png",0);
     //s.wait("Browse.png");
    }
   }
    catch(FindFailed e)
    {
     e.printStackTrace();
    }

  }

it will find the matcher in the pattern and if it is finding so then i am clicking on that object.

Please correct me if i am wrong some where.. this code is working fine...

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

Interesting transformation ;-)

In this context here, when talking about a match, it is the Match object returned by a find operation. And a Pattern is an object, that contains the image filename, the needed min. similarity to find and a target offset.

It might be helpful to have a look at the docs at http://sikuli.org/docx/ and read at least through the class description intros ;-)

Your implementation works, sure, but besides the 2 click()'s, there is nothing, that makes sense from the Sikuli view.

This is how based on your question, what you need:

// let the normal notFoundSituation be handled by Java
public static void launchApplication() throws FindFailed
  {
   Screen s=new Screen();

   img1 = "1296729896349.png"
   imgBrowse = "Browse.png"

    if (s.exists(img1))
    {
     s.click(img1,0);
     // alternatively avoids a new find: s.click(s.getLastMatch())
     System.out.print ("hello world");
     s.click(s.exists(imgBrowse,10),0);
     // waits max 10 seconds for the image, if it does not come up, just does nothing: click(null,0)
    }
    else
    {
         System.out.print ("hello NotFound-Hell");
    }
  }

Revision history for this message
RAGHUBANSH (raghubanshp) said :
#4

sir ,,

if (s.exist(img1)) returns object and if need a Boolean , java wont be able to convert match to Boolean.

i have tried it first..

but s.click(s.exists(imgBrowse,10),0); is what was my question for the day.. thanks.. for the help . :)

i am trying to automate a java AWT and swing developed application .. i think i will keep posting the question...

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

You say:
if (s.exist(img1)) returns object and if need a Boolean , java wont be able to convert match to Boolean.

Ok, this was too fluffy (my every day languages are more Ruby and Python - apologize)

That should be Java:
if (null != s.exist(img1))

Feel free to come back every time or even contact me directly using my mail at: https://launchpad.net/~raimund-hocke

Revision history for this message
Sukanya (sukanyaeeegat) said :
#6

My problem is also same and i am trying in Java. when i use code Pattern p = Pattern.compile("\\d");
    Matcher m = p.matcher("1296729896349.png");

There is an error here Pattern.compile is not available and there are no suggestions on that. Kindly help.

Revision history for this message
arwan.khoiruddin (arwan-khoiruddin) said :
#8

Hi Sukanya,

To check whether an image exists and click when it appears, I made this function:

public void waitAppear(Screen s, String img) throws Exception {
        while (true) {
            if (s.exists(img) != null) {
                s.click(img);
                break;
            } else {
                s.wait(1000);
            }
        }
    }

Hope it helps

Revision history for this message
paulo doido silva (petronao) said :
#9

work here

s = new Screen();
        String img ="img/7.PNG";
                    //s.hover("img/7.PNG");
                    if(s.exists("img/7.PNG")!= null)
                    {
                        s.click("img/7.PNG");
                     s.type("nada");
                    }

Revision history for this message
Ilya Levyant (ilyalevyant) said :
#10

screen.exists("img").isValid(); - not works as boolean exists?