抓取给定像素RGB的x,y坐标? [英] Grab x,y coordinates for a given pixel RGB?

查看:84
本文介绍了抓取给定像素RGB的x,y坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个屏幕截图方法,还有一个 BufferedImage 实例.我想知道是否有可能在图像数据中搜索特定的RGB,然后返回像素的X,Y坐标.

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?

推荐答案

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};
    }
}

用法:

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

这篇关于抓取给定像素RGB的x,y坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