Search code examples
javaperformanceawtawtrobot

Why does this code run so slowly?


I'm attempting to use these methods to find a color in a rectangular area on the screen. However there are sometimes millions of pixels on a screen, and I'm only achieving about 35 iterations of getColor a second at the moment. There must be something in my code causing this to run extremely slowly.

How can I scan my screen quicker than this? Ideally I'd like to scan the entire screen for a color in less than a second, not 8 hours as it stands now :P

Here are my two methods.

public static int getColor(int x, int y){
    try{
        return(robot.getPixelColor(x, y).getRGB() * -1);
    }catch(Exception e){
        System.out.println("getColor ERROR");
        return 0;
    }
}

//returns first instance of color,
//Begins top left, works way down to bottom right
public static Point findColor(Box searchArea, int color){
    System.out.println("Test");
    if(searchArea.x1 > searchArea.x2){
        int temp = searchArea.x1;
        searchArea.x1 = searchArea.x2;
        searchArea.x2 = temp;
    }
    if(searchArea.y1 > searchArea.y2){
        int temp = searchArea.y1;
        searchArea.y1 = searchArea.y2;
        searchArea.y2 = temp;
    }
    for(int i = searchArea.x1;i <=searchArea.x2; i++){
        for(int j = searchArea.y1;j<=searchArea.y2;j++){
            if(getColor(i, j) == color){
                return new Point(i, j);

            }
            System.out.println(i + " " + j);
        }
    }
    return new Point(-1, -1);
}

Solution

  • Robot#getColor will be very slow, especially when used in this manner.

    A better solution would be to grab a screen shot (even in small chunks) and process the resulting BufferedImage.

    Using the following example I got...

    Took 0 seconds to scan image
    Took 3 seconds to scan screen
    

    For an area of 10x10

    Example code...

    import java.awt.AWTException;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.image.BufferedImage;
    import java.util.concurrent.TimeUnit;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class TestColorGrab {
    
        private static Robot robot;
    
        public static void main(String[] args) {
            try {
                robot = new Robot();
            } catch (AWTException ex) {
                Logger.getLogger(TestColorGrab.class.getName()).log(Level.SEVERE, null, ex);
            }
            new TestColorGrab();
        }
    
        public TestColorGrab() {
            Rectangle bounds = new Rectangle(0, 0, 10, 10);
            long start = System.currentTimeMillis();
            scanImageArea(bounds);
            System.out.println("Took " + ((System.currentTimeMillis() - start) / 1000), TimeUnit.SECONDS) + " seconds to scan image");
    
            start = System.currentTimeMillis();
            scanRobotArea(bounds);
            System.out.println("Took " + ((System.currentTimeMillis() - start) / 1000) + " seconds to scan screen");
        }
    
        public static int getColor(int x, int y) {
            try {
                return (robot.getPixelColor(x, y).getRGB() * -1);
            } catch (Exception e) {
                System.out.println("getColor ERROR");
                return 0;
            }
        }
    
        public static void scanRobotArea(Rectangle searchArea) {
            for (int i = searchArea.x; i < searchArea.x + searchArea.width; i++) {
                for (int j = searchArea.y; j < searchArea.y + searchArea.height; j++) {
                    getColor(i, j);
                }
            }
        }
    
        public static void scanImageArea(Rectangle searchArea) {
            BufferedImage image = robot.createScreenCapture(searchArea);
            for (int x = 0; x < image.getWidth(); x++) {
                for (int y = 0; y < image.getHeight(); y++) {
                    image.getRGB(x, y);
                }
            }
        }
    
    }