使用ThumbnailUtils类获取视频的缩略图

来源:互联网 发布:js正则过滤表情符号 编辑:程序博客网 时间:2024/05/19 23:27

转载出处:http://blog.csdn.net/ouyang_peng/article/details/16864975


今天看了一段代码,是关于获取视频的缩略图的,让我认识了一个ThumbnailUtils类,代码如下。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MINI_KIND); //用于获取视频的缩略图  
  2.   
  3. BitmapDrawable background=new BitmapDrawable(bitmap);  


通过察看Android.media.ThumbnailUtils的源码,可以发现该类提供了三种静态方法可以直接调用获取,从而可以帮助我们获取系统视频或图片文件的缩略图。


第一个方法可以用来创建一张视频的缩略图,如果视频已损坏或者格式不支持可能返回null

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Create a video thumbnail for a video. May return null if the video is 
  3.  * corrupt or the format is not supported. 
  4.  * 
  5.  * @param filePath the path of video file 
  6.  * @param kind could be MINI_KIND or MICRO_KIND 
  7.  */  
  8. public static Bitmap createVideoThumbnail(String filePath, int kind) {  
  9.     Bitmap bitmap = null;  
  10.     MediaMetadataRetriever retriever = new MediaMetadataRetriever();  
  11.     try {  
  12.         retriever.setDataSource(filePath);  
  13.         bitmap = retriever.getFrameAtTime(-1);  
  14.     } catch (IllegalArgumentException ex) {  
  15.         // Assume this is a corrupt video file  
  16.     } catch (RuntimeException ex) {  
  17.         // Assume this is a corrupt video file.  
  18.     } finally {  
  19.         try {  
  20.             retriever.release();  
  21.         } catch (RuntimeException ex) {  
  22.             // Ignore failures while cleaning up.  
  23.         }  
  24.     }  
  25.   
  26.     if (bitmap == nullreturn null;  
  27.   
  28.     if (kind == Images.Thumbnails.MINI_KIND) {  
  29.         // Scale down the bitmap if it's too large.  
  30.         int width = bitmap.getWidth();  
  31.         int height = bitmap.getHeight();  
  32.         int max = Math.max(width, height);  
  33.         if (max > 512) {  
  34.             float scale = 512f / max;  
  35.             int w = Math.round(scale * width);  
  36.             int h = Math.round(scale * height);  
  37.             bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);  
  38.         }  
  39.     } else if (kind == Images.Thumbnails.MICRO_KIND) {  
  40.         bitmap = extractThumbnail(bitmap,  
  41.                 TARGET_SIZE_MICRO_THUMBNAIL,  
  42.                 TARGET_SIZE_MICRO_THUMBNAIL,  
  43.                 OPTIONS_RECYCLE_INPUT);  
  44.     }  
  45.     return bitmap;  
  46. }  


参数说明:

  • 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进行相关设置。


第二个方法可以用来创建一个指定大小的缩略图


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Creates a centered bitmap of the desired size. 
  3.  * 
  4.  * @param source original bitmap source 
  5.  * @param width targeted width 
  6.  * @param height targeted height 
  7.  */  
  8. public static Bitmap extractThumbnail(  
  9.         Bitmap source, int width, int height) {  
  10.     return extractThumbnail(source, width, height, OPTIONS_NONE);  
  11. }  

参数说明:

  • source表示图片源文件(Bitmap类型)
  • width表示指定压缩成后的宽度
  • height表示指定压缩成后的高度


第三个方法可以用来创建一个指定大小的缩略图

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * Creates a centered bitmap of the desired size. 
  3.      * 
  4.      * @param source original bitmap source 
  5.      * @param width targeted width 
  6.      * @param height targeted height 
  7.      * @param options options used during thumbnail extraction 
  8.      */  
  9.     public static Bitmap extractThumbnail(  
  10.             Bitmap source, int width, int height, int options) {  
  11.         if (source == null) {  
  12.             return null;  
  13.         }  
  14.   
  15.         float scale;  
  16.         if (source.getWidth() < source.getHeight()) {  
  17.             scale = width / (float) source.getWidth();  
  18.         } else {  
  19.             scale = height / (float) source.getHeight();  
  20.         }  
  21.         Matrix matrix = new Matrix();  
  22.         matrix.setScale(scale, scale);  
  23.         Bitmap thumbnail = transform(matrix, source, width, height,  
  24.                 OPTIONS_SCALE_UP | options);  
  25.         return thumbnail;  
  26.     }  




