How to capture screenshot or video frame of VideoView in Android

后端 未结 2 693
情歌与酒
情歌与酒 2020-12-21 15:29

I\'ve tried the following tutorial from this blog ( http://android-er.blogspot.kr/2013/05/get-current-frame-in-videoview-using.html ), which shows how to capture a video fra

2条回答
  •  情歌与酒
    2020-12-21 15:58

    Video View is a Subclass of SurfaceView so Pixel Copy can take a screenshot of video View as well

    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)
            }
    

提交回复
热议问题