Displaying YUV Image in Android

前端 未结 4 1624
不知归路
不知归路 2020-11-29 01:07

In my application, we need to display the Video frame receives from server to our android application,
Server is sending video data @ 50 frame per second, having encoded

4条回答
  •  囚心锁ツ
    2020-11-29 01:15

    Another way would be using ScriptIntrinsicYuvToRGB, this is more efficient then encoding (and decoding) each time a JPEG

    fun yuvByteArrayToBitmap(bytes: ByteArray, width: Int, height: Int): Bitmap {
        val rs = RenderScript.create(this)
    
        val yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
        val yuvType = Type.Builder(rs, Element.U8(rs)).setX(bytes.size);
        val input = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
    
        val rgbaType = Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
        val output = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
    
        input.copyFrom(bytes);
    
        yuvToRgbIntrinsic.setInput(input);
        yuvToRgbIntrinsic.forEach(output);
    
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        output.copyTo(bitmap)
    
        input.destroy()
        output.destroy()
        yuvToRgbIntrinsic.destroy()
        rs.destroy()
    
        return bitmap
    }
    

提交回复
热议问题