将原始负rgb int值转换回3 number rgb值 [英] Convert a raw negative rgb int value back to a 3 number rgb value
问题描述
好的,所以我正在研究一个接收图像的程序,将一个像素块隔离成一个数组,然后获取该数组中每个像素的每个rgb值。
Ok so I'm working on a program that takes in an image, isolates a block of pixels into an array, and then gets each individual rgb value for each pixel in that array.
当我这样做时
//first pic of image
//just a test
int pix = myImage.getRGB(0,0)
System.out.println(pix);
吐出来-16106634
It spits out -16106634
我需要从这个int值中得到(R,G,B)值
I need to get the (R, G, B) value out of this int value
是否有公式,算法,方法?
Is there a formula, alg, method?
推荐答案
BufferedImage.getRGB(int x,int y)
方法总是返回 TYPE_INT_ARGB
颜色模型。所以你只需要为每种颜色隔离正确的位,如下所示:
The BufferedImage.getRGB(int x, int y)
method always returns a pixel in the TYPE_INT_ARGB
color model. So you just need to isolate the right bits for each color, like this:
int pix = myImage.getRGB(0, 0);
int r = (pix >> 16) & 0xFF;
int g = (pix >> 8) & 0xFF;
int b = pix & 0xFF;
如果你碰巧想要alpha组件:
If you happen to want the alpha component:
int a = (pix >> 24) & 0xFF;
或者你可以使用 Color(int rgba,boolean hasalpha)
构造函数以方便(以性能为代价)。
Alternatively you can use the Color(int rgba, boolean hasalpha)
constructor for convenience (at the cost of performance).
这篇关于将原始负rgb int值转换回3 number rgb值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!