标签:
1 public class BitmapUtil { 2 3 private static BitmapFactory.Options opt; 4 /** 5 * 从SD Card中获取图片 6 * 7 */ 8 public static Bitmap getBitmapFormSDCrad(String PicturePath) { 9 Bitmap bitmap = null; 10 opt = new BitmapFactory.Options(); 11 opt.inPreferredConfig = Bitmap.Config.RGB_565; 12 opt.inPurgeable = true; 13 opt.inInputShareable = true; 14 try { 15 InputStream iss = new FileInputStream(PicturePath); 16 opt.inJustDecodeBounds = true; 17 BitmapFactory.decodeStream(iss, null, opt); 18 if (opt.outWidth > 3000 || opt.outHeight > 3000) { 19 opt.inSampleSize = 2; 20 } 21 opt.inJustDecodeBounds = false; 22 iss = new FileInputStream(PicturePath); 23 bitmap = BitmapFactory.decodeStream(iss, null, opt); 24 // 获取图片的原始拍摄角度,旋转修正 25 int rotatedDegree = readPictureDegree(PicturePath); 26 bitmap = rotateBitmap(bitmap, rotatedDegree); 27 } catch (FileNotFoundException e) { 28 Log.e("HeadColorView", e.toString()); 29 } 30 return bitmap; 31 } 32 33 /* 34 * 按照模板计算图片尺寸 35 */ 36 private static int calculateInSampleSize(Options options, int reqWidth, 37 int reqHeight) { 38 final int height = options.outHeight; 39 final int width = options.outWidth; 40 int inSampleSize = 1; 41 //等比例缩放 42 if (height > reqHeight || width > reqWidth) { 43 44 final int heightRatio = Math.round((float) height 45 / (float) reqHeight); 46 final int widthRatio = Math.round((float) width / (float) reqWidth); 47 inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio; 48 } 49 return inSampleSize; 50 } 51 52 /** 53 * 旋转图片,使图片保持正确的方向。 54 * @param bitmap 原始图片 55 * @param degrees 原始图片的角度 56 * @return Bitmap 旋转后的图片 57 */ 58 public static Bitmap rotateBitmap(Bitmap bitmap, int degrees) { 59 if (degrees == 0 || null == bitmap) { 60 return bitmap; 61 } 62 Matrix matrix = new Matrix(); 63 matrix.setRotate(degrees, bitmap.getWidth() / 2, bitmap.getHeight() / 2); 64 Bitmap bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 65 if (null != bitmap) { 66 bitmap.recycle(); 67 } 68 return bmp; 69 } 70 71 /** 72 * 读取图片属性:旋转的角度 73 * @param path 图片绝对路径 74 * @return degree旋转的角度 75 */ 76 private static int readPictureDegree(String filePath) { 77 int degree = 0; 78 try { 79 ExifInterface exifInterface = new ExifInterface(filePath); 80 int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL); 81 switch (orientation) { 82 case ExifInterface.ORIENTATION_ROTATE_90: 83 degree = 90; 84 break; 85 case ExifInterface.ORIENTATION_ROTATE_180: 86 degree = 180; 87 break; 88 case ExifInterface.ORIENTATION_ROTATE_270: 89 degree = 270; 90 break; 91 } 92 } catch (IOException e) { 93 e.printStackTrace(); 94 return degree; 95 } 96 return degree; 97 } 98 99 }
标签:
原文地址:http://www.cnblogs.com/ware/p/4432364.html