参数说明:
  • source表示图片源文件(Bitmap类型)
  • width表示指定压缩成后的宽度
  • height表示指定压缩成后的高度
  • options表示在缩略图抽取时提供的选项,如果options定义为OPTIONS_RECYCLE_INPUT,则回收@param source这个资源文件(除非缩略图等于@param source)

=====================华丽丽的分界线========================


下面展示两个封装好的代码,用来获取图像和视频的缩略图

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /**  
  2.      * 根据指定的图像路径和大小来获取缩略图  
  3.      * 此方法有两点好处:  
  4.      *     1. 使用较小的内存空间,第一次获取的bitmap实际上为null,只是为了读取宽度和高度,  
  5.      *        第二次读取的bitmap是根据比例压缩过的图像,第三次读取的bitmap是所要的缩略图。  
  6.      *     2. 缩略图对于原图像来讲没有拉伸,这里使用了2.2版本的新工具ThumbnailUtils,使  
  7.      *        用这个工具生成的图像不会被拉伸。  
  8.      * @param imagePath 图像的路径  
  9.      * @param width 指定输出图像的宽度  
  10.      * @param height 指定输出图像的高度  
  11.      * @return 生成的缩略图  
  12.      */    
  13.     private Bitmap getImageThumbnail(String imagePath, int width, int height) {    
  14.         Bitmap bitmap = null;    
  15.         BitmapFactory.Options options = new BitmapFactory.Options();    
  16.         options.inJustDecodeBounds = true;    
  17.         // 获取这个图片的宽和高,注意此处的bitmap为null    
  18.         bitmap = BitmapFactory.decodeFile(imagePath, options);    
  19.         options.inJustDecodeBounds = false// 设为 false    
  20.         // 计算缩放比    
  21.         int h = options.outHeight;    
  22.         int w = options.outWidth;    
  23.         int beWidth = w / width;    
  24.         int beHeight = h / height;    
  25.         int be = 1;    
  26.         if (beWidth < beHeight) {    
  27.             be = beWidth;    
  28.         } else {    
  29.             be = beHeight;    
  30.         }    
  31.         if (be <= 0) {    
  32.             be = 1;    
  33.         }    
  34.         options.inSampleSize = be;    
  35.         // 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false    
  36.         bitmap = BitmapFactory.decodeFile(imagePath, options);    
  37.         // 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象    
  38.         bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,    
  39.                 ThumbnailUtils.OPTIONS_RECYCLE_INPUT);    
  40.         return bitmap;    
  41.     }    
  42.     
  43.     /**  
  44.      * 获取视频的缩略图  
  45.      * 先通过ThumbnailUtils来创建一个视频的缩略图,然后再利用ThumbnailUtils来生成指定大小的缩略图。  
  46.      * 如果想要的缩略图的宽和高都小于MICRO_KIND,则类型要使用MICRO_KIND作为kind的值,这样会节省内存。  
  47.      * @param videoPath 视频的路径  
  48.      * @param width 指定输出视频缩略图的宽度  
  49.      * @param height 指定输出视频缩略图的高度度  
  50.      * @param kind 参照MediaStore.Images.Thumbnails类中的常量MINI_KIND和MICRO_KIND。  
  51.      *            其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96  
  52.      * @return 指定大小的视频缩略图  
  53.      */    
  54.     private Bitmap getVideoThumbnail(String videoPath, int width, int height,    
  55.             int kind) {    
  56.         Bitmap bitmap = null;    
  57.         // 获取视频的缩略图    
  58.         bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);    
  59.         System.out.println("w"+bitmap.getWidth());    
  60.         System.out.println("h"+bitmap.getHeight());    
  61.         bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,    
  62.                 ThumbnailUtils.OPTIONS_RECYCLE_INPUT);    
  63.         return bitmap;    
  64.     }    

0 0
原创粉丝点击