簡體   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