Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ByteBuffer into image in Android

I am receiving jpg image through socket and it is sent as ByteBuffer what I am doing is:

        ByteBuffer receivedData ;
        // Image bytes
        byte[] imageBytes = new byte[0];
        // fill in received data buffer with data
        receivedData=  DecodeData.mReceivingBuffer;
        // Convert ByteByffer into bytes
        imageBytes = receivedData.array();
        //////////////
        // Show image
        //////////////
        final Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
        showImage(bitmap1);

But what is happening that it fails to decode the imageBytes and bitmap is null.

Also I got imagebytes as: imageBytes: {-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 96, 0, 0, 0, 0, -1, -37, 0, 40, 28, 30, 35, +10,478 more}

What would be the problem? is it decoding problem? or conversion from ByteBuffer to Byte array?

Thanks in advance for help.

like image 334
zelf Avatar asked Jul 28 '16 14:07

zelf


People also ask

How do I get data from ByteBuffer?

In order to get the byte array from ByteBuffer just call the ByteBuffer. array() method. This method will return the backed array. Now you can call the String constructor which accepts a byte array and character encoding to create String.

How do I get strings from ByteBuffer?

The toString() method of ByteBuffer class is the inbuilt method used to returns a string representing the data contained by ByteBuffer Object. A new String object is created and initialized to get the character sequence from this ByteBuffer object and then String is returned by toString().

What is ByteBuffer used for?

A ByteBuffer is a buffer which provides for transferring bytes from a source to a destination. In addition to storage like a buffer array, it also provides abstractions such as current position, limit, capacity, etc. A FileChannel is used for transferring data to and from a file to a ByteBuffer.

What is ByteBuffer in Android Studio?

ByteBuffer is among several buffers provided by Java NIO. Its just a container or holding tank to read data from or write data to. Above behavior is achieved by allocating a direct buffer using allocateDirect() API on Buffer.


2 Answers

This one worked for me (for ARGB_8888 pixel buffer):

private Bitmap getBitmap(Buffer buffer, int width, int height) {
    buffer.rewind();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
}
like image 70
Asaf Pinhassi Avatar answered Oct 19 '22 15:10

Asaf Pinhassi


ByteBuffer buf = DecodeData.mReceivingBuffer;
byte[] imageBytes= new byte[buf.remaining()];
buf.get(imageBytes);
final Bitmap bmp=BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length);
    showImage(bmp);

OR

// Create a byte array
byte[] bytes = new byte[10];

// Wrap a byte array into a buffer
ByteBuffer buf = ByteBuffer.wrap(bytes);

// Retrieve bytes between the position and limit
// (see Putting Bytes into a ByteBuffer)
bytes = new byte[buf.remaining()];

// transfer bytes from this buffer into the given destination array
buf.get(bytes, 0, bytes.length);

// Retrieve all bytes in the buffer
buf.clear();
bytes = new byte[buf.capacity()];

// transfer bytes from this buffer into the given destination array
buf.get(bytes, 0, bytes.length);

final Bitmap bmp=BitmapFactory.decodeByteArray(bytes,0,bytes.length); showImage(bmp);

USE ANY ONE ABOVE TO CONVERT BYTEBUFFER TO BYTE ARRAY AND CONVERT IT TO BITMAP AND SET IT INTO YOUR IMAGEVIEW.

Hope this will help you.

like image 8
Andolasoft Avatar answered Oct 19 '22 15:10

Andolasoft