Results 1 to 9 of 9
  1. #1
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Find Pixel With RGB Color?

    I'm trying to scan my desktop for a certain pixel color. I have made a function that scans the screen, but its too slow. I was wondering if there is a way to make this faster or if i'm just approaching this wrong.

    Here is my function:
    Java Code:
        public void findColor(int neededColor) throws AWTException {
            Robot r = new Robot();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            int found = 0;
            for (int h = 0; h < (screenSize.height); h++) {
                for (int w = 0; w < (screenSize.width); w++) {
                    Color foundColor = r.getPixelColor(w, h);
                    
                    if(neededColor == foundColor.getRGB()) {
                        found++;
                    } else {
                        
                    }
                }
            }
            System.out.println(found);
        }
    Its too slow, but works.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Find Pixel With RGB Color?

    I imagine that it's going to be slow given the number of data points you are iterating through. If you can limit the size of the screen real estate that you have to go through, of course the program will run quicker.

  3. #3
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Find Pixel With RGB Color?

    I'm trying to recognize where a window is located. When the window can be positioned anywhere on the screen.
    Last edited by TyCox94; 09-22-2011 at 04:38 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Find Pixel With RGB Color?

    Why not find the window by other criteria such as by its title which can be done via the operating system, and can be done in Java via JNA?

  5. #5
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Find Pixel With RGB Color?

    Once I find the window, I will need specific coordinates of it's top left corner.

    I'm kinda new to Java so sorry if I'm not making any sense.
    Last edited by TyCox94; 09-22-2011 at 04:55 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Find Pixel With RGB Color?

    Quote Originally Posted by TyCox94 View Post
    Once I find the window, I will need specific coordinates of it's top left corner.

    I'm kinda new to Java so sorry if I'm not making any sense.
    I'm wondering why you are using Java for this when most of this exercise appears to require a programming language that can interact closely with the operating system and Java isn't the best language for this. C or C++ comes to mind as a better fit. If you must use Java, then as I mentioned above, you can use JNA to get the Window you're looking for and its screen coordinates, but I wouldn't categorize this as basic Java but more intermediate to advanced.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Find Pixel With RGB Color?

    Use the Robot to get a BufferedImage of the screen. Then use a BufferedImage method to check each pixel.

    Its faster to use the Robot once instead of getting the information pixel by pixel.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Find Pixel With RGB Color?

    Can I ask where Databases come into this?

  9. #9
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Find Pixel With RGB Color?

    Sorry, I misread. Also I used buffered image and it worked great! Thanks camickr! Also here is my function for future visiters:


    This function is used to find color sequence horizontally. Then setting a global variable as the first neededColor.

    Java Code:
        public void findColorSequence(int neededColor1,int neededColor2, int neededColor3) throws AWTException {
            BufferedImage screenCapture = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );
            ppLocationx = 0;
            ppLocationy = 0;
                            
            for(int h = 0; h < screenCapture.getHeight(); h++) { //set horizontal
                for(int w = 0; w < screenCapture.getWidth(); w++) { //scan vertical
                    if(neededColor1 == screenCapture.getRGB(w, h)) {
                        send("Found Color 1");
                        if(neededColor2 == screenCapture.getRGB(w+1, h)) {
                            send("Found Color 2");
                            if(neededColor3 == screenCapture.getRGB(w+1+1, h)) {
                                send(w);
                                send(h);
                                ppLocationx = w;
                                ppLocationy = h;
                            }
                        }
                    }
                }
            }
        }
    Last edited by TyCox94; 09-23-2011 at 05:06 AM.

Similar Threads

  1. Scanning Image Pixel by Pixel
    By the_transltr in forum Advanced Java
    Replies: 5
    Last Post: 08-28-2012, 04:01 PM
  2. Replies: 4
    Last Post: 11-08-2011, 05:04 AM
  3. can you ask the color of one pixel?
    By liluma in forum New To Java
    Replies: 7
    Last Post: 09-06-2011, 07:53 PM
  4. compare two images pixel by pixel
    By java_bond in forum Advanced Java
    Replies: 6
    Last Post: 03-02-2010, 11:27 AM
  5. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 04:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •