gpt4 book ai didi

java - if/then 与像素查找和循环,所有这些目前都在我之上

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:23 26 4
gpt4 key购买 nike

我想截取一张屏幕截图,如果像素的 RGB 值正确,则截取另一张屏幕截图并找到下一个像素,否则重复。

这是获取像素的代码,它的作用就像一个魅力!

{
BufferedImage image = robot.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == color3.getRGB())
{

break search;
}
}
}
}

我想知道的是我想如何要求它重复这段代码直到像素等于真实颜色。我要找的颜色是:

Color color3 = new Color(114, 46, 33);

好的上下文,我正在构建一个程序,该程序会执行以下步骤,一个打开给定的谜题,我将其保留下来,因为我可以使用简单的像素数据,然后需要将鼠标居中于中心像素上。问题是我不能只使用第二个获取像素图像,因为游戏需要一段时间才能打开相关的 jpanel,所以我需要我的程序等待,直到它找到一个指示游戏已打开的像素,然后才开始寻找使鼠标居中的像素。

最佳答案

您可以将屏幕截图代码分离到一个方法中并调用它,直到获得所需的结果:

public boolean checkColor(Color inputColor) {
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 true;
}
}
}
return false;
}

如果可以在屏幕截图中找到给定的inputColor,则此方法将返回 true。然后您可以在循环中使用它,如下所示:

Color newColor = ...;

while (!checkColor(newColor)) {
new Color = new Color(114, 46, 33);
// Or change color in here for every iteration
}

如果无法将屏幕截图与 newColor 匹配,此循环将终止。

关于java - if/then 与像素查找和循环,所有这些目前都在我之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9090693/

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