按颜色查找像素序列

发布于 2025-01-05 02:27:27 字数 675 浏览 4 评论 0原文

我有这段代码:

{
Robot robot = new Robot();
Color inputColor = new Color();


Rectangle rectangle = new Rectangle(0, 0, 1365, 770);

    BufferedImage image = robot.createScreenCapture(rectangle);
    for(int x = 0; x < rectangle.getWidth(); x++) 
    {
        for (int y = 0; y < rectangle.getHeight(); y++) 
        {
            if (image.getRGB(x, y) == inputColor.getRGB()) 
            {
            return 1;
            break;
            }
        }
    }
}

它应该并且确实截取屏幕截图并在其中找到由 inputColor 指定的像素。然而程序要求发生了变化,现在它需要找到与给定字符串匹配的长度为 5 的像素字符串。有没有一种简单的方法可以用现有代码指定它,或者我需要更改它吗?我的意思是,我可以保留现有代码并将 inputColor 定义为具有 5 个像素值的字符串,还是需要更改整个算法?

I have this code:

{
Robot robot = new Robot();
Color inputColor = new Color();


Rectangle rectangle = new Rectangle(0, 0, 1365, 770);

    BufferedImage image = robot.createScreenCapture(rectangle);
    for(int x = 0; x < rectangle.getWidth(); x++) 
    {
        for (int y = 0; y < rectangle.getHeight(); y++) 
        {
            if (image.getRGB(x, y) == inputColor.getRGB()) 
            {
            return 1;
            break;
            }
        }
    }
}

it is supposed to, and does, take a screenshot and find in it a pixel specified by the inputColor. However the program requirements have changed, and now it needs to find a string of pixels 5 long that match a given string. Is there an easy way to specify this with the existing code, or will I need to change it? I mean, can I keep the existing code and define inputColor as a string with the values of the 5 pixels, or do I need to change the whole algorithm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤仙姿 2025-01-12 02:27:27

我认为这样的事情会起作用。不是最好的效率,但它是一个难啃的骨头。

int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth())) {
for (int i = 0; i < pixels.length; i++) {
    if (Array.equals(search, Array.copyOfRange(pixels, i, i + search.length)) {
        //found it
    }
}

搜索将是一个整数数组(您的颜色)。

I think something like this would work. Not the best efficiency, but its a bone to chew.

int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth())) {
for (int i = 0; i < pixels.length; i++) {
    if (Array.equals(search, Array.copyOfRange(pixels, i, i + search.length)) {
        //found it
    }
}

search would be an array of integers(your colors).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文