开发者

Convert RBG char array to RGB int value in Java

开发者 https://www.devze.com 2023-01-17 05:25 出处:网络
I have a char[] rbg containing : rgb[0] : red value rgb[1] : green value rgb[2] : blue value I want to 开发者_StackOverflow社区use the method BufferedImage::setRGB(x, y, int rgb)

I have a char[] rbg containing :

  • rgb[0] : red value
  • rgb[1] : green value
  • rgb[2] : blue value

I want to 开发者_StackOverflow社区use the method BufferedImage::setRGB(x, y, int rgb)

How can I proceed to convert the char array to the int value ?

Note : I use Java

Edit : I'm looking for the fastest solution

Thanks


int value = ((255 & 0xFF) << 24) | //alpha
            (((int)rgb[0] & 0xFF) << 16) | //red
            (((int)rgb[1] & 0xFF) << 8)  | //green
            (((int)rgb[2] & 0xFF) << 0); //blue


import java.awt.Color;

Color color = new Color(rgb[0],rgb[1],rgb[2]);
int rgb = color.getRGB();

ref : http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Color.html#getRGB%28%29


int rgb2 = rgb[0] << 16 | rgb[1] << 8 | rgb[2];

Maybe you will have to reverse the order of indices.


BufferedImage buffImg = new BufferedImage(176, 144, BufferedImage.TYPE_INT_RGB);

            /*Setting RGB values to BufferedImage*/
            int[] raw = new int[rgbCameraData.length * 4 / 3];
            for (int i = 0; i < rgbCameraData.length / 3; i++) {
                raw[i] = 0xFF000000
                        | ((rgbCameraData[3 * i + 0] & 0xFF) << 16)
                        | ((rgbCameraData[3 * i + 1] & 0xFF) << 8)
                        | ((rgbCameraData[3 * i + 2] & 0xFF));
            }
            buffImg.setRGB(0, 0, 176, 144, raw, 0, 176);
            System.out.println("BufferedImage  :" + img);
0

精彩评论

暂无评论...
验证码 换一张
取 消