Seudonym
Seudonym

Reputation: 562

Search for color in BufferedImage Java

I currently have a script that can create a bufferedimage of the screen, and then list the value for a specific pixel. However, I'm trying to search the entire bufferedimage for a specific color. Is there a way to do this?

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Main {
    public static void main(String args[]) throws IOException, AWTException {
        BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

        int x = 10;
        int y = 10;

        int clr = image.getRGB(x, y);
        int red = (clr & 0x00ff0000) >> 16;
        int green = (clr & 0x0000ff00) >> 8;
        int blue = clr & 0x000000ff;
        System.out.println("Red  = " + red);
        System.out.println("Green  = " + green);
        System.out.println("Blue  = " + blue);
    }
}

Upvotes: 1

Views: 368

Answers (1)

Denis Fuenzalida
Denis Fuenzalida

Reputation: 3346

You could use nested for loops for every (x, y) coordinate of the image (with x between 0 and image.getWidth(), and y between 0 and image.getHeight()) and compare if the color at the given position is equal to the color you are looking for.

Upvotes: 2

Related Questions