Take Screenshot of SurfaceView

前端 未结 6 2096
既然无缘
既然无缘 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:08

    This is how I do it. Put this method in some Util class

    /**
     * Pixel copy to copy SurfaceView/VideoView into BitMap
     */
     fun usePixelCopy(videoView: SurfaceView,  callback: (Bitmap?) -> Unit) {
        val bitmap: Bitmap = Bitmap.createBitmap(
            videoView.width,
            videoView.height,
            Bitmap.Config.ARGB_8888
        );
        try {
        // Create a handler thread to offload the processing of the image.
        val handlerThread = HandlerThread("PixelCopier");
        handlerThread.start();
        PixelCopy.request(
            videoView, bitmap,
            PixelCopy.OnPixelCopyFinishedListener { copyResult ->
                if (copyResult == PixelCopy.SUCCESS) {
                    callback(bitmap)
                }
                handlerThread.quitSafely();
            },
            Handler(handlerThread.looper)
        )
        } catch (e: IllegalArgumentException) {
            callback(null)
            // PixelCopy may throw IllegalArgumentException, make sure to handle it
            e.printStackTrace()
        }
    }
    

    Usage:

    usePixelCopy(videoView) { bitmap: Bitmap? ->
                processBitMp(bitmap)
            }
    

    Note: Video View is a Subclass of SurfaceView so this method can take screenshot of video View as well

提交回复
热议问题