问题描述
我有一张图片,我想出了如何使用robot和getPixelColor()来获取特定像素的颜色的方法.图像是我要控制的字符,我希望机器人不断扫描图像,并告诉我周围的像素是否等于某种颜色.这是可能吗?谢谢!
I have an image, and I figured out how to use robot and getPixelColor() to grab the color of a certain pixel. The image is a character that I'm controlling, and I want robot to scan around the image constantly, and tell me if the pixels around it equal a certain color. Is this at all possible? Thanks!
推荐答案
我自己,我将使用机器人提取比字符"稍大的图像,然后分析获得的BufferedImage.当然,细节将取决于您的程序的细节.可能最快的方法是获取BufferedImage的Raster,然后获取thatsdataBuffer,然后获取thats数据,并分析返回的数组.
Myself, I'd use the Robot to extract the image that's just a little larger than the "character", and then analyze the BufferedImage obtained. The details of course will depend on the details of your program. Probably the quickest would be to get the BufferedImage's Raster, then get thats dataBuffer, then get thats data, and analyze the array returned.
例如,
// 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();
}
这篇关于获取图像周围的像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!