gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:54:31 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com