Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将原始负rgb int值转换回3个数字的rgb值_Java_Int_Rgb_Negative Number - Fatal编程技术网

Java 将原始负rgb int值转换回3个数字的rgb值

Java 将原始负rgb int值转换回3个数字的rgb值,java,int,rgb,negative-number,Java,Int,Rgb,Negative Number,好的,我正在开发一个程序,它接收一幅图像,将一块像素分割成一个数组,然后获取该数组中每个像素的每个rgb值 当我这么做的时候 //first pic of image //just a test int pix = myImage.getRGB(0,0) System.out.println(pix); 它吐出-16106634 我需要从这个int值中得到(R,G,B)值 是否有公式、alg、方法?该方法始终在类型\u INT\u ARGB颜色模型中返回一个像素。因此,您只需为每种颜色分离正确

好的,我正在开发一个程序,它接收一幅图像,将一块像素分割成一个数组,然后获取该数组中每个像素的每个rgb值

当我这么做的时候

//first pic of image
//just a test
int pix = myImage.getRGB(0,0)
System.out.println(pix);
它吐出-16106634

我需要从这个int值中得到(R,G,B)值

是否有公式、alg、方法?

该方法始终在
类型\u INT\u ARGB
颜色模型中返回一个像素。因此,您只需为每种颜色分离正确的位,如下所示:

int pix = myImage.getRGB(0, 0);
int r = (pix >> 16) & 0xFF;
int g = (pix >> 8) & 0xFF;
int b = pix & 0xFF;
如果您碰巧想要alpha组件:

int a = (pix >> 24) & 0xFF;

或者,为了方便起见,您可以使用构造函数(以性能为代价)。

Cool。确保按正确的方向复制位移位。。。事实上,我第一次打错了,并在编辑中修复了它。
int pix = myImage.getRGB(0,0);
Color c = new Color(pix,true); // true for hasalpha
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();