繁体   English   中英

在Android中将RGBA转换为ARGB_8888

[英]Convert RGBA to ARGB_8888 in Android

我从RGBA格式的NDK获得一个字节数组。 我需要将字节数组刷新到Bitmap(在ImageView或Surface中显示)。

我想用:

    Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bm.copyPixelsFromBuffer(ByteBuffer.wrap(buffer));

但无法找到将RGBA转换为ARGB_8888格式的方法。

有任何想法吗?

谢谢!

(Oldie但是......)您也可以使用Java代码交换字节顺序。 访问我在这里写的Bitmap字节的一个例子可能会有所帮助......

由于公众的需求(以下是未经测试的 - 我仍然会看到这个神秘链接背后的内容)IE Follow未经测试:

public static Bitmap RGBA2ARGB(Bitmap img)
{

   int width  = img.getWidth();
   int height = img.getHeight();

   int[] pixelsIn  = new int[height*width];
   int[] pixelsOut = new int[height*width];

   img.getPixels(pixelsIn,0,width,0,0,width,height);

   int pixel=0;
   int count=width*height;

   while(count-->0){
       int inVal = pixelsIn[pixel];

       //Get and set the pixel channel values from/to int  //TODO OPTIMIZE!
       int r = (int)( (inVal & 0xff000000)>>24 );
       int g = (int)( (inVal & 0x00ff0000)>>16 );
       int b = (int)( (inVal & 0x0000ff00)>>8  );
       int a = (int)(  inVal & 0x000000ff)      ;

       pixelsOut[pixel] = (int)( a <<24 | r << 16 | g << 8 | b );
       pixel++;
   }

   Bitmap out =  Bitmap.createBitmap(pixelsOut,0,width,width,height, Bitmap.Config.ARGB_8888);
   return out;
}

您可以使用OpenCV(您可以将它与NDK一起使用)。 然后使用此方法cvCvtColor(sourceIplimage, destinationIplImage, code)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM