如何从Java中的R,G,B值获取RGB像素值以获取BufferedImage
问题描述:
我可以使用以下函数获取r,g,b值.
I can get the r,g,b values using the following functions.
int rgb=bImg.getRGB(i, j);
int r=(rgb>>16) & 0xff;
int g=(rgb>>8) & 0xff;
int b=(rgb) & 0xff;
现在我对这些值进行一些操作,并希望使用以下函数设置rgb值
Now I do some operations in on those values and want to set the rgb values using the following function
bImg.setRgb(int x,int y,int rgb)
但是我不知道如何从R,G,B值计算出rgb.
But i do not know how to calculate rgb from R,G,B values.
答
int rgb = (r<<16) + (g<<8) + b;
或
int rgb = (r<<16) | (g<<8) | b;
将执行逆运算,并将 r
, g
和 b
存储为解码后的单个整数.
will do the inverse operation and store r
, g
and b
into a single integer as you have decoded.