Kimv
Kimv

Reputation: 13

Grab x,y coordinates for a given pixel RGB?

I have a screenshot method in my code and a BufferedImage instance. I'm wondering if it's possible to search the image data for a specific RGB, then return the X,Y coordinates for the pixel.

What could I use for that? Is it possible at all?

Upvotes: 1

Views: 4160

Answers (2)

Martijn Courteaux
Martijn Courteaux

Reputation: 68847

public int[] searchForColor(BufferedImage bi, int searchColor)
{
    for (int x = 0; x < bi.getWidth(); ++x)
    for (int y = 0; y < bi.getHeight(); ++y)
    {
        if ((bi.getRGB(x, y) & 0x00FFFFFF) == searchColor)
            return new int[]{x, y};
    }
}

Usage:

BufferedImage bi = takeScreenShot();
int searchColor = 0x2D5E83; // A random color
int[] coordinate = searchForColor(bi, searchColor);
int x = coordinate[0];
int y = coordinate[1];

Upvotes: 2

PeterMmm
PeterMmm

Reputation: 24630

http://www.roseindia.net/java/java-get-example/get-color-of-pixel.shtml and loop thru the image data

Upvotes: 1

Related Questions