Search code examples
javaperformancematrixrgbawtrobot

Speed up looking through matrix


I have code

public static void program() throws Exception{
    BufferedImage input = null;

    long start = System.currentTimeMillis();
    while((System.currentTimeMillis() - start)/1000 < 220){
        for (int i = 1; i < 13; i++){
            for (int j = 1; j < 7; j++){
                input = robot.createScreenCapture(new Rectangle(3+i*40, 127+j*40, 40, 40));
                if ((input.getRGB(6, 3) > -7000000) && (input.getRGB(6, 3)<-5000000)){
                    robot.mouseMove(10+i*40, 137+j*40);
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
                } 
            }
        }
    }
}

On a webpage there's a matrix (12*6) and there will randomly spawn some images. Some are bad, some are good.

I'm looking for a better way to check for good images. At the moment, on good images on location (6,3) the RGB color is different from bad images. I'm making screenshot from every box (40 * 40) and looking at pixel in location (6,3)

Don't know how to explain my code any better

EDIT: Picture of the webpage. External links ok? https://i.sstatic.net/YAZkF.png


Solution

  • I'm not sure what exactly the bottleneck is in your code, but I have a hunch it might be the repeated calls to robot.createScreenCapture.

    You could try calling robot.createScreenCapture on the entire matrix (i.e. a large rectangle that covers all the smaller rectangles you are interested in) outside your nested loops, and then look up the pixel values at the points you are interested in using offsets for the x and y coordinates for the sub rectangles you are inspecting.