java - 一旦在 BufferedImage Java 中不再找到颜色就停止脚本

标签 java if-statement while-loop bufferedimage

我目前有一个脚本,可以获取某个区域的屏幕截图并搜索该区域的每个颜色值。但是,我希望一旦在图像的任何区域中找不到特定颜色,脚本就停止运行。我当前的脚本会在像素颜色不正确时停止,这不是我想要的。

import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    public static void main(String args[]) throws AWTException {
        int i = 0;
        while (i < 1){
        BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        BufferedImage image2 = new Robot().createScreenCapture(new Rectangle(70, 102,200,222));
\        for (int y = 0; y < image2.getHeight(); y++) {
            for (int x = 0; x < image2.getWidth(); x++) {
                Color pixcolor = new Color(image2.getRGB(x, y));
                int red = pixcolor.getRed();
                int green = pixcolor.getGreen();
                int blue = pixcolor.getBlue();
                System.out.println("Red  = " + red);
                System.out.println("Green  = " + green);
                System.out.println("Blue  = " + blue);

                if (red == 253 && green == 222 && blue == 131){
                            continue;
                        }
                        else {
                            System.out.println(x);
                            System.out.println(y);
                            i ++;
                            System.exit(1);;
                        }

    }
}}}}

最佳答案

像这样的东西会起作用吗?基本上我只是使用 boolean 值“isFound”来记住 如果在图片中找到该颜色,如果没有,则 while 循环结束。

import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    public static void main(String args[]) throws AWTException {
        boolean isFound = false; // before was Boolean isNotFound = true;
        while (!isFound) {
            BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            BufferedImage image2 = new Robot().createScreenCapture(new Rectangle(70, 102, 200, 222));
            isFound = false;
            for (int y = 0; y < image2.getHeight(); y++) {
                for (int x = 0; x < image2.getWidth(); x++) {
                    Color pixcolor = new Color(image2.getRGB(x, y));
                    int red = pixcolor.getRed();
                    int green = pixcolor.getGreen();
                    int blue = pixcolor.getBlue();
                    System.out.println("Red  = " + red);
                    System.out.println("Green  = " + green);
                    System.out.println("Blue  = " + blue);

                    if (red == 253 && green == 222 && blue == 131) {
                        isFound = true;
                        break;
                    }


                }
                if (isFound) break;
            }
        }
    }
}

关于java - 一旦在 BufferedImage Java 中不再找到颜色就停止脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100569/

相关文章:

c++ - 打开一个 fstream 进行读/写(二进制)

python - While循环中断问题(Python)

c - 当输入大于 10 时,While 循环会产生致命的运行时错误

c++ - 为什么 if 语句不起作用

java - 基于文本的圆点和十字

c++ - 这个无操作 while 循环用于断言宏的原因是什么?

javascript - 如何在 Chrome 中访问 USB 设备?

java - 与 Web 服务的相互身份验证

java - Java中的无锁且大小受限的队列

java - jboss 无法处理超过 3000 个请求