如何在Android中将ByteBuffer转换为图像 [英] How to convert ByteBuffer into image in Android

查看:443
本文介绍了如何在Android中将ByteBuffer转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过套接字接收jpg图像,并将其作为ByteBuffer发送 我正在做的是:

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);

但是发生的事情是无法解码imageBytes并且位图为空.

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

我也得到了imagebytes作为: imageBytes:{-1,-40,-1,-32、0、16、74、70、73、70、0、1、1、1、0、96、0、0、0,-1, -37,0,40,28,30,35,+10,478更多}

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}

会是什么问题? 是解码问题吗? 还是从ByteBuffer转换为Byte数组?

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

预先感谢您的帮助.

推荐答案

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

// 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,并将其转换为位图并将其设置为IMAGEVIEW.

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.

这篇关于如何在Android中将ByteBuffer转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