September 04, 2010, 04:17:46 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Image finder  (Read 1615 times)
0 Members and 1 Guest are viewing this topic.
laurens
Member
*

Reputation: 8
Offline Offline
Posts: 121
Referrals: 0

Awards
« on: July 10, 2008, 10:41:38 AM »

Found this on the net only this could be done better

tried to recode it a week ego but failed :(



Can someone try to make this one better?

Code:
public static boolean Searchimage(String imagename, int tolerance, boolean movemouse, int random) {
return findImage(imagename, new Dimension(0, 0), new Dimension(Toolkit.getDefaultToolkit().getScreenSize()), tolerance, movemouse, random);
}


    public static boolean findImage(String path, Dimension start, Dimension end, int tolerance, boolean move, int random) {
  boolean found = false;
  try {
            Robot r = new Robot();
            int checkSum = 0;
            BufferedImage searchImg = null;
            try {
                searchImg = ImageIO.read(new File(path));
            } catch (Exception ex) {
                System.out.println("Couldn't load the image we are searching for.");
            }
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension d = tk.getScreenSize();
            BufferedImage canvasImg = r.createScreenCapture(new Rectangle(d.width, d.height));
            Color firstPixel = new Color(searchImg.getRGB(0, 0));
            for (int y = start.height; y < end.height; y++) {
                for (int x = start.width; x < end.width; x++) {
                    Color curPixel = new Color(canvasImg.getRGB(x, y));
                    if (Math.abs(firstPixel.getRed() - curPixel.getRed()) <= tolerance && Math.abs(firstPixel.getGreen() - curPixel.getGreen()) <= tolerance && Math.abs(firstPixel.getBlue() - curPixel.getBlue()) <= tolerance) {
                        for (int px = 1; px < searchImg.getWidth(); px++) {
                            for (int py = 1; py < searchImg.getHeight(); py++) {
                                Color lapsePixel = new Color(searchImg.getRGB(px, py));
                                if (px + x >= end.width && py + y >= end.height) {
                                    return false;
                                }
                                Color canvasPixel = new Color(canvasImg.getRGB(x + px, y + py));
                                if (Math.abs(lapsePixel.getRed() - canvasPixel.getRed()) <= tolerance + 10 && Math.abs(lapsePixel.getGreen() - canvasPixel.getGreen()) <= tolerance + 10 && Math.abs(lapsePixel.getBlue() - canvasPixel.getBlue()) <= tolerance + 10) {
                                    checkSum++;
                                    if (checkSum >= .35 * (searchImg.getWidth() * searchImg.getHeight())) {
                                        if (move) {
                                            mouse.Movemouse(x + (searchImg.getWidth() / 2) + (int)(random*Math.random()), y + (searchImg.getHeight() / 2) +(int)(random*Math.random()));
                                        }
                                        return true;
                                    }
                                } else if (lapsePixel.getRed() == 0 && lapsePixel.getGreen() == 240 && lapsePixel.getBlue() == 240) {
                                    checkSum++;
                                    if (checkSum >= .35 * (searchImg.getWidth() * searchImg.getHeight())) {
                                        if (move) {
                                            mouse.Movemouse(x + (searchImg.getWidth() / 2) + (int)(random*Math.random()), y + (searchImg.getHeight() / 2) + (int)(random*Math.random()));
                                        }
                                        return true;
                                    }
                                } else {
                                    checkSum = 0;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
        }
        return found;
    }



Logged
Javaforums.net :: a community about Java software development.
« on: July 10, 2008, 10:41:38 AM »

 Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« Reply #1 on: July 10, 2008, 10:53:24 AM »

Uhm, what should the code do? A brief explanation about what you want would be nice.
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
laurens
Member
*

Reputation: 8
Offline Offline
Posts: 121
Referrals: 0

Awards
« Reply #2 on: July 10, 2008, 11:00:45 AM »


Must have dimension's so it only search at selected place

tolerance for when the object changed and is a bit diff then normal

Get the x and y of the item with a string,

Like X-Y (1111-2222)

Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« Reply #3 on: July 27, 2008, 05:51:38 PM »

Can you post the entire file(s) so i can take a look at it?
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.241 seconds with 35 queries.

Google visited last this page August 29, 2010, 03:02:35 AM