android中图片压缩以及图片旋转的方法

来源:互联网 发布:红蜘蛛软件破解 编辑:程序博客网 时间:2024/06/09 20:40

转载:http://blog.csdn.net/xiaorenwu1206/article/details/41803431


在开发中,如果需要上传图片到服务器中,而且还需要在本地预览,就会用到图片的压缩:

[java] view plaincopy
  1. /** 
  2.  * 计算图片的缩放值 
  3.  *  
  4.  * @param options 
  5.  * @param reqWidth 
  6.  * @param reqHeight 
  7.  * @return 
  8.  */  
  9. public static int calculateInSampleSize(BitmapFactory.Options options,  
  10.         int reqWidth, int reqHeight) {  
  11.     final int height = options.outHeight;  
  12.     final int width = options.outWidth;  
  13.     int inSampleSize = 1;  
  14.     if (height > reqHeight || width > reqWidth) {  
  15.         final int heightRatio = Math.round((float) height  
  16.                 / (float) reqHeight);  
  17.         final int widthRatio = Math.round((float) width / (float) reqWidth);  
  18.         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  19.     }  
  20.     return inSampleSize;  
  21. }  
  22.   
  23. /** 
  24.  * 根据路径获得图片并压缩返回bitmap 
  25.  *  
  26.  * @param filePath 
  27.  * @return 
  28.  */  
  29. public static Bitmap getSmallBitmap(String filePath, int reqWidth,  
  30.         int reqHeight) {  
  31.     final BitmapFactory.Options options = new BitmapFactory.Options();  
  32.     options.inJustDecodeBounds = true;  
  33.     BitmapFactory.decodeFile(filePath, options);  
  34.     options.inSampleSize = calculateInSampleSize(options, reqWidth,  
  35.             reqHeight);  
  36.     options.inJustDecodeBounds = false;  
  37.     return BitmapFactory.decodeFile(filePath, options);  
  38. }  

有时候显示图片的时候 会发现有些图片的位置不正,这时我们就需要调整一下方向:

[java] view plaincopy
  1.    /** 
  2.  * 获取图片文件的信息,是否旋转了90度,如果是则反转 
  3.  * @param bitmap 需要旋转的图片 
  4.  * @param path   图片的路径 
  5.  */  
  6. public static Bitmap reviewPicRotate(Bitmap bitmap,String path){  
  7.     int degree = getPicRotate(path);  
  8.     if(degree!=0){  
  9.         Matrix m = new Matrix();    
  10.         int width = bitmap.getWidth();    
  11.         int height = bitmap.getHeight();    
  12.         m.setRotate(degree); // 旋转angle度    
  13.         bitmap = Bitmap.createBitmap(bitmap, 00, width, height,m, true);// 从新生成图片    
  14.     }  
  15.     return bitmap;  
  16. }  
  17.   
  18. /** 
  19.  * 读取图片文件旋转的角度 
  20.  * @param path 图片绝对路径 
  21.  * @return 图片旋转的角度 
  22.  */  
  23. public static int getPicRotate(String path) {  
  24.     int degree  = 0;  
  25.     try {  
  26.         ExifInterface exifInterface = new ExifInterface(path);  
  27.         int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);  
  28.         switch (orientation) {  
  29.         case ExifInterface.ORIENTATION_ROTATE_90:  
  30.             degree = 90;  
  31.             break;  
  32.         case ExifInterface.ORIENTATION_ROTATE_180:  
  33.             degree = 180;  
  34.             break;  
  35.         case ExifInterface.ORIENTATION_ROTATE_270:  
  36.             degree = 270;  
  37.             break;  
  38.         }  
  39.     } catch (IOException e) {  
  40.         e.printStackTrace();  
  41.     }  
  42.     return degree;  
  43. }  

1 0
原创粉丝点击