android - Android中如何将ByteBuffer转为图片

标签 android image bitmap bytearray bytebuffer

我通过套接字接收 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/

相关文章:

java - HttpURLConnection getInputStream() 有时包含响应头

android - 无法使用 API 28 解析符号 'TabLayout'

android - 具有一行而不是两行文本的线性布局中按钮的奇怪行为

android - 为什么我创建的位图总是泄漏内存?

android - 即使在使用高效加载大位图指南后如何避免 OutOfMemoryError

android - SurfaceView 到位图

android - 如何在特定时间内填充进度条?

java - Android 无法更改 ImageView

javascript - 单击时转到新样式页面的图像页面

javascript - 高度使我的图像上出现了损坏的图像链接