您当前的位置: 首页 >  android

xiangzhihong8

暂无认证

  • 3浏览

    0关注

    1324博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

android读取sd卡图片并进行缩放操作

xiangzhihong8 发布时间:2014-01-08 19:17:10 ,浏览量:3

在Android中对大图片进行缩放真的很不尽如人意,不知道是不是我的方法不对。下面我列出3种对图片缩放的方法,并给出相应速度。请高人指教。 第一种是BitmapFactory和BitmapFactory.Options。 首先,BitmapFactory.Options有几个Fields很有用: inJustDecodeBounds:If set to true, the decoder will return null (no bitmap), but the out... 也就是说,当inJustDecodeBounds设成true时,bitmap并不加载到内存,这样效率很高哦。而这时,你可以获得bitmap的高、宽等信息。 outHeight:The resulting height of the bitmap, set independent of the state of inJustDecodeBounds. outWidth:The resulting width of the bitmap, set independent of the state of inJustDecodeBounds.  看到了吧,上面3个变量是相关联的哦。 inSampleSize : If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. 这就是用来做缩放比的。这里有个技巧: inSampleSize=(outHeight/Height+outWidth/Width)/2 实践证明,这样缩放出来的图片还是很好的。 最后用BitmapFactory.decodeFile(path, options)生成。 由于只是对bitmap加载到内存一次,所以效率比较高。解析速度快。 第二种是使用Bitmap加Matrix来缩放。

首先要获得原bitmap,再从原bitmap的基础上生成新图片。这样效率很低。

第三种是用2.2新加的类ThumbnailUtils来做。 让我们新看看这个类,从API中来看,此类就三个静态方法:createVideoThumbnail、extractThumbnail(Bitmap source, int width, int height, int options)、extractThumbnail(Bitmap source, int width, int height)。 我这里使用了第三个方法。再看看它的源码,下面会附上。是上面我们用到的BitmapFactory.Options和Matrix等经过人家一阵加工而成。 效率好像比第二种方法高一点点。

下面是我的例子:

[html] view plain copy
  1.   
  2.   
  3.       
  4.      
  5.     
  6.   
  7.   
