1、校驗圖片角度,有旋轉進行修改;然後校驗圖片大小,大於標清(720*1280)進行壓縮;調用下面該方法傳入需要壓縮的圖片文件,返回壓縮後的圖片文件;方法調用需要運行在子線程中進行
public File getModificationFile(File file){ file=reviewPicRotate(file); if(verifyPictureSize(file.getPath())){//標清不壓縮 return file; }else{//壓縮 File compressionFile = saveBitmapToLocal(getBitmapFromFile(file.getPath())); if(compressionFile == null) return file; return compressionFile; } }
2、獲取圖片文件的信息,判斷是否旋轉矯正
public File reviewPicRotate(File file){ //讀取圖片文件旋轉的角度 int degree = 0; try { ExifInterface exifInterface = new ExifInterface(file.getPath()); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } if(degree != 0){//圖片旋轉了;進行圖片旋轉 Bitmap bitmap = getBitmapFromFile(file.getPath()); Matrix m = new Matrix(); int width = bitmap.getWidth(); int height = bitmap.getHeight(); m.setRotate(degree); // 旋轉angle度 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true); return saveBitmapToLocal(bitmap);// 從新生成圖片文件 } return file; }
3、判斷圖片如果小於720 * 1280直接返回原圖片文件,否則壓縮
public static boolean verifyPictureSize(String path) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, opts); if (opts.outHeight <= 1280 && opts.outWidth <= 720) return true; else return false; }
4、將文件轉換爲720*1280的bitmap
public static Bitmap getBitmapFromFile(String path) { int width = 720; int height = 1280; BitmapFactory.Options opts = null; if(path != null) { if (width > 0 && height > 0) { opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, opts); final int minSideLength = Math.min(width, height); opts.inSampleSize = computeSampleSize(opts, minSideLength,width * height); opts.inJustDecodeBounds = false; } return BitmapFactory.decodeFile(path, opts); } else return null; }
5、計算壓縮比率
public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; }
private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } }
6、將轉換後的bitmap對象保存爲file文件並存儲到本地
public File saveBitmapToLocal(Bitmap bitmap) { if (null == bitmap) { return null; } FileOutputStream fileOutput = null; File imgFile = null; try { imgFile = SDUtils.getCreatFile(context,SDUtils.imgCachePicUrl, System.currentTimeMillis()+".jpg");//新創建的圖片文件對象 fileOutput = new FileOutputStream(imgFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutput); fileOutput.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); imgFile = null; } catch (IOException e) { e.printStackTrace(); imgFile = null; } finally { if (null != fileOutput) { try { fileOutput.close(); } catch (IOException e) { e.printStackTrace(); } } } return imgFile; }