我使用以下方法获取图像中像素的整数值:
int colour = img.getRGB(x, y);
然后我打印出这些值,我看到黑色像素对应于"-16777216“这样的值,蓝色对应于"-16755216”之类的东西。请有人解释一下这个值背后的逻辑。
发布于 2014-09-10 09:12:51
getRGB(int x, int y)
在(x,y)位置返回颜色像素的值。
您误解了返回的值。
它是二进制格式的。比如11.11010101,这是作为int值给你的。
如果您想获得RGB (即红色、绿色、蓝色)组件的值,请使用Color类。例如:
Color mycolor = new Color(img.getRGB(x, y));
然后,您可以使用getRed()
、getGreen()
、getBlue()
、getAlpha()
获取红色、绿色、蓝色或阿尔法值。然后,这些方法将以具有值int
的熟悉格式返回一个0 < value < 255
值。
int red = mycolor.getRed();
如果不想使用Color
类,则需要使用按位操作来获得其值。
发布于 2014-09-10 09:12:39
RGB int
颜色中包含红色、绿色、蓝色的部分。您必须查看它的二进制或十六进制表示,而不是将它看作一个整数(而不是它的十进制表示)。
int
有32位,3x8 = 24用于以下列格式存储RGB组件(每个8位):
2 1 0
bitpos 32109876 54321098 76543210
------ --+--------+--------+--------+
bits ..|RRRRRRRR|GGGGGGGG|BBBBBBBB|
可以使用位掩码提取或设置组件:
int color = img.getRGB(x, y);
// Components will be in the range of 0..255:
int blue = color & 0xff;
int green = (color & 0xff00) >> 8;
int red = (color & 0xff0000) >> 16;
如果颜色也有一个alpha组件(透明) ARGB,那么它将得到最后的8位。
3 2 1 0
bitpos 10987654 32109876 54321098 76543210
------ +--------+--------+--------+--------+
bits |AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBB|
以及价值:
int alpha = (color & 0xff000000) >>> 24; // Note the >>> shift
// required due to sign bit
alpha值255表示颜色完全不透明,值0表示颜色完全透明。
您的颜色:
您的颜色是color = -16755216
,它有:
blue : 240 // Strong blue
green: 85 // A little green mixed in
red : 0 // No red component at all
alpha: 255 // Completely opaque
发布于 2014-09-10 09:14:02
见实现 of ColorModel.getRgb
589 public int getRGB(int pixel) {
590 return (getAlpha(pixel) << 24)
591 | (getRed(pixel) << 16)
592 | (getGreen(pixel) << 8)
593 | (getBlue(pixel) << 0);
594 }
https://stackoverflow.com/questions/25761438
复制相似问题