Java 了解 BufferedImage.getRGB 输出值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25761438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:04:59  来源:igfitidea点击:

Understanding BufferedImage.getRGB output values

javaimage-processingcolorsrgbbufferedimage

提问by leonardo

I'm getting an integer value for the pixel in an image using this method:

我使用此方法获取图像中像素的整数值:

int colour = img.getRGB(x, y);

Then I'm printing out the values and I see that black pixels correspond to a value like "-16777216", a kind of blue to something like "-16755216", etc. Can someone please explain me the logic behind this value?

然后我打印出这些值,我看到黑色像素对应于“-16777216”之类的值,一种蓝色对应于“-16755216”之类的东西,等等。有人可以解释一下这个值背后的逻辑吗?

采纳答案by afzalex

getRGB(int x, int y)return you the value of color pixel at location (x,y).
You are misinterpreting the returned value.
It is in the binary format. like 11...11010101 and that is given to you as int value.
If you want to get RGB (i.e. Red, Green, Blue) components of that value use Color class. e.g.

getRGB(int x, int y)返回位置(x,y)处颜色像素的值。
您误解了返回的值。
它是二进制格式。像 11...11010101,它作为 int 值提供给你。
如果您想获得该值的 RGB(即红色、绿色、蓝色)分量,请使用 Color 类。例如

Color mycolor = new Color(img.getRGB(x, y));

Then you can get Red, Green, or Blue value by using getRed(), getGreen(), getBlue(), getAlpha(). Then an intvalue will be returned by these methods in familiar format having value 0 < value < 255

然后您可以使用getRed(), getGreen(), getBlue(),获得红色、绿色或蓝色值getAlpha()。然后int这些方法将返回一个具有值的熟悉格式的值0 < value < 255

int red = mycolor.getRed();

If you don't wants to use Colorclass then you will need to use bitwise operations to get its value.

如果您不想使用Color类,那么您将需要使用按位运算来获取其值。

回答by icza

The RGB intcolor contains the Red, Green, Blue components of the color in its bits. You have to look at its binary or hexadecimal representation and not look at it as a whole integer number (not look at its decimal representation).

RGBint颜色在其位中包含颜色的红、绿、蓝分量。您必须查看其二进制或十六进制表示,而不是将其视为整数(而不是查看其十进制表示)。

An inthas 32 bits, 3x8 = 24 is used to store the RGB components (8 bits for each) in the following format:

Anint有 32 位,3x8 = 24 用于以以下格式存储 RGB 分量(每个 8 位):

               2          1          0
bitpos      32109876 54321098 76543210
------   --+--------+--------+--------+
bits     ..|RRRRRRRR|GGGGGGGG|BBBBBBBB|

You can extract or set the components using bitmasks:

您可以使用位掩码提取或设置组件:

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;

If the color also has an alpha component (transparency) ARGB, it gets the last remaining 8 bits.

如果颜色也有一个 alpha 分量(透明度)ARGB,它会得到最后剩下的 8 位。

           3          2          1          0
bitpos    10987654 32109876 54321098 76543210
------   +--------+--------+--------+--------+
bits     |AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBB|

And the value:

和价值:

int alpha = (color & 0xff000000) >>> 24; // Note the >>> shift
                                         // required due to sign bit

An alpha value of 255 means that a color is completely opaque and a value of 0 means that the color is completely transparent.

alpha 值为 255 表示颜色完全不透明,值为 0 表示颜色完全透明。

Your color:

你的颜色:

Your color is color = -16755216which has:

你的颜色是color = -16755216

blue : 240         // Strong blue
green:  85         // A little green mixed in
red  :   0         // No red component at all
alpha: 255         // Completely opaque

回答by Aaron Digulla

It's explained in the docs:

在文档中解释

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) [...]

返回默认 RGB 颜色模型 (TYPE_INT_ARGB) 中的整数像素 [...]

So you get 8 bits alpha channel, 8 bits red, 8 bits green, 8 bits blue.

所以你得到 8 位 alpha 通道,8 位红色,8 位绿色,8 位蓝色。

A simple (and slow way) to examine the value is to use new java.awt.Color(colour, true);and then call the getters.

检查值的一种简单(且缓慢的方法)是使用new java.awt.Color(colour, true);然后调用 getter。

Or you can print the value as an unsigned 32bit hex value: Integer.toString(colour, 16). Each two characters of the output will be one part of the ARGB set.

或者您可以将该值打印为无符号的 32 位十六进制值:Integer.toString(colour, 16). 输出的每两个字符将是 ARGB 集的一部分。

回答by Jens

See the implementationof ColorModel.getRgb:

请参阅实现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   }

回答by ben

Actually,You can transform the int to binary string by Integer.toBinaryString(-16755216),which is 11111111000000000101010111110000.it made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF.

实际上,您可以通过Integer.toBinaryString(-16755216) 将int 转换为二进制字符串,即11111111000000000101010111110000。它由4 个字节组成:alpha、红色、绿色、蓝色。这些值是未预乘的,这意味着任何透明度都单独存储在 alpha 分量中,而不是存储在颜色分量中。组件存储如下 (alpha << 24) | (红色<<16) | (绿色 << 8) | 蓝色。每个组件的范围在 0..255 之间,0 表示该组件没有贡献,255 表示 100% 贡献。因此,不透明黑色将是 0xFF000000(100% 不透明,但没有来自红色、绿色或蓝色的贡献),不透明白色将是 0xFFFFFFFF。