使用ThumbnailUtils类获取视频的缩略图
来源:互联网 发布:js正则过滤表情符号 编辑:程序博客网 时间:2024/05/19 23:27
转载出处:http://blog.csdn.net/ouyang_peng/article/details/16864975
今天看了一段代码,是关于获取视频的缩略图的,让我认识了一个ThumbnailUtils类,代码如下。
- Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MINI_KIND); //用于获取视频的缩略图
- BitmapDrawable background=new BitmapDrawable(bitmap);
通过察看Android.media.ThumbnailUtils的源码,可以发现该类提供了三种静态方法可以直接调用获取,从而可以帮助我们获取系统视频或图片文件的缩略图。
第一个方法可以用来创建一张视频的缩略图,如果视频已损坏或者格式不支持可能返回null
- /**
- * Create a video thumbnail for a video. May return null if the video is
- * corrupt or the format is not supported.
- *
- * @param filePath the path of video file
- * @param kind could be MINI_KIND or MICRO_KIND
- */
- public static Bitmap createVideoThumbnail(String filePath, int kind) {
- Bitmap bitmap = null;
- MediaMetadataRetriever retriever = new MediaMetadataRetriever();
- try {
- retriever.setDataSource(filePath);
- bitmap = retriever.getFrameAtTime(-1);
- } catch (IllegalArgumentException ex) {
- // Assume this is a corrupt video file
- } catch (RuntimeException ex) {
- // Assume this is a corrupt video file.
- } finally {
- try {
- retriever.release();
- } catch (RuntimeException ex) {
- // Ignore failures while cleaning up.
- }
- }
- if (bitmap == null) return null;
- if (kind == Images.Thumbnails.MINI_KIND) {
- // Scale down the bitmap if it's too large.
- int width = bitmap.getWidth();
- int height = bitmap.getHeight();
- int max = Math.max(width, height);
- if (max > 512) {
- float scale = 512f / max;
- int w = Math.round(scale * width);
- int h = Math.round(scale * height);
- bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
- }
- } else if (kind == Images.Thumbnails.MICRO_KIND) {
- bitmap = extractThumbnail(bitmap,
- TARGET_SIZE_MICRO_THUMBNAIL,
- TARGET_SIZE_MICRO_THUMBNAIL,
- OPTIONS_RECYCLE_INPUT);
- }
- return bitmap;
- }
参数说明:
- filePath表示视频文件路径
- kind表示类型,可以有两个选项,分别是Images.Thumbnails.MICRO_KIND和Images.Thumbnails.MINI_KIND,其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96,当然读了代码你会发现,你也可以传入任意的int型数字,只是就不会对获取的bitmap进行相关的设置,我们可以自己使用extractThumbnail( Bitmap source, int width, int height)方法对返回的bitmap进行相关设置。
第二个方法可以用来创建一个指定大小的缩略图
- /**
- * Creates a centered bitmap of the desired size.
- *
- * @param source original bitmap source
- * @param width targeted width
- * @param height targeted height
- */
- public static Bitmap extractThumbnail(
- Bitmap source, int width, int height) {
- return extractThumbnail(source, width, height, OPTIONS_NONE);
- }
参数说明:
- source表示图片源文件(Bitmap类型)
- width表示指定压缩成后的宽度
- height表示指定压缩成后的高度
第三个方法可以用来创建一个指定大小的缩略图
- /**
- * Creates a centered bitmap of the desired size.
- *
- * @param source original bitmap source
- * @param width targeted width
- * @param height targeted height
- * @param options options used during thumbnail extraction
- */
- public static Bitmap extractThumbnail(
- Bitmap source, int width, int height, int options) {
- if (source == null) {
- return null;
- }
- float scale;
- if (source.getWidth() < source.getHeight()) {
- scale = width / (float) source.getWidth();
- } else {
- scale = height / (float) source.getHeight();
- }
- Matrix matrix = new Matrix();
- matrix.setScale(scale, scale);
- Bitmap thumbnail = transform(matrix, source, width, height,
- OPTIONS_SCALE_UP | options);
- return thumbnail;
- }
参数说明:
- source表示图片源文件(Bitmap类型)
- width表示指定压缩成后的宽度
- height表示指定压缩成后的高度
- options表示在缩略图抽取时提供的选项,如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件(除非缩略图等于@param source)
=====================华丽丽的分界线========================
下面展示两个封装好的代码,用来获取图像和视频的缩略图
- /**
- * 根据指定的图像路径和大小来获取缩略图
- * 此方法有两点好处:
- * 1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,
- * 第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。
- * 2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使
- * 用这个工具生成的图像不会被拉伸。
- * @param imagePath 图像的路径
- * @param width 指定输出图像的宽度
- * @param height 指定输出图像的高度
- * @return 生成的缩略图
- */
- private Bitmap getImageThumbnail(String imagePath, int width, int height) {
- Bitmap bitmap = null;
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- // 获取这个图片的宽和高,注意此处的bitmap为null
- bitmap = BitmapFactory.decodeFile(imagePath, options);
- options.inJustDecodeBounds = false; // 设为 false
- // 计算缩放比
- int h = options.outHeight;
- int w = options.outWidth;
- int beWidth = w / width;
- int beHeight = h / height;
- int be = 1;
- if (beWidth < beHeight) {
- be = beWidth;
- } else {
- be = beHeight;
- }
- if (be <= 0) {
- be = 1;
- }
- options.inSampleSize = be;
- // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
- bitmap = BitmapFactory.decodeFile(imagePath, options);
- // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
- bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
- ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
- return bitmap;
- }
- /**
- * 获取视频的缩略图
- * 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。
- * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。
- * @param videoPath 视频的路径
- * @param width 指定输出视频缩略图的宽度
- * @param height 指定输出视频缩略图的高度度
- * @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。
- * 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96
- * @return 指定大小的视频缩略图
- */
- private Bitmap getVideoThumbnail(String videoPath, int width, int height,
- int kind) {
- Bitmap bitmap = null;
- // 获取视频的缩略图
- bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
- System.out.println("w"+bitmap.getWidth());
- System.out.println("h"+bitmap.getHeight());
- bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
- ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
- return bitmap;
- }
0 0
- 使用ThumbnailUtils类获取视频的缩略图
- 使用ThumbnailUtils类获取视频的缩略图
- Android 使用ThumbnailUtils类获取视频的缩略图
- android 使用ThumbnailUtils类获取图片、视频的缩略图
- 使用ThumbnailUtils类回获取视频第一帧缩略图
- 我的Android进阶之旅------>使用ThumbnailUtils类获取视频的缩略图
- 实现获取视频的缩略图(ThumbnailUtils),并且播放
- 实现获取视频的缩略图(ThumbnailUtils),并且播放
- 实现获取视频的缩略图(ThumbnailUtils),并且播放 .
- Android之使用ThumbnailUtils类来获取视频第一帧缩略图
- 获取图片和视频缩略图Thumbnails ThumbnailUtils
- Android:通过ThumbnailUtils类获取视频文件的缩略图
- Android ThumbnailUtils 视频缩略图
- [Android算法] 实现获取视频的缩略图(ThumbnailUtils),并且播放
- ThumbnailUtils:用于实现缩略图的工具类
- 缩略图工具类ThumbnailUtils
- 缩略图ThumbnailUtils类
- Android2.2的缩略图ThumbnailUtils
- 性能测试新利器,HyperPacer
- 在Android 中使用KSOAP2调用WebService
- Swift中assert(断言)的使用
- iOS开发中的推送通知的讲解
- 作业:C++作业4
- 使用ThumbnailUtils类获取视频的缩略图
- Shell 学习笔记
- MacOS登陆win10 时出现”远程桌面无法验证您希望连接的计算机的身份"解决方法
- OpenCV之ROI图像叠加和混合
- dll教程1:如何编写dll,一步一步,全部可行
- 阿里云ecs云服务器安装wdcp控制面板教程(推荐CentOS6.5)
- ASP.NET mvc 在视图中使用@helper封装输出代码
- kafka基本命令
- iOS: 在键盘之上显示一个 View