在 BufferedImage 中搜索颜色 Java
Search for color in BufferedImage Java
我目前有一个脚本可以创建屏幕的缓冲图像,然后列出特定像素的值。但是,我正在尝试在整个缓冲图像中搜索特定颜色。有办法吗?
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);
}
}
您可以对图像的每个 (x, y) 坐标使用嵌套 for
循环(x
在 0 和 image.getWidth()
之间,y
在 0 之间和 image.getHeight()
) 并比较给定位置的颜色是否等于您要查找的颜色。
我目前有一个脚本可以创建屏幕的缓冲图像,然后列出特定像素的值。但是,我正在尝试在整个缓冲图像中搜索特定颜色。有办法吗?
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);
}
}
您可以对图像的每个 (x, y) 坐标使用嵌套 for
循环(x
在 0 和 image.getWidth()
之间,y
在 0 之间和 image.getHeight()
) 并比较给定位置的颜色是否等于您要查找的颜色。