Search for color in BufferedImage Java

0

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);
    }
}
java
bufferedimage
asked on Stack Overflow May 12, 2019 by Seudonym

1 Answer

2

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.

answered on Stack Overflow May 12, 2019 by Denis Fuenzalida

User contributions licensed under CC BY-SA 3.0