//判断照片角度 private final static int getDegress(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); 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(); } return degree; } /** * rotate the bitmap * @param bitmap * @param degress * @return */ private static Bitmap rotateBitmap(Bitmap bitmap, int degress) { if (bitmap != null) { Matrix m = new Matrix(); m.postRotate(degress); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); return bitmap; } return bitmap; } /** * 压缩指定路径图片,并将其保存在缓存目录中,通过isDelSrc判定是否删除源文件,并获取到缓存后的图片路径 * @param context * @param srcPath * @param isDelSrc * @return */ public final static String compressBitmap(Context context, String srcPath, boolean isDelSrc) { Bitmap bitmap = compressImage(srcPath); File srcFile = new File(srcPath); String desPath = ""; int degree = getDegress(srcPath); try { if (degree != 0) bitmap = rotateBitmap(bitmap, degree); File file = new File("新路径"+srcFile.getName()); desPath = file.getPath(); FileOutputStream fos = new FileOutputStream(file); if(srcPath.toLowerCase().endsWith("png")) bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);//把压缩后的数据存放到baos中 else bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos); fos.close(); if (isDelSrc) srcFile.deleteOnExit(); } catch (Exception e) { Log.e("BitmapHelper-->compressBitmap", e.getMessage()+""); } return desPath; } /** * 基于质量的压缩算法, 此方法未 解决压缩后图像失真问题 * <br> 可先调用比例压缩适当压缩图片后,再调用此方法可解决上述问题 * @param bts * @param maxBytes 压缩后的图像最大大小 单位为byte * @return */ public final static Bitmap compressImage(Bitmap image, long maxBytes) { try { int options = 100; ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 while ( baos.toByteArray().length/1024 > maxBytes) {//循环判断如果压缩后图片是否大maxByteskb,大于继续压缩 baos.reset();//重置baos即清空baos options -= 10;//每次都减少10 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 } byte[] bts = baos.toByteArray(); Bitmap bmp = BitmapFactory.decodeByteArray(bts, 0, bts.length); return bmp; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 按照图片的比例进行压缩 * @param srcPath * @return */ public final static Bitmap compressImage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float reqHeight = 800f;//这里设置高度为800f float reqWidth = 480f;//这里设置宽度为480f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int inSampleSize = 1; if (h > reqHeight || w > reqWidth) { final int heightRatio = Math.round((float) h/ (float) reqHeight); final int widthRatio = Math.round((float) w / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } if (inSampleSize <= 0) inSampleSize = 1; newOpts.inSampleSize = inSampleSize;//设置缩放比例 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); if(bitmap != null && !srcPath.toLowerCase().endsWith("png"))//png格式的图片不能进行质量压缩 bitmap = compressImage(bitmap,100);//压缩好比例大小后再进行质量压缩 return bitmap; }