[java] view plain copy
  1. package com.linc.ResolvePicture;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7.   
  8. import android.app.Activity;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.graphics.Matrix;  
  12. import android.graphics.drawable.BitmapDrawable;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.media.ThumbnailUtils;  
  15. import android.os.Bundle;  
  16. import android.util.Log;  
  17. import android.widget.ImageView;  
  18. import android.widget.TextView;  
  19.   
  20. public class ResolvePicture extends Activity {  
  21.     private static String tag="ResolvePicture";  
  22.     Drawable bmImg;    
  23.     ImageView imView;   
  24.     ImageView imView2;   
  25.     TextView text;  
  26.     String theTime;  
  27.     long start, stop;   
  28.     /** Called when the activity is first created. */  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setContentView(R.layout.main);  
  33.           
  34.         text=(TextView)findViewById(R.id.text);  
  35.           
  36.         imView=(ImageView) findViewById(R.id.imageShow);  
  37.         imView2=(ImageView) findViewById(R.id.image2);  
  38.           
  39.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(),     
  40.                 R.drawable.pic);  
  41.           
  42.         start=System.currentTimeMillis();  
  43.           
  44. //        imView.setImageDrawable(resizeImage(bitmap, 300, 100));   
  45.           
  46.         imView2.setImageDrawable(resizeImage2("/sdcard/2.jpeg", 200, 100));   
  47.           
  48.         stop=System.currentTimeMillis();  
  49.           
  50.         String theTime= String.format("\n1 iterative: (%d msec)",    
  51.                 stop - start);    
  52.           
  53.         start=System.currentTimeMillis();  
  54.         imView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,200,100));//2.2才加进来的新类,简单易用  
  55. //        imView.setImageDrawable(resizeImage(bitmap, 30, 30));   
  56.         stop=System.currentTimeMillis();  
  57.           
  58.          theTime+= String.format("\n2 iterative: (%d msec)",    
  59.                 stop - start);   
  60.           
  61.         text.setText(theTime);  
  62.     }  
  63.       
  64.     //使用Bitmap加Matrix来缩放  
  65.     public static Drawable resizeImage(Bitmap bitmap, int w, int h)   
  66.     {    
  67.         Bitmap BitmapOrg = bitmap;    
  68.         int width = BitmapOrg.getWidth();    
  69.         int height = BitmapOrg.getHeight();    
  70.         int newWidth = w;    
  71.         int newHeight = h;    
  72.   
  73.         float scaleWidth = ((float) newWidth) / width;    
  74.         float scaleHeight = ((float) newHeight) / height;    
  75.   
  76.         Matrix matrix = new Matrix();    
  77.         matrix.postScale(scaleWidth, scaleHeight);    
  78.         // if you want to rotate the Bitmap     
  79.         // matrix.postRotate(45);     
  80.         Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,    
  81.                         height, matrix, true);    
  82.         return new BitmapDrawable(resizedBitmap);    
  83.     }  
  84.       
  85.     //使用BitmapFactory.Options的inSampleSize参数来缩放  
  86.     public static Drawable resizeImage2(String path,  
  87.             int width,int height)   
  88.     {  
  89.         BitmapFactory.Options options = new BitmapFactory.Options();  
  90.         options.inJustDecodeBounds = true;//不加载bitmap到内存中  
  91.         BitmapFactory.decodeFile(path,options);   
  92.         int outWidth = options.outWidth;  
  93.         int outHeight = options.outHeight;  
  94.         options.inDither = false;  
  95.         options.inPreferredConfig = Bitmap.Config.ARGB_8888;  
  96.         options.inSampleSize = 1;  
  97.           
  98.         if (outWidth != 0 && outHeight != 0 && width != 0 && height != 0)   
  99.         {  
  100.             int sampleSize=(outWidth/width+outHeight/height)/2;  
  101.             Log.d(tag, "sampleSize = " + sampleSize);  
  102.             options.inSampleSize = sampleSize;  
  103.         }  
  104.       
  105.         options.inJustDecodeBounds = false;  
  106.         return new BitmapDrawable(BitmapFactory.decodeFile(path, options));       
  107.     }  
  108.   
  109.     //图片保存  
  110.     private void saveThePicture(Bitmap bitmap)  
  111.     {  
  112.         File file=new File("/sdcard/2.jpeg");  
  113.         try  
  114.         {  
  115.             FileOutputStream fos=new FileOutputStream(file);  
  116.             if(bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos))  
  117.             {  
  118.                 fos.flush();  
  119.                 fos.close();  
  120.             }  
  121.         }  
  122.         catch(FileNotFoundException e1)  
  123.         {  
  124.             e1.printStackTrace();  
  125.         }  
  126.         catch(IOException e2)  
  127.         {  
  128.             e2.printStackTrace();  
  129.         }  
  130.     }  
  131. }  
