android bitmap compress(图片压缩)
android bitmap compress
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(filePath, options);
- // Calculate inSampleSize
- options.inSampleSize = calculateInSampleSize(options, 480, 800);
- // Decode bitmap with inSampleSize set
- options.inJustDecodeBounds = false;
- Bitmap bm = BitmapFactory.decodeFile(filePath, options);
- int degree = readPictureDegree(filePath);
- bm = rotateBitmap(bm,degree) ;
- private static int readPictureDegree(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;
- }
- private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){
- if(bitmap == null)
- return null ;
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- // Setting post rotate to 90
- Matrix mtx = new Matrix();
- mtx.postRotate(rotate);
- return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
- }
- bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0
- public static Bitmap getSmallBitmap(String filePath) {
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeFile(filePath, options);
- // Calculate inSampleSize
- options.inSampleSize = calculateInSampleSize(options, 480, 800);
- // Decode bitmap with inSampleSize set
- options.inJustDecodeBounds = false;
- Bitmap bm = BitmapFactory.decodeFile(filePath, options);
- if(bm == null){
- return null;
- }
- int degree = readPictureDegree(filePath);
- bm = rotateBitmap(bm,degree) ;
- ByteArrayOutputStream baos = null ;
- try{
- baos = new ByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);
- }finally{
- try {
- if(baos != null)
- baos.close() ;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return bm ;
- }
- private static int calculateInSampleSize(BitmapFactory.Options options,
- int reqWidth, int reqHeight) {
- // Raw height and width of image
- final int height = options.outHeight;
- final int width = options.outWidth;
- int inSampleSize = 1;
- if (height > reqHeight || width > reqWidth) {
- // Calculate ratios of height and width to requested height and
- // width
- final int heightRatio = Math.round((float) height
- / (float) reqHeight);
- final int widthRatio = Math.round((float) width / (float) reqWidth);
- // Choose the smallest ratio as inSampleSize value, this will
- // guarantee
- // a final image with both dimensions larger than or equal to the
- // requested height and width.
- inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
- }
- return inSampleSize;
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。