Android: Convert image object to bitmap does not work

后端 未结 2 657
感动是毒
感动是毒 2021-01-14 01:17

I am trying to convert image object to bitmap, but it return null.

image = reader.acquireLatestImage();

                        ByteBuffer buffer = image.ge         


        
相关标签:
2条回答
  • 2021-01-14 01:45

    You haven't copied bytes.You checked capacity but not copied bytes.

    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
    byte[] bytes = new byte[buffer.remaining()];
    buffer.get(bytes);
    Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
    
    0 讨论(0)
  • 2021-01-14 01:56

    I think you're trying to decode an empty array, you just create it but never copy the image data to it.

    You can try:

    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
    byte[] bytes = buffer.array();
    Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
    

    As you say it doesn't work, then we need to copy the buffer manually ... try this :)

        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        Bitmap myBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,null);
    
    0 讨论(0)
提交回复
热议问题