Android編程擷取圖片和影片縮圖的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程擷取圖片和影片縮圖的方法。分享給大家供大家參考,具體如下:

從Android 2.2開始系統新增了一個縮圖ThumbnailUtils類,位於framework的android.media.ThumbnailUtils位 置,可以協助我們從mediaprovider中擷取系統中的視頻或圖片檔案的縮圖,該類提供了三種靜態方法可以直接調用擷取。

1. createVideoThumbnail

static Bitmap createVideoThumbnail(String filePath, int kind)//擷取視頻檔案的縮圖//第一個參數為視頻檔案的位置,比如/sdcard/android123.3gp,//第二個參數可以為MINI_KIND或 MICRO_KIND最終和解析度有關

2. extractThumbnail

static Bitmap extractThumbnail(Bitmap source, int width, int height, int options)//直接對Bitmap進行縮減操作//最後一個參數定義為OPTIONS_RECYCLE_INPUT ,來回收資源

3. extractThumbnail

static Bitmap extractThumbnail(Bitmap source, int width, int height)// 這個和上面的方法一樣,無options選項

擷取手機裡影片縮圖:

public static Bitmap getVideoThumbnail(ContentResolver cr, Uri uri) {  Bitmap bitmap = null;  BitmapFactory.Options options = new BitmapFactory.Options();  options.inDither = false;  options.inPreferredConfig = Bitmap.Config.ARGB_8888;  Cursor cursor = cr.query(uri,new String[] { MediaStore.Video.Media._ID }, null, null, null);  if (cursor == null || cursor.getCount() == 0) { return null;  }  cursor.moveToFirst();  String videoId = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media._ID)); //image id in image table.s  if (videoId == null) {  return null;  }  cursor.close();  long videoIdLong = Long.parseLong(videoId);  bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, videoIdLong,Images.Thumbnails.MICRO_KIND, options);  return bitmap;}

獲得指定目錄sdcard裡的影片縮圖:

import java.io.File;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.media.ThumbnailUtils;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.widget.ImageView;/*** 擷取圖片和視頻的縮圖* 這兩個方法必須在2.2及以上版本使用,因為其中使用了ThumbnailUtils這個類*/public class AndroidTestActivity extends Activity {   private ImageView imageThumbnail;   private ImageView videoThumbnail;   /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   imageThumbnail = (ImageView) findViewById(R.id.image_thumbnail);   videoThumbnail = (ImageView) findViewById(R.id.video_thumbnail);   String imagePath = Environment.getExternalStorageDirectory()    .getAbsolutePath()    + File.separator    + "photo"    + File.separator    + "yexuan.jpg";   String videoPath = Environment.getExternalStorageDirectory()    .getAbsolutePath()    + File.separator    + "video"    + File.separator    + "醋點燈.avi";   imageThumbnail.setImageBitmap(getImageThumbnail(imagePath, 60, 60));   videoThumbnail.setImageBitmap(getVideoThumbnail(videoPath, 60, 60,    MediaStore.Images.Thumbnails.MICRO_KIND));   }   /**   * 根據指定的映像路徑和大小來擷取縮圖   * 此方法有兩點好處:   *   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;   }}

布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="圖片縮圖" />    <ImageView      android:id="@+id/image_thumbnail"      android:layout_width="wrap_content"      android:layout_height="wrap_content" />    <TextView      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="影片縮圖" />    <ImageView      android:id="@+id/video_thumbnail"      android:layout_width="wrap_content"      android:layout_height="wrap_content" /></LinearLayout>

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android圖形與影像處理技巧總結》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結》及《Android控制項用法總結》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.