標籤:image android
擷取影片縮圖:ThumbnailUtils方式擷取
/** * 擷取視頻的縮圖 必須在2.2及以上版本使用,因為其中使用了ThumbnailUtils這個類 * 先通過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; } }
MediaMetadataRetriever方式
public Bitmap getVideoThumbnail(String filePath) {Bitmap bitmap = null;MediaMetadataRetriever retriever = new MediaMetadataRetriever();try {retriever.setDataSource(filePath);bitmap = retriever.getFrameAtTime();} catch(IllegalArgumentException e) {e.printStackTrace();} catch (RuntimeException e) {e.printStackTrace();} finally {try {retriever.release();} catch (RuntimeException e) {e.printStackTrace();}}return bitmap;}
內容提供器來擷取
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.sif (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;}
圖片縮圖擷取
** * 根據指定的映像路徑和大小來擷取縮圖 * 此方法有兩點好處: * 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; }
Android 下擷取視頻和圖片的縮圖