我有这个代码:
{
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个像素值的字符串吗?或者我需要更改整个算法吗?
我认为这样的东西会起作用。不是最好的效率,但这是一块骨头。
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
}
}
搜索将是一个整数数组(您的颜色)。