1

Folks,

In my Android application, a video stream from an external source need to be displayed on a Canvas view. From my Java code, I pass in ByteBuffer to an underlying C++ library via JNI. The library decodes the stream and builds an RGB bitmap image in ByteBuffer and returns it back.

Now, in my Java code, I have a ByteBuffer that contains width, height and rows of RGB values. I could now draw this in a for loop:

for(int y=0;y<height;y++) {
   for(int x=0;x<width;x++) {
       get the right RGB value from ByteBuffer and draw the pixel
   }
}

I am wondering if there is a more efficient way to do this? Any ideas would be appreciated.

The layout of ByteBuffer data is under my control. Perhaps I can rearrange it for a better performance.

Thank you in advance for your help.

Regards,
Peter

Peter
  • 11,260
  • 14
  • 78
  • 155

3 Answers3

0

Read this Android API "Loading Large Bitmaps Efficiently" official guide. You will use BitmapFactory, that is a class made just for it, and probably do it on a optimized way like others sdk classes. Here the link: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Razec Luar
  • 289
  • 4
  • 18
0

the efficient way is to call copyPixelsFromBuffer. Do not draw pixel one by one. you just need to operate int array, and draw them together. For example:

int[] data8888 = new int[N];
for (int i = 0; i < N; i++) {

        data8888[i] = 78255360;

        }
mBitmap.copyPixelsFromBuffer(makeBuffer(data8888, N));

private static IntBuffer makeBuffer(int[] src, int n) {

         IntBuffer dst = IntBuffer.allocate(n);

         for (int i = 0; i < n; i++) {

             dst.put(src[i]);

         }

         dst.rewind();

         return dst;

     }
yushulx
  • 11,695
  • 8
  • 37
  • 64
0

I was able to adapt the code from this question to decode (Byte[3])RGB streams with the other day. It will probably need to be optimized for your app, but it was the easiest way I've found so far.

Byte[] bytesImage = {0,1,2, 0,1,2, 0,1,2, 0,1,2};
int intByteCount = bytesImage.length;
int[] intColors = new int[intByteCount / 3];
int intWidth = 2;
int intHeight = 2;
final int intAlpha = 255;
if ((intByteCount / 3) != (intWidth * intHeight)) {
    throw new ArrayStoreException();
}
for (int intIndex = 0; intIndex < intByteCount - 2; intIndex = intIndex + 3) {
    intColors[intIndex / 3] = (intAlpha << 24) | (bytesImage[intIndex] << 16) | (bytesImage[intIndex + 1] << 8) | bytesImage[intIndex + 2];
}
Bitmap bmpImage = Bitmap.createBitmap(intColors, intWidth, intHeight, Bitmap.Config.ARGB_8888);
Community
  • 1
  • 1
Jon
  • 1,398
  • 9
  • 14