Android 图片压缩的方法大全

本文介绍了一种高效的图片压缩方法,通过调整图片的尺寸和质量来减少文件大小,适用于Android应用和其他需要压缩图片的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[java]  view plain copy
  1. public static Bitmap revitionImageSize(String path) throws IOException {  
  2.         BufferedInputStream in = new BufferedInputStream(new FileInputStream(  
  3.                 new File(path)));  
  4.         BitmapFactory.Options options = new BitmapFactory.Options();  
  5.         options.inJustDecodeBounds = true;  
  6.         BitmapFactory.decodeStream(in, null, options);  
  7.         in.close();  
  8.         int i = 0;  
  9.         Bitmap bitmap = null;  
  10.         while (true) {  
  11.             if ((options.outWidth >> i <= 1000)  
  12.                     && (options.outHeight >> i <= 1000)) {  
  13.                 in = new BufferedInputStream(  
  14.                         new FileInputStream(new File(path)));  
  15.                 options.inSampleSize = (int) Math.pow(2.0D, i);  
  16.                 options.inJustDecodeBounds = false;  
  17.                 bitmap = BitmapFactory.decodeStream(in, null, options);  
  18.                 break;  
  19.             }  
  20.             i += 1;  
  21.         }  
  22.         return bitmap;  
  23.     }  

图片按比例大小压缩方法(根据路径获取图片并压缩)

[java]  view plain copy
  1. public static Bitmap getimage(String srcPath)throws IOException{    
  2.         BitmapFactory.Options newOpts = new BitmapFactory.Options();    
  3.         //开始读入图片,此时把options.inJustDecodeBounds 设回true了    
  4.         newOpts.inJustDecodeBounds = true;    
  5.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空    
  6.         newOpts.inJustDecodeBounds = false;    
  7.         int w = newOpts.outWidth;    
  8.         int h = newOpts.outHeight;    
  9.         //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为    
  10.         float hh = 800f;//这里设置高度为800f    
  11.         float ww = 480f;//这里设置宽度为480f    
  12.         //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可    
  13.         int be = 1;//be=1表示不缩放    
  14.         if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放    
  15.             be = (int) (newOpts.outWidth / ww);    
  16.         } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放    
  17.             be = (int) (newOpts.outHeight / hh);    
  18.         }    
  19.         if (be <= 0)    
  20.             be = 1;    
  21.         newOpts.inSampleSize = be;//设置缩放比例    
  22.         //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了    
  23.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);    
  24.         return bitmap;//压缩好比例大小后再进行质量压缩    
  25.     }    
第三:图片按比例大小压缩方法(根据Bitmap图片压缩):

[java]  view plain copy
  1. public static Bitmap comp(Bitmap image) {    
  2.             
  3.         ByteArrayOutputStream baos = new ByteArrayOutputStream();           
  4.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);    
  5.         if( baos.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出      
  6.             baos.reset();//重置baos即清空baos    
  7.             image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中    
  8.         }    
  9.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());    
  10.         BitmapFactory.Options newOpts = new BitmapFactory.Options();    
  11.         //开始读入图片,此时把options.inJustDecodeBounds 设回true了    
  12.         newOpts.inJustDecodeBounds = true;    
  13.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);    
  14.         newOpts.inJustDecodeBounds = false;    
  15.         int w = newOpts.outWidth;    
  16.         int h = newOpts.outHeight;    
  17.         //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为    
  18.         float hh = 800f;//这里设置高度为800f    
  19.         float ww = 480f;//这里设置宽度为480f    
  20.         //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可    
  21.         int be = 1;//be=1表示不缩放    
  22.         if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放    
  23.             be = (int) (newOpts.outWidth / ww);    
  24.         } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放    
  25.             be = (int) (newOpts.outHeight / hh);    
  26.         }    
  27.         if (be <= 0)    
  28.             be = 1;    
  29.         newOpts.inSampleSize = be;//设置缩放比例    
  30.         //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了    
  31.         isBm = new ByteArrayInputStream(baos.toByteArray());    
  32.         bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);    
  33.         return bitmap;//压缩好比例大小后再进行质量压缩    
  34.     }    
根据路径获得突破并压缩返回bitmap用于显示

[java]  view plain copy
  1. public static Bitmap getSmallBitmap(String filePath) throws IOException{  
  2.           
  3.         final BitmapFactory.Options options = new BitmapFactory.Options();  
  4.         options.inJustDecodeBounds = true;  
  5.         BitmapFactory.decodeFile(filePath, options);  
  6.         options.inSampleSize = calculateInSampleSize(options, 480800);  
  7.         options.inJustDecodeBounds = false;  
  8.           
  9.         return BitmapFactory.decodeFile(filePath, options);  
  10.     }  
  11.     /** 
  12.      * 计算图片的缩放值 
  13.      *  
  14.      * @param options 
  15.      * @param reqWidth 
  16.      * @param reqHeight 
  17.      * @return 
  18.      */  
  19.     public static int calculateInSampleSize(BitmapFactory.Options options,  
  20.             int reqWidth, int reqHeight) {  
  21.         final int height = options.outHeight;  
  22.         final int width = options.outWidth;  
  23.         int inSampleSize = 1;  
  24.         if (height > reqHeight || width > reqWidth) {  
  25.             final int heightRatio = Math.round((float) height  
  26.                     / (float) reqHeight);  
  27.             final int widthRatio = Math.round((float) width / (float) reqWidth);  
  28.             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  29.         }  
  30.         return inSampleSize;  
  31.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值