【问题标题】:Get Pixel Color around an image获取图像周围的像素颜色
【发布时间】:2012-09-30 00:51:48
【问题描述】:

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

【问题讨论】:

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

标签: java pixel


【解决方案1】:

我自己,我会使用Robot提取比“字符”大一点的图像,然后分析获得的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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多