我通过套接字接收 jpg 图像,它作为 ByteBuffer 发送 我正在做的是:
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);
但是发生了什么,它无法解码 imageBytes 并且位图为空。
我还得到了 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 更多
会出现什么问题? 是解码问题吗? 或者从 ByteBuffer 到 Byte 数组的转换?
在此先感谢您的帮助。
最佳答案
这个对我有用(对于 ARGB_8888 像素缓冲区):
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;
}
关于android - Android中如何将ByteBuffer转为图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38639436/