/** * <图片按比例大小压缩方法(根据路径获取图片并压缩)> * * @return 压缩后图片路径 */ public static String getImageScaleByPath(PicturePropertiesBean propertiesBean, Context context) { Bitmap bitmap = null; BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; bitmap = BitmapFactory.decodeFile(propertiesBean.getSrcPath(), newOpts); int width = newOpts.outWidth; int height = newOpts.outHeight; // float minHeight = 800f;//设置为主流手机分辨率800*480 // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1; // be=1表示不缩放 if (height > width && width > propertiesBean.getWidth()) { // 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / propertiesBean.getWidth()); } else if (width > height && height > propertiesBean.getHeight()) { // 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / propertiesBean.getHeight()); } if (be <= 0) be = 1; newOpts.inSampleSize = be; // 设置缩放比例 newOpts.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(propertiesBean.getSrcPath(), newOpts); return compressImage(bitmap, context, propertiesBean); // 压缩好比例大小后再进行质量压缩 }
/** * <质量压缩方法> * * @return 压缩后图片路径 */ private static String compressImage( Bitmap image, Context context, PicturePropertiesBean propertiesBean) { File file = null; if (image != null) { try { int degree = getExifOrientation(propertiesBean.getSrcPath()); if (degree > 0) { Matrix matrix = new Matrix(); matrix.setRotate(degree); Bitmap rotateBitmap = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true); if (rotateBitmap != null) { image.recycle(); image = rotateBitmap; } } ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); // 图片大于最大值,则压缩,否则不做任何操作 if (baos.toByteArray().length > propertiesBean.getMaxSize()) { baos.reset(); // 质量压缩方法,首先压缩options的压缩率 image.compress(Bitmap.CompressFormat.JPEG, propertiesBean.getDefaultOption(), baos); // 循环判断如果压缩后图片是否大于200kb,大于继续压缩 while (baos.toByteArray().length > propertiesBean.getMaxSize()) { baos.reset(); // 重置baos即清空baos // 这里压缩defaultOptions%,把压缩后的数据存放到baos中 propertiesBean.setDefaultOption( propertiesBean.getDefaultOption() - propertiesBean.getOptions()); // 每次都减少option image.compress(Bitmap.CompressFormat.JPEG, propertiesBean.getDefaultOption(), baos); } while (baos.toByteArray().length < propertiesBean.getMinSize()) { baos.reset(); // 重置baos即清空baos // 这里压缩options%,把压缩后的数据存放到baos中 propertiesBean.setDefaultOption( propertiesBean.getDefaultOption() + propertiesBean.getOptions()); // 每次都增加option image.compress(Bitmap.CompressFormat.JPEG, propertiesBean.getDefaultOption(), baos); } } file = new File(propertiesBean.getDestPath()); FileOutputStream stream = new FileOutputStream(file); if (baos != null) { stream.write(baos.toByteArray()); stream.flush(); } stream.close(); baos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (image != null) image.recycle(); } return file.getPath(); } else { return ""; } }