bitmap 将图片压缩到指定的大小 JAVA

private void imageZoom() {  
                //图片允许最大空间   单位:KB  
                double maxSize =400.00;  
                //将bitmap放至数组中,意在bitmap的大小(与实际读取的原文件要大)    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                bitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
                byte[] b = baos.toByteArray();  
                //将字节换成KB  
                double mid = b.length/1024;  
                //判断bitmap占用空间是否大于允许最大空间  如果大于则压缩 小于则不压缩  
                if (mid > maxSize) {  
                        //获取bitmap大小 是允许最大大小的多少倍  
                        double i = mid / maxSize;  
                        //开始压缩  此处用到平方根 将宽带和高度压缩掉对应的平方根倍 (1.保持刻度和高度和原bitmap比率一致,压缩后也达到了最大大小占用空间的大小)  
                        bitMap = zoomImage(bitMap, bitMap.getWidth() / Math.sqrt(i),  
                                        bitMap.getHeight() / Math.sqrt(i));  
                }  
        }  

private void imageZoom() {  
                //图片允许最大空间   单位:KB  
                double maxSize =400.00;  
                //将bitmap放至数组中,意在bitmap的大小(与实际读取的原文件要大)    
                ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                bitMap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
                byte[] b = baos.toByteArray();  
                //将字节换成KB  
                double mid = b.length/1024;  
                //判断bitmap占用空间是否大于允许最大空间  如果大于则压缩 小于则不压缩  
                if (mid > maxSize) {  
                        //获取bitmap大小 是允许最大大小的多少倍  
                        double i = mid / maxSize;  
                        //开始压缩  此处用到平方根 将宽带和高度压缩掉对应的平方根倍 (1.保持刻度和高度和原bitmap比率一致,压缩后也达到了最大大小占用空间的大小)  
                        bitMap = zoomImage(bitMap, bitMap.getWidth() / Math.sqrt(i),  
                                        bitMap.getHeight() / Math.sqrt(i));  
                }  
        }  

 public static Bitmap resizeBitMapImage1(String filePath, int targetWidth,
            int targetHeight) {
        Bitmap bitMapImage = null;
        // First, get the dimensions of the image
        Options options = new Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        double sampleSize = 0;
        // Only scale if we need to
        // (16384 buffer for img processing)
        Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math
                .abs(options.outWidth - targetWidth);
            if (options.outHeight * options.outWidth * 2 >= 1638) {
            // Load, scaling to smallest power of 2 that'll get it <= desired
            // dimensions
            sampleSize = scaleByHeight ? options.outHeight / targetHeight
                    : options.outWidth / targetWidth;
            sampleSize = (int) Math.pow(2d,
                    Math.floor(Math.log(sampleSize) / Math.log(2d)));
        }
        // Do the actual decoding
        options.inJustDecodeBounds = false;
        options.inTempStorage = new byte[128];
        while (true) {
            try {
                options.inSampleSize = (int) sampleSize;
                bitMapImage = BitmapFactory.decodeFile(filePath, options);
                  break;
            } catch (Exception ex) {
                try {
                    sampleSize = sampleSize * 2;
                } catch (Exception ex1) {
                  }
            }
        }
        return bitMapImage;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值