• Log in or sign up for an account here: https://www.sythe.org/login

NeedleHaystack FindImage Method

speedster239

Forum Addict

Java Programmers
Joined
Jan 21, 2007
Sythe Gold
0
Messages
313
Location
Arkansas
This is the older version of a "needleHaystack" theory findImage(); method I used in JMacro, It can't hurt to release it so that the java gurus can flame me for its terrible consistency. I'm no longer using the method because 1 it is slow and 2 its not very organized.

I just slopped it together to work with the entire screen but if you have the least bit of competance I assume you can see how to use this elsewhere.

Its already setup with a main method so it will compile and search for icon.png

I should also mention it recognizes the RGB{0,240,240} as a "mask" color, meaning that irrelevant pixels such as backgrounds for a desktop icon which won't be "universal" or backgrounds for toptext can be shaded out on a square image and that it will be recognized as a valid part of the image.

The mouse will move to the "center" of the found image relative to its position on the screen.

It supports "tolerance" for modified/morphed/changed bitmaps such as in runescape.

It appears to have a hard time with a large area such as the screen when searching for a smaller image and brown/red/greyish colors.


Code:
Old Search Outputs for a Dual Core 1.0 gig RAM pc
Image Thread -
Performed findImage() for 1280x1024 pixel screen, searching for "start.png" a 19x19 pixel image in .131ms
Performed findImage() for 1280x1024 pixel screen, searching for "paint.png" a 21x43 pixel image in .101ms
Performed findImage() for 1280x1024 pixel screen, searching for "internet.png" a 16x14 pixel image in .934ms
Performed findImage() for 1280x1024 pixel screen, searching for "go.png" a 85x87 pixel image in .223ms
Performed findImage() for 1280x1024 pixel screen, searching for "run.png" a 3x7 pixel image in .012ms

Code:
package com.cheatmacro;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: Vaughan D
 * Date: Jan 30, 2008
 * Time: 5:29:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class findImage {
    public static boolean found = false;

    public static boolean findImage(String path, Dimension start, Dimension end, int tolerance, boolean move) {
        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) {
                                            r.mouseMove(x + (searchImg.getWidth() / 2), y + (searchImg.getHeight() / 2));
                                        }
                                        found = true;
                                        return found;
                                    }
                                } else if (lapsePixel.getRed() == 0 && lapsePixel.getGreen() == 240 && lapsePixel.getBlue() == 240) {
                                    checkSum++;
                                    if (checkSum >= .35 * (searchImg.getWidth() * searchImg.getHeight())) {
                                        if (move) {
                                            r.mouseMove(x + (searchImg.getWidth() / 2), y + (searchImg.getHeight() / 2));
                                        }
                                        found = true;
                                        return found;
                                    }
                                } else {
                                    checkSum = 0;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception ex) {
        }
        return found;
    }

    public static void main(String[] args) {
        System.out.println(findImage("icon.png", new Dimension(0, 0), new Dimension(Toolkit.getDefaultToolkit().getScreenSize()), 5, true));
    }
}
 
Thank you, take your time and read through it all carefully and its really not that complicated, it's actually poorly written code anyways.
 
duelarena.gg
Back
Top