【问题标题】:Convert RGBA to ARGB_8888 in Android在 Android 中将 RGBA 转换为 ARGB_8888
【发布时间】:2013-03-27 12:48:30
【问题描述】:

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

我想使用:

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

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

有什么想法吗?

谢谢!

【问题讨论】:

  • 你想要 alpha 作为最后一个通道吗?
  • 没有。我将它作为最后一个通道 (RGBA) 并需要将其转换为 (ARGB) - alpha 是第一个(我猜)
  • 你有原始像素值吗?
  • 我使用 onPreviewFrame 从相机获取图像,将其发送到我的 C++ 代码,它将其从 NV21 转换为 RGBA,当我将其取回时 - 我想将其转换为 ARGB_8888
  • 如果你有原始 RGBA 像素,在 C/C++ 中,转换应该很容易执行

标签: java android rgba argb


【解决方案1】:

(Oldie but...) 你也可以用 Java 代码交换字节顺序。访问我写的here 的位图字节的示例可能会有所帮助......

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

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;
}

【讨论】:

  • 更正这行代码:pixelOut[pixel++] = (int)( a
  • 也:pixelOut[pixel]= (int)( a
【解决方案2】:

您可以使用 OpenCV(您可以将它与 NDK 一起使用)。 然后使用this methodcvCvtColor(sourceIplimage, destinationIplImage, code)

【讨论】:

    猜你喜欢
    • 2013-08-07
    • 2021-09-18
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多