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 |
public static Bitmap createVideoThumbnail (String filePath, 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.
?
12 |
extractThumbnail(Bitmap source, int width, int height, int options) extractThumbnail(Bitmap source, int width, 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.
?
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 = 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.
?
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