我通过套接字接收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视为: 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数组?
提前感谢您的帮助。
答案 0 :(得分:4)
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);

使用任何一个将BYTEBUFFER转换为BYTE ARRAY并将其转换为BITMAP并将其设置为您的IMAGEVIEW。
希望这会对你有所帮助。
答案 1 :(得分:4)
这个适用于我(对于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;
}