简体   繁体   English

如何在videoView android中截取当前视频帧的屏幕截图?

[英]How to take the screen-shot of the current video frame in the videoView android?

I am using the video-view to play media and I want to take the screen shot of the current frame.我正在使用视频视图播放媒体,我想拍摄当前帧的屏幕截图。 I found some solutions which include using mediaMetaDataRetriever but that method is way too slow and does not perform well in many cases.我找到了一些解决方案,其中包括使用 mediaMetaDataRetriever,但该方法太慢,并且在许多情况下表现不佳。 I can not use media player over texture view to capture view I knew that approach, because I need by default media controls in my app.我不能在纹理视图上使用媒体播放器来捕获我知道这种方法的视图,因为我的应用程序中默认需要媒体控件。 Is there any way to make that process fast?有什么方法可以使这个过程快速吗? My CODE:我的代码:

 public Bitmap getBitmapVideoView(){
        MediaMetadataRetriever mediaMetadataRetriever;
        mediaMetadataRetriever = new MediaMetadataRetriever();
        try {
            int currentPosition =videoView.getCurrentPosition();
            mediaMetadataRetriever.setDataSource(getVideoURL(),new HashMap<String, String>());
            Bitmap bitmap = mediaMetadataRetriever.getFrameAtTime(currentPosition*1000);
            if (bitmap != null) {
//                Canvas canvas = new Canvas(bitmap);
//                textureview.draw(canvas);
                return bitmap;
            }
            else{
                return null;
            }
        }
        catch (Exception e){
            Log.i("Errorrr",e.getMessage());
            return null;
        }
        finally {
            try {
                mediaMetadataRetriever.release();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }

Approach:方法:

Bitmap bitmap = Screenshot.INSTANCE.with((MainActivity)activity).setView(findViewById(R.id.relative)).setQuality(Quality.HIGH).getScreenshot();
                    LayoutInflater inflater = getLayoutInflater();
                    Toast toastShot = null;
                    View layout = inflater.inflate(R.layout.layout, (ViewGroup) findViewById(R.id.screen));
                    ImageView image = (ImageView) layout.findViewById(R.id.shot_image);
                    image.setImageBitmap(bitmap);
                    toastShot = new Toast(getApplicationContext());
                    toastShot.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                    toastShot.setDuration(Toast.LENGTH_LONG);
                    toastShot.setView(layout);
                    toastShot.show();

For taking screenshot you can use this library要截取屏幕截图,您可以使用此库

implementation 'com.github.MindorksOpenSource:screenshot:v0.0.1'

And for taking screenshot just call this function on button click or any other event对于截屏,只需在单击按钮或任何其他事件时调用此函数

 var b = Screenshot.with(activity!!).setView(rl_imageText).setQuality(Quality.HIGH).getScreenshot()

Note: rl_imageText = This is the id of my xml relative layout of which i am taking screenshot.注意:rl_imageText = 这是我正在截屏的 xml 相对布局的 id。

It will provide you the bitmap of the screenshot and for saving it into the storage or getting path use the below mentioned function它将为您提供屏幕截图的位图,并使用下面提到的功能将其保存到存储中或获取路径

private fun getImageUriFromBitmap(context: Context, bitmap: Bitmap): Uri {
    val bytes = ByteArrayOutputStream()
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
    val path = MediaStore.Images.Media.insertImage(context.contentResolver, bitmap,"Title",null)
    return Uri.parse(path.toString())
}

Note: Please mention if it will not work so that i will provide you other solution.注意:请说明它是否不起作用,以便我为您提供其他解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM