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.

 Public Static int kind)

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.
int int int int int height)

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.

Bitmap Bitmap = Thumbnailutils.createvideothumbnail (path1, thumbnails.mini_kind);  Bitmap = Thumbnailutils.extractthumbnail (Bitmap, 210, 210);

A new class of mediametadataretriever can be used to get media files from API

New Mediametadataretriever ();  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.

 Public StaticBitmap Createvideothumbnail (String filePath) {//Mediametadataretriever is available on API level 8//But was hidden until API levelClass<?> Clazz =NULL; Object instance =NULL;Try{clazz = Class.forName ("Android.media.MediaMetadataRetriever");            Instance = Clazz.newinstance (); 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)returnBitmap }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; }

Other wonderful articles

Android Learning Notes ($) Android Options Menu and submenu (submenu) Android Learning note (notification) features and usage of Android learning Note (Android) using listeners to listen to menu events Android Learning notes (+) Android Create a single-selection menu and a check menuAndroid Learning Notes (Android) setting activity associated with menu itemsAndroid Learning Notes (Android context menu)

more about Android Development articles

Android Video Processing-process video first frame thumbnail

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.