将ByteBuffer转换为Image会产生嘈杂的输出

时间:2019-12-11 11:01:54

标签: java android bitmap android-bitmap bytebuffer

我正在尝试将ByteBuffer转换为Bitmap Image,但是得到的输出却很嘈杂,即不是我所期望的。我的代码如下:

private Bitmap getOutputImage(ByteBuffer output){
        output.rewind();

        int outputWidth = 384;
        int outputHeight = 384;
        Bitmap bitmap = Bitmap.createBitmap(outputWidth, outputHeight, Bitmap.Config.RGB_565);
        int [] pixels = new int[outputWidth * outputHeight];
        for (int i = 0; i < outputWidth * outputHeight; i++) {
            //val a = 0xFF;
            //float a = (float) 0xFF;

            //val r: Float = output?.float!! * 255.0f;
            //byte val = output.get();
            float r = ((float) output.get()) * 255.0f;

            //val g: Float = output?.float!! * 255.0f;
            float g = ((float) output.get()) * 255.0f;

            //val b: Float = output?.float!! * 255.0f;
            float b = ((float) output.get()) * 255.0f;


            //pixels[i] = a shl 24 or (r.toInt() shl 16) or (g.toInt() shl 8) or b.toInt()
            pixels[i] = (((int) r) << 16) | (((int) g) << 8) | ((int) b);
        }
        bitmap.setPixels(pixels, 0, outputWidth, 0, 0, outputWidth, outputHeight);

        return bitmap;
    }

我得到的图像是

enter image description here

请告诉我这是怎么了?

0 个答案:

没有答案