Method for Android to obtain the first frame of video as thumbnail

Today, let's talk about how to get the first frame of a video file in Android and display it on the interface as a thumbnail.

Previously, I said that I need to download video files from the server recently, but after downloading, I certainly need to display the thumbnail of the video to the user on the interface, so I thought of displaying the first frame of the video as the thumbnail. But I didn't know how to write it, so I searched for information on the Internet and finally solved the problem. Record it here.

1、 Use the mediametadataretriever to obtain the first frame of the video as a thumbnail

/**
* 获取视频文件截图
*
* @param path 视频文件的路径
* @return Bitmap 返回获取的Bitmap
*/

public static Bitmap getVideoThumb(String path) { 

MediaMetadataRetriever media = new MediaMetadataRetriever(); 

media.setDataSource(path); 

return media.getFrameAtTime(); 

}

It should be noted here that a new type of mediametadataretriever is added from API 10, which can be used to obtain the information of media files and the thumbnail of any frame of video. Therefore, the minimum API for using mediametadataretriever is 10

Here are some other functions of mediametadataretriever to obtain video:

//获取第一帧原尺寸图片
mmrc.getFrameAtTime();

//获取指定位置的原尺寸图片 注意这里传的timeUs是微秒
mmrc.getFrameAtTime(timeUs,option);

//获取指定位置指定宽高的缩略图
mmrc.getScaledFrameAtTime(timeUs,MediaMetadataRetrieverCompat.OPTION_CLOSEST,width,height);

//获取指定位置指定宽高并且旋转的缩略图
mmrc.getScaledFrameAtTime(timeUs,height,rotate);

Here's a brief introduction. Media. Getframeattime () actually calls MMRC. Getframeattime (- 1, option_close_sync); That is, the key frame at the nearest position from - 1 second is actually the first frame data. Let's briefly talk about public bitmap getframeattime (long timeus, int option):

The first parameter of public bitmap getframeattime (long timeus, int option) is the incoming time, which can only be us (microseconds). At that time, the MS I passed in always obtained the first frame, so this problem has been solved for a long time.

Then there is the second parameter. First look at the official explanation:

OPTION_ Close retrieves the latest frame at a given time. This frame is not necessarily a key frame.

OPTION_ CLOSEST_ Sync retrieves the latest synchronization frame (key frame) associated with the data source at a given time.

OPTION_ NEXT_ Sync retrieves a key frame associated with the data source after a given time.

OPTION_ PREVIOUS_ Sync, as the name suggests, is the same as above

2、 Use thumbnailutils to get the first frame of the video as a thumbnail

/**
   * 获取视频的缩略图
   * 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
   * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
   * @param videoPath 视频的路径
   * @param width 指定输出视频缩略图的宽度
   * @param height 指定输出视频缩略图的高度度
   * @param kind 参照MediaStore.Images(Video).Thumbnails类中的常量MINI_KIND和MICRO_KIND。
   *      其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
   * @return 指定大小的视频缩略图
   */
  public static Bitmap getVideoThumbnail(String videoPath,int width,int height,int kind) {
    Bitmap bitmap = null;
    // 获取视频的缩略图
    bitmap = ThumbnailUtils.createVideoThumbnail(videoPath,kind); //調用ThumbnailUtils類的靜態方法createVideoThumbnail獲取視頻的截圖;
    if(bitmap!= null){
      bitmap = ThumbnailUtils.extractThumbnail(bitmap,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);//調用ThumbnailUtils類的靜態方法extractThumbnail將原圖片(即上方截取的圖片)轉化為指定大小;
    }
    return bitmap;
  }

To be brief, this is actually very simple, that is, call thumbnails.createvideothumbnail (path, kind) to obtain the first frame data, and then use bitmap = thumbnails.extractthumbnail (bitmap, thumbnails. Options_recycle_input); Converts the picture to the specified size.

The following also lists how to save the bitmap to a file, because after obtaining the thumbnail of the video, it may need to be saved locally. You can view it directly the next time you enter the app.

/**
* Bitmap保存成File
*
* @param bitmap input bitmap
* @param name output file's name
* @return String output file's path
*/

public static String bitmap2File(Bitmap bitmap,String name) { 

File f = new File(Environment.getExternalStorageDirectory() + name + ".jpg"); 

if (f.exists()) f.delete(); 

FileOutputStream fOut = null; 

try { 

fOut = new FileOutputStream(f); 

bitmap.compress(Bitmap.CompressFormat.JPEG,100,fOut); 

fOut.flush(); 

fOut.close(); 

} catch (IOException e) { 

return null; 

} 

return f.getAbsolutePath(); 

}

Android gets the first frame of the video as a thumbnail.

It's that simple. The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>