获取 byte[] 数据显示在 surfaceview 上

    private var mCanvas: Canvas? = null
    val videoBitmap: Bitmap = Bitmap.createBitmap(800, 600, Bitmap.Config.ARGB_8888)

    fun initVideo() {
        GlobalScope.launch {
            delay(1000)
            mainViewModel.requestImage(ComputerStrict.OnRecieveImageListener { data ->
                mCanvas= mBinding.sfv.holder.lockCanvas()
                mCanvas!!.drawColor(Color.BLACK)
                val buffer = ByteBuffer.wrap(data)
                videoBitmap.copyPixelsFromBuffer(buffer)

                // 计算宽高缩放率,用宽填充满
                val matrix = Matrix()
                val scaleWidth = mBinding.sfv.width.toFloat() / 800
//            val scaleHeight = mBinding.sfv.height.toFloat() / 600
                matrix.postScale(scaleWidth, scaleWidth)
                mCanvas!!.drawBitmap(videoBitmap, matrix, null)
                if (mCanvas!= null) {
                    mBinding.sfv.holder.unlockCanvasAndPost(m_canvas)
                }
            })
        }
    }

你可能感兴趣的:(获取 byte[] 数据显示在 surfaceview 上)