8

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.

zelf
  • 93
  • 1
  • 1
  • 7
  • `it is sent as ByteBuffer`. Don't think so. It's sent as a stream of bytes. – greenapps Jul 28 '16 at 15:12
  • `DecodeData.mReceivingBuffer`. You did not show how you received the data. Very incomplete code. Please show a hexadecimal notation of the received bytes. And also the sent bytes hexadecimal please. – greenapps Jul 28 '16 at 15:14

2 Answers2

15

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;
}
Asaf Pinhassi
  • 15,177
  • 12
  • 106
  • 130
9
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.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Andolasoft Inc
  • 1,296
  • 1
  • 7
  • 16
  • 1
    Just code without any further explanation isn't very helpful. – Robert Jul 28 '16 at 14:55
  • it is not working using buf.get(imageBytes); do not fill imagebytes with data I think imageBytes = receivedData.array(); is better and now it is decoding but also can not display, but thanks – zelf Jul 28 '16 at 15:01
  • @Andolasoft thanks for your help I think first solution with changing buf.get(imageBytes); to buf.array will work, as buf.get() writing nothing inside imageBytes in the two solutions. – zelf Jul 28 '16 at 15:15
  • Thanks! `BitmapFactory.decodeByteArray(buf, 0, buf.length)` was really useful! – Trake Vital Mar 29 '21 at 08:45