gpt4 book ai didi

java - 获取图像周围的像素颜色

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

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

最佳答案

我自己,我会使用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();
}

关于java - 获取图像周围的像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12657776/

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