获取图像周围的像素颜色

时间:2012-09-30 00:51:49

标签: java pixel

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

1 个答案:

答案 0 :(得分: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();
}