将原始负rgb int值转换回3 number rgb值

好的,我正在研究一个接收图像的程序,将一个像素块隔离成一个数组,然后为该数组中的每个像素获取每个单独的rgb值。

当我这样做

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

吐出来-16106634

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

有一个公式,alg,方法?

BufferedImage.getRGB(int x, int y)方法始终返回TYPE_INT_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; 

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

 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();