[英]Get pixel color in java
I know this is an expensive operation and I already tried to use the robot.getPixelColor()
function but it works slow, can only calculate like 5 times in a second. 我知道这是一项昂贵的操作,我已经尝试使用robot.getPixelColor()
函数,但是它的运行速度很慢,每秒只能计算5次。
What I'm saying is that 5 times is too small for what I actually want to do, but 20 should be enough. 我的意思是说5倍对于我实际想做的事情来说太小了,但是20倍就足够了。 So I ask you if you can suggest me some optimisations to make to my actual code in order to get this result. 所以我问你是否可以建议我对我的实际代码进行一些优化,以便获得此结果。
My code is: 我的代码是:
while (true) {
color = robot.getPixelColor(x, y);
red = color.getRed();
green = color.getGreen();
blue = color.getBlue();
// do a few other operations in constant time
}
I don't know if this would help, but x
and y
don't change inside the while
loop. 我不知道这是否有帮助,但是x
和y
在while
循环内不会改变。 So it's all the time the same pixel coordinates. 所以一直都是相同的像素坐标。
Thanks in advance! 提前致谢!
EDIT: The pixel color will be taken from a game which will run at the same time with the java program, it will keep changing. 编辑:像素颜色将取自将与Java程序同时运行的游戏,它将不断变化。 The only thing is that are always the same coordinates. 唯一的事情是总是相同的坐标。
I'm assuming the color is represented as a 32-bit int encoded as ARGB. 我假设颜色表示为编码为ARGB的32位int。 In that case, instead of calling a method, you could just do bit masking to extract the colors, and that may end up being faster because you don't waste the overhead of calling a method. 在那种情况下,您可以只执行位掩码以提取颜色,而不用调用方法,这样最终可能会更快,因为您不会浪费调用方法的开销。 I'd recommend doing something like this: 我建议做这样的事情:
int color = robot.getPixelColor(x,y);
int redBitMask = 0x00FF0000;
int greenBitMask = 0x0000FF00;
int blueBitMask = 0x000000FF;
int extractedRed = (color & redBitMask) >> 16;
int extractedGreen = (color & greenBitMask) >> 8;
int extractedBlue = (color & blueBitMask);
Bit shifting and bitwise operations tend to be very fast. 移位和按位运算往往非常快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.