1

我有一张图片,我想出了如何使用机器人和 getPixelColor() 来获取某个像素的颜色。图像是我正在控制的一个角色,我希望机器人不断地扫描图像,并告诉我它周围的像素是否等于某种颜色。这是可能吗?谢谢!

4

1 回答 1

1

我自己,我会使用机器人提取比“字符”大一点的图像,然后分析获得的 BufferedImage。当然,细节将取决于你的程序的细节。可能最快的方法是获取 BufferedImage 的 Raster,然后获取那个 dataBuffer,然后获取那个数据,然后分析返回的数组。

例如,

// screenRect is a Rectangle the contains your "character" 
// + however many images around your character that you desire
BufferedImage img = robot.createScreenCapture(screenRect);
int[] imgData = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

// now that you've got the image ints, you can analyze them as you wish.
// All I've done below is get rid of the alpha value and display the ints.
for (int i = 0; i < screenRect.height; i++) {
  for (int j = 0; j < screenRect.width; j++) {
    int index = i * screenRect.width + j;
    int imgValue = imgData[index] & 0xffffff;
    System.out.printf("%06x ", imgValue );
  }
  System.out.println();
}
于 2012-09-30T01:39:08.183 回答