1

I'm getting a byte array from NDK in RGBA format. I need to flush the byte array to a Bitmap (to show in ImageView or Surface).

I would like to use:

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

But can't find a way to convert the RGBA to ARGB_8888 format.

Any ideas?

Thanks!

Roman
  • 4,443
  • 14
  • 56
  • 81

2 Answers2

3

(Oldie but...) You can swap the order of bytes also with Java code. An example of accessing Bitmap bytes I wrote here might be of some help...

Due to public demand (following is untested - I'd still see what's behind that mysterious link) I.E. Following is untested:

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;
}
carlos.baez
  • 1,063
  • 2
  • 11
  • 31
Sami Varjo
  • 128
  • 10
1

You can may be use OpenCV (you can use it with the NDK). then use this method : cvCvtColor(sourceIplimage, destinationIplImage, code)

ahmed_khan_89
  • 2,755
  • 26
  • 49