ThumbnailUtils源码:
[java] view plain copy
  1. /* 
  2.  * Copyright (C) 2009 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */  
  16.   
  17. package android.media;  
  18.   
  19. import android.content.ContentResolver;  
  20. import android.content.ContentUris;  
  21. import android.content.ContentValues;  
  22. import android.database.Cursor;  
  23. import android.graphics.Bitmap;  
  24. import android.graphics.BitmapFactory;  
  25. import android.graphics.Canvas;  
  26. import android.graphics.Matrix;  
  27. import android.graphics.Rect;  
  28. import android.media.MediaMetadataRetriever;  
  29. import android.media.MediaFile.MediaFileType;  
  30. import android.net.Uri;  
  31. import android.os.ParcelFileDescriptor;  
  32. import android.provider.BaseColumns;  
  33. import android.provider.MediaStore.Images;  
  34. import android.provider.MediaStore.Images.Thumbnails;  
  35. import android.util.Log;  
  36.   
  37. import java.io.FileInputStream;  
  38. import java.io.FileDescriptor;  
  39. import java.io.IOException;  
  40. import java.io.OutputStream;  
  41.   
  42. /** 
  43.  * Thumbnail generation routines for media provider. 
  44.  */  
  45.   
  46. public class ThumbnailUtils {  
  47.     private static final String TAG = "ThumbnailUtils";  
  48.   
  49.     /* Maximum pixels size for created bitmap. */  
  50.     private static final int MAX_NUM_PIXELS_THUMBNAIL = 512 * 384;  
  51.     private static final int MAX_NUM_PIXELS_MICRO_THUMBNAIL = 128 * 128;  
  52.     private static final int UNCONSTRAINED = -1;  
  53.   
  54.     /* Options used internally. */  
  55.     private static final int OPTIONS_NONE = 0x0;  
  56.     private static final int OPTIONS_SCALE_UP = 0x1;  
  57.   
  58.     /** 
  59.      * Constant used to indicate we should recycle the input in 
  60.      * {@link #extractThumbnail(Bitmap, int, int, int)} unless the output is the input. 
  61.      */  
  62.     public static final int OPTIONS_RECYCLE_INPUT = 0x2;  
  63.   
  64.     /** 
  65.      * Constant used to indicate the dimension of mini thumbnail. 
  66.      * @hide Only used by media framework and media provider internally. 
  67.      */  
  68.     public static final int TARGET_SIZE_MINI_THUMBNAIL = 320;  
  69.   
  70.     /** 
  71.      * Constant used to indicate the dimension of micro thumbnail. 
  72.      * @hide Only used by media framework and media provider internally. 
  73.      */  
  74.     public static final int TARGET_SIZE_MICRO_THUMBNAIL = 96;  
  75.   
  76.     /** 
  77.      * This method first examines if the thumbnail embedded in EXIF is bigger than our target 
  78.      * size. If not, then it'll create a thumbnail from original image. Due to efficiency 
  79.      * consideration, we want to let MediaThumbRequest avoid calling this method twice for 
  80.      * both kinds, so it only requests for MICRO_KIND and set saveImage to true. 
  81.      * 
  82.      * This method always returns a "square thumbnail" for MICRO_KIND thumbnail. 
  83.      * 
  84.      * @param filePath the path of image file 
  85.      * @param kind could be MINI_KIND or MICRO_KIND 
  86.      * @return Bitmap 
  87.      * 
  88.      * @hide This method is only used by media framework and media provider internally. 
  89.      */  
  90.     public static Bitmap createImageThumbnail(String filePath, int kind) {  
  91.         boolean wantMini = (kind == Images.Thumbnails.MINI_KIND);  
  92.         int targetSize = wantMini  
  93.                 ? TARGET_SIZE_MINI_THUMBNAIL  
  94.                 : TARGET_SIZE_MICRO_THUMBNAIL;  
  95.         int maxPixels = wantMini  
  96.                 ? MAX_NUM_PIXELS_THUMBNAIL  
  97.                 : MAX_NUM_PIXELS_MICRO_THUMBNAIL;  
  98.         SizedThumbnailBitmap sizedThumbnailBitmap = new SizedThumbnailBitmap();  
  99.         Bitmap bitmap = null;  
  100.         MediaFileType fileType = MediaFile.getFileType(filePath);  
  101.         if (fileType != null && fileType.fileType == MediaFile.FILE_TYPE_JPEG) {  
  102.             createThumbnailFromEXIF(filePath, targetSize, maxPixels, sizedThumbnailBitmap);  
  103.             bitmap = sizedThumbnailBitmap.mBitmap;  
  104.         }  
  105.   
  106.         if (bitmap == null) {  
  107.             try {  
  108.                 FileDescriptor fd = new FileInputStream(filePath).getFD();  
  109.                 BitmapFactory.Options options = new BitmapFactory.Options();  
  110.                 options.inSampleSize = 1;  
  111.                 options.inJustDecodeBounds = true;  
  112.                 BitmapFactory.decodeFileDescriptor(fd, null, options);  
  113.                 if (options.mCancel || options.outWidth == -1  
  114.                         || options.outHeight == -1) {  
  115.                     return null;  
  116.                 }  
  117.                 options.inSampleSize = computeSampleSize(  
  118.                         options, targetSize, maxPixels);  
  119.                 options.inJustDecodeBounds = false;  
  120.   
  121.                 options.inDither = false;  
  122.                 options.inPreferredConfig = Bitmap.Config.ARGB_8888;  
  123.                 bitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);  
  124.             } catch (IOException ex) {  
  125.                 Log.e(TAG, "", ex);  
  126.             }  
  127.         }  
  128.   
  129.         if (kind == Images.Thumbnails.MICRO_KIND) {  
  130.             // now we make it a "square thumbnail" for MICRO_KIND thumbnail  
  131.             bitmap = extractThumbnail(bitmap,  
  132.                     TARGET_SIZE_MICRO_THUMBNAIL,  
  133.                     TARGET_SIZE_MICRO_THUMBNAIL, OPTIONS_RECYCLE_INPUT);  
  134.         }  
  135.         return bitmap;  
  136.     }  
  137.   
  138.     /** 
  139.      * Create a video thumbnail for a video. May return null if the video is 
  140.      * corrupt or the format is not supported. 
  141.      * 
  142.      * @param filePath the path of video file 
  143.      * @param kind could be MINI_KIND or MICRO_KIND 
  144.      */  
  145.     public static Bitmap createVideoThumbnail(String filePath, int kind) {  
  146.         Bitmap bitmap = null;  
  147.         MediaMetadataRetriever retriever = new MediaMetadataRetriever();  
  148.         try {  
  149.             retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY);  
  150.             retriever.setDataSource(filePath);  
  151.             bitmap = retriever.captureFrame();  
  152.         } catch (IllegalArgumentException ex) {  
  153.             // Assume this is a corrupt video file  
  154.         } catch (RuntimeException ex) {  
  155.             // Assume this is a corrupt video file.  
  156.         } finally {  
  157.             try {  
  158.                 retriever.release();  
  159.             } catch (RuntimeException ex) {  
  160.                 // Ignore failures while cleaning up.  
  161.             }  
  162.         }  
  163.         if (kind == Images.Thumbnails.MICRO_KIND && bitmap != null) {  
  164.             bitmap = extractThumbnail(bitmap,  
  165.                     TARGET_SIZE_MICRO_THUMBNAIL,  
  166.                     TARGET_SIZE_MICRO_THUMBNAIL,  
  167.                     OPTIONS_RECYCLE_INPUT);  
  168.         }  
  169.         return bitmap;  
  170.     }  
  171.   
  172.     /** 
  173.      * Creates a centered bitmap of the desired size. 
  174.      * 
  175.      * @param source original bitmap source 
  176.      * @param width targeted width 
  177.      * @param height targeted height 
  178.      */  
  179.     public static Bitmap extractThumbnail(  
  180.             Bitmap source, int width, int height) {  
  181.         return extractThumbnail(source, width, height, OPTIONS_NONE);  
  182.     }  
  183.   
  184.     /** 
  185.      * Creates a centered bitmap of the desired size. 
  186.      * 
  187.      * @param source original bitmap source 
  188.      * @param width targeted width 
  189.      * @param height targeted height 
  190.      * @param options options used during thumbnail extraction 
  191.      */  
  192.     public static Bitmap extractThumbnail(  
  193.             Bitmap source, int width, int height, int options) {  
  194.         if (source == null) {  
  195.             return null;  
  196.         }  
  197.   
  198.         float scale;  
  199.         if (source.getWidth() 
关注
打赏
1482932726
查看更多评论
立即登录/注册

微信扫码登录

0.0517s