【问题标题】:Search for color in BufferedImage Java在 BufferedImage Java 中搜索颜色
【发布时间】:2019-05-12 02:42:42
【问题描述】:

我目前有一个脚本可以创建屏幕的缓冲图像,然后列出特定像素的值。但是,我正在尝试在整个缓冲图像中搜索特定颜色。有没有办法做到这一点?

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


    【解决方案1】:

    您可以对图像的每个 (x, y) 坐标使用嵌套的 for 循环(x 在 0 和 image.getWidth() 之间,y 在 0 和 image.getHeight() 之间)并比较颜色是否给定位置的颜色与您要查找的颜色相同。

    【讨论】:

    • 实际上效果很好。但是,我认为我需要相反的情况:一旦特定颜色 not 在该区域中,脚本就会停止。我的错。
    猜你喜欢
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2012-11-02
    • 1970-01-01
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多