ANDROID图片压缩代码

  2 
  3 import java.io.FileDescriptor;
  4 import java.io.FileInputStream;
  5 
  6 import android.graphics.Bitmap;
  7 import android.graphics.BitmapFactory;
  8 
  9 /**
 10  * 图片处理类   压缩
 11  * 
 12  * @author Administrator
 13  * 
 14  */
 15 public class ImageUtil {
 16     /**
 17      * get Bitmap
 18      * 
 19      * @param imgPath
 20      * @param minSideLength
 21      * @param maxNumOfPixels
 22      * @return
 23      */
 24     public static Bitmap getBitmap(String imgPath, int minSideLength,
 25             int maxNumOfPixels) {
 26         if (imgPath == null || imgPath.length() == 0)
 27             return null;
 28 
 29         try {
 30             FileDescriptor fd = new FileInputStream(imgPath).getFD();
 31             BitmapFactory.Options options = new BitmapFactory.Options();
 32             options.inJustDecodeBounds = true;
 33             // BitmapFactory.decodeFile(imgFile, options);
 34             BitmapFactory.decodeFileDescriptor(fd, null, options);
 35 
 36             options.inSampleSize = computeSampleSize(options,
 37                     minSideLength, maxNumOfPixels);
 38             try {
 39                 // 这里一定要将其设置回false,因为之前我们将其设置成了true
 40                 // 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度
 41                 options.inJustDecodeBounds = false;
 42 
 43                 Bitmap bmp = BitmapFactory.decodeFile(imgPath, options);
 44                 return bmp == null ? null : bmp;
 45             } catch (OutOfMemoryError err) {
 46                 return null;
 47             }
 48         } catch (Exception e) {
 49             return null;
 50         }
 51     }
 52 
 53     /**
 54      * compute Sample Size
 55      * 
 56      * @param options
 57      * @param minSideLength
 58      * @param maxNumOfPixels
 59      * @return
 60      */
 61     private static  int computeSampleSize(BitmapFactory.Options options,
 62             int minSideLength, int maxNumOfPixels) {
 63         int initialSize = computeInitialSampleSize(options, minSideLength,
 64                 maxNumOfPixels);
 65 
 66         int roundedSize;
 67         if (initialSize <= 8) {
 68             roundedSize = 1;
 69             while (roundedSize < initialSize) {
 70                 roundedSize <<= 1;
 71             }
 72         } else {
 73             roundedSize = (initialSize + 7) / 8 * 8;
 74         }
 75 
 76         return roundedSize;
 77     }
 78 
 79     /**
 80      * compute Initial Sample Size
 81      * 
 82      * @param options
 83      * @param minSideLength
 84      * @param maxNumOfPixels
 85      * @return
 86      */
 87     private static  int computeInitialSampleSize(BitmapFactory.Options options,
 88             int minSideLength, int maxNumOfPixels) {
 89         double w = options.outWidth;
 90         double h = options.outHeight;
 91 
 92         // 上下限范围
 93         int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
 94                 .sqrt(w * h / maxNumOfPixels));
 95         int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
 96                 Math.floor(w / minSideLength), Math.floor(h / minSideLength));
 97 
 98         if (upperBound < lowerBound) {
 99             // return the larger one when there is no overlapping zone.
100             return lowerBound;
101         }
102 
103         if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
104             return 1;
105         } else if (minSideLength == -1) {
106             return lowerBound;
107         } else {
108             return upperBound;
109         }
110     }
111 }

 

转载于:https://www.cnblogs.com/afluy/p/3343742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android图片压缩工具类是一种用于压缩Android应用中的图片文件的工具。根据引用和引用的内容,这个工具类的具体实现可能包括以下功能: - 通过将图片文件转换为字节数组,以便进行后续的压缩处理。 - 使用缓冲输出流将压缩后的图片数据写入输出流中。 - 通过设置合适的压缩参数,对图片进行压缩操作,减小文件大小但尽量保持良好的图像质量。 - 可能还包括一些其他的图像处理操作,如旋转、裁剪等。 具体的实现细节可以参考引用和引用提供的代码示例。在这些示例中,使用了ByteArrayInputStream和ByteArrayOutputStream类来处理字节数组的输入和输出。通过设置合适的压缩参数,可以实现对图片的压缩操作。此外,还可以使用BufferedOutputStream类来提高输出流的写入性能。 如果想要进一步了解关于Android图片压缩工具类的使用和实现细节,可以参考引用提供的配套资料,该资料可能提供了更详细的解释和示例代码。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android开发之图片压缩工具类完整实例](https://download.csdn.net/download/weixin_38517904/14018839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [android图片压缩工具类分享](https://download.csdn.net/download/weixin_38616330/14881444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Spring Boot(六十四):SpringBoot集成Gzip压缩数据](https://download.csdn.net/download/u013938578/88221156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值