1

i have a problem regarding a pixel search in java. At the moment my Class/Programm is searching pixel by pixel thats to slow for my purposes. I wan't Java to search the Pixels much faster so i came to the idea to ask you guys. I'm searching for the pixels by an RGB color. This is my Source code:

    final int rot = 0;
    final int gruen = 0;
    final int blau = 0;
    int toleranz = 1;

    Color pixelFarbe;

    Dimension bildschirm = Toolkit.getDefaultToolkit().getScreenSize();


    Robot roboter = null;
    try {
        roboter = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
        OrbitRaider.log("Robot is not working.");
    }

    for(int x = 0; x <= bildschirm.getWidth(); x++)
    {
        for(int y = 0; y <= bildschirm.getHeight(); y++)
        {
            // Pixelfarbe bekommen
            pixelFarbe = roboter.getPixelColor(x, y);

            // Wenn Pixelfarbe gleich unserer Farbe
            if( (pixelFarbe.getRed() < (rot - toleranz)) || (pixelFarbe.getRed() > (rot + toleranz))
                && (pixelFarbe.getGreen() < (gruen - toleranz)) || (pixelFarbe.getGreen() > (gruen + toleranz)) 
                && (pixelFarbe.getBlue() < (blau - toleranz)) || (pixelFarbe.getBlue() > (blau + toleranz)) ){("Could not find Pixel Color");

            }

            else{
                System.out.println("Pixelcolor found at x: " + x + " y: " + y);
            }
        }
    }
Digiben
  • 11
  • 1
  • 3
  • 1
    You haven't exposed how your getPixelColor function works, which is pretty important! If you are calling a native dll every time getPixelColor is called, you are going to have very slow results. Instead, you should take a snapshot of the screen and check the pixels stored in memory. If that is even a problem for you. You need to provide more information. – Chill Oct 30 '13 at 16:20
  • @Chill: It's `java.awt.Robot`. And yes, getting a single pixel makes it surely slow. – maaartinus Oct 30 '13 at 16:23
  • I didn't realize the jdk has pixel scraping capabilities. I was just speaking from experience with other languages. Ibalazscs answer below is exactly what I suggest you do. The major bottleneck is actually getting the initial pixel information, not scanning the information you have. If you get it in bulk, it is much quicker. – Chill Oct 30 '13 at 17:36

1 Answers1

3

Probably it is much faster to create a screen capture with the createScreenCapture method of the Robot class, and then inspect the pixels of this BufferedImage - not with the obvious getRGB method (this is also quite slow because of the color space conversions that occur on each call), but going through the int array which is behind the BufferedImage.

See this: Java - get pixel array from image

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50