Android video processing and android video editing
A class has been added since API 8:
Android. media. ThumbnailUtils provides three static methods: one to obtain the Bitmap obtained from the first frame of the video, and the other two to scale down the image.
public static Bitmap createVideoThumbnail (String filePath, int kind)
The first parameter is the video file path, and the second parameter is the image size. Two options are available: Thumbnails. MINI_KIND and Thumbnails. MICRO_KIND.
- In the first document, the size is 512x384. I tested 544x960 using an MP4 file and 160x120 using a wmv file. Obviously unreliable.
- In the second format, the file size is 96x96, which is the thumbnail.
extractThumbnail(Bitmap source, int width, int height, int options)extractThumbnail(Bitmap source, int width, int height)
Both methods are used to process the Bitmap size. The first parameter is the Bitmap to be processed, the second parameter is the width after processing, the third parameter is the height, and the fourth parameter is options, if options is defined as OPTIONS_RECYCLE_INPUT, resources are recycled. That is to say, you can use the third method to convert the Bitmap of the first frame of the captured video to any desired size. The third method can also obtain the thumbnail of the image in the memory card.
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path1, Thumbnails.MINI_KIND); bitmap = ThumbnailUtils.extractThumbnail(bitmap, 210, 210);
A MediaMetadataRetriever class is added from API 10 to obtain media file information.
MediaMetadataRetriever mmr = 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 be used to obtain thumbnails of any frame of a video.
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; }
Other highlights
Android learning notes (41) android option menu and SubMenu android learning notes (40) Functions and usage of Notification android learning notes (42) android uses listeners to listen to menu events android learning notes (43) android creates single-choice menus and check menus android learning notes (44) android sets Activityandroid learning notes associated with menu items (45) android context menu