亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android如何獲取圖片或視頻略縮圖

 更新時間:2016年08月24日 09:49:29   作者:※WYF※  
這篇文章主要為大家詳細(xì)介紹了Android如何獲取圖片或視頻略縮圖的方法,感興趣的小伙伴們可以參考一下

根據(jù)指定的圖像路徑和大小來獲取縮略圖 此方法有兩點好處:

1.使用較小的內(nèi)存空間,第一次獲取的bitmap實際上為null,只是為了讀取寬度和高度,第二次讀取的bitmap是根據(jù)比例壓縮過的圖像,第三次讀取的bitmap是所要的縮略圖。

2. 縮略圖對于原圖像來講沒有拉伸,這里使用了2.2版本的新工具ThumbnailUtils,使 用這個工具生成的圖像不會被拉伸。

/**

  * @param imagePath
  *   圖像的路徑
  * @param width
  *   指定輸出圖像的寬度
  * @param height
  *   指定輸出圖像的高度
  * @return 生成的縮略圖
  */
 public static 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; // 設(shè)為 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 設(shè)為 false
 bitmap = BitmapFactory.decodeFile(imagePath, options);
 // 利用ThumbnailUtils來創(chuàng)建縮略圖,這里要指定要縮放哪個Bitmap對象
 bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
  ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
 return bitmap;
 }

 /**
  * 獲取視頻的縮略圖 先通過ThumbnailUtils來創(chuàng)建一個視頻的縮略圖,然后再利用ThumbnailUtils來生成指定大小的縮略圖。
  * 如果想要的縮略圖的寬和高都小于MICRO_KIND,則類型要使用MICRO_KIND作為kind的值,這樣會節(jié)省內(nèi)存。
  * 
  * @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 指定大小的視頻縮略圖
  */
 public static 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;
 }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論