Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

performance - Fast Pixel Search in Java

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);
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...