Android Video Processing-process video first frame thumbnail

Source: Internet
Author: User

Starting with API 8, a new class has been added:

Android.media.ThumbnailUtils This class provides 3 static methods for getting the first frame of the video to get the bitmap,2 of the thumbnail processing of the image.

?
1 publicstatic Bitmap createVideoThumbnail (String filePath, intkind)

The first parameter is the path to the video file, the second parameter is the size of the specified picture, and there are two options Thumbnails.mini_kind and Thumbnails.micro_kind.

The first document says size is x 384, I test it with a MP4 format file to get 544 x 960, and test it with a WMV format file for x 120. Obviously not reliable. The second parameter is the size of the two format files is 96, this is the thumbnail.

?
12 extractThumbnail(Bitmap source, intwidth, int height, int options)extractThumbnail(Bitmap source, int width, intheight)

Both methods are used to handle the size of the bitmap, the first parameter is the bitmap to be processed, the second parameter is the processing width, the third is the height, the fourth parameter options, and if the options are defined as Options_recycle_input, the resource is recycled. In other words, you can use the third method to bitmap the first frame of the captured video into any desired size, and the third method can also get a thumbnail image of the memory card.

?
12 Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path1, Thumbnails.MINI_KIND);  bitmap = ThumbnailUtils.extractThumbnail(bitmap, 210, 210);

Starting with API 10, a new class of mediametadataretriever can be used to get information about media files.

?
1234567 MediaMetadataRetriever mmr = newMediaMetadataRetriever();  mmr.setDataSource(/sdcard/33.mp4);  Bitmap bitmap = mmr.getFrameAtTime();  image.setImageBitmap(bitmap);  System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE)+);  System.out.println(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE)+);  mmr.release();

Mediametadataretriever can get thumbnails of any frame in the video.

?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 public static Bitmap createVideoThumbnail(String filePath) {      // MediaMetadataRetriever is available on API Level 8      // but is hidden until API Level 10      Class<!--?--> clazz = null    Object instance = null    try         clazz = Class.forName(android.media.MediaMetadataRetriever);          instance = clazz.newInstance();            Method method = clazz.getMethod(setDataSource, String.class);          method.invoke(instance, filePath);            // The method name changes between API Level 9 and 10.          if (Build.VERSION.SDK_INT <= 9) {              return (Bitmap) clazz.getMethod(captureFrame).invoke(instance);          } else             byte[] data = (byte[]) clazz.getMethod(getEmbeddedPicture).invoke(instance);              if (data != null) {                  Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);                  if (bitmap != null) return bitmap;                          return (Bitmap) clazz.getMethod(getFrameAtTime).invoke(instance);              } catch (IllegalArgumentException ex) {          // Assume this is a corrupt video file      } catch (RuntimeException ex) {          // Assume this is a corrupt video file.      } catch (InstantiationException e) {          Log.e(TAG, createVideoThumbnail, e);      } catch (InvocationTargetException e) {          Log.e(TAG, createVideoThumbnail, e);      } catch (ClassNotFoundException e) {          Log.e(TAG, createVideoThumbnail, e);      } catch (NoSuchMethodException e) {          Log.e(TAG, createVideoThumbnail, e);      } catch (IllegalAccessException e) {          Log.e(TAG, createVideoThumbnail, e);      } finally         try             if (instance != null) {                  clazz.getMethod(release).invoke(instance);                      } catch (Exception ignored) {                  return null}

Push family, free tickets, scenic spots: www.tuituizu.com

A companion tour, a free dating site: www.jieberu.com

Android Video Processing-process video first frame thumbnail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.