Take Screenshot of SurfaceView

前端 未结 6 2097
既然无缘
既然无缘 2020-11-27 17:32

Am developing a simple camera app. I have code that takes screenshot of the whole activity and writes it to the Sd card. the problem is that the Surfaceview returns a black

6条回答
  •  无人及你
    2020-11-27 18:02

    My situation related with ExoPlayer, need get bitmap of current frame.

    Work on API >= 24.

    private val copyFrameHandler = Handler()
    
    fun getFrameBitmap(callback: FrameBitmapCallback) {
        when(val view = videoSurfaceView) {
            is TextureView -> callback.onResult(view.bitmap)
            is SurfaceView -> {
                val bitmap = Bitmap.createBitmap(
                        videoSurfaceView.getWidth(),
                        videoSurfaceView.getHeight(),
                        Bitmap.Config.ARGB_8888
                )
    
                copyFrameHandler.removeCallbacksAndMessages(null)
    
                PixelCopy.request(view, bitmap, { copyResult: Int ->
                    if (copyResult == PixelCopy.SUCCESS) {
                        callback.onResult(bitmap)
                    } else {
                        callback.onResult(null)
                    }
                }, copyFrameHandler)
            }
            else -> callback.onResult(null)
        }
    }
    
    fun onDestroy() {
        copyFrameHandler.removeCallbacksAndMessages(null)
    }
    
    interface FrameBitmapCallback {
        fun onResult(bitmap: Bitmap?)
    }
    

提交回复
热议问题