【问题标题】:Stopping script once color is no longer found in BufferedImage Java在 BufferedImage Java 中不再找到颜色后停止脚本
【发布时间】:2019-05-12 15:11:07
【问题描述】:

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

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

    }
}}}}

【问题讨论】:

    标签: java if-statement while-loop bufferedimage


    【解决方案1】:

    这样的工作是否可行。基本上我只是用布尔'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;
                }
            }
        }
    }
    

    【讨论】:

    • 显然我在该代码中将 isFound 声明为 isNotFound 。所以如果你只是将 isNotFound 更改为 isFound
    • 我是 Java 新手。虽然我认为代码应该可以工作,但似乎没有?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2010-11-29
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    相关资源
    最近更新 更多