android 图片压缩

大致思路,根据后台上传图片限制,自己对图片进行处理

一般后台会限制图片宽高,对于超出限制的图片要进行宽高比压缩(减少图片的宽高) 核心代码

  BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;//只读大小,不读数据
        BitmapFactory.decodeFile(path, op);
        int height = op.outHeight;
        int width = op.outWidth;
  LogUtil.e("scal_from_size" + outputFile.length() + "宽度为" + height + "高度为" + height, PhotoUtil.class);
  Bitmap bitmap = null;
  while (height > maxHeight || width > maxWidth) {// 如果宽度大的话根据宽度固定大小缩放
                double scaleH = (float) height / maxHeight;
                double scaleW = (float) width / maxWidth;
                options.inSampleSize = (int) Math.ceil(Math.max(scaleH, scaleW)); // 1表示不缩放
                bitmap = BitmapFactory.decodeFile(outputFile.getPath(), options);
                LogUtil.e("宽度压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight +
                        "scal_ok_size" + outputFile.length(), PhotoUtil.class);
            }

当图片大小大于服务器限制大小时就要进行质量压缩

try {
    while (fileSize >= fileMaxSize) {//如果图片大小 > 服务器要求大小
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(path, options);
        fos = new FileOutputStream(outputFile);
        quality -= 10;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
        fos.close();
        fileSize = outputFile.length();
        LogUtil.e("采样率压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight
                + "scal_ok_size" + outputFile.length(), PhotoUtil.class);
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
   全部代码
  static final int maxHeight = 1023;
    static final int maxWidth = 2047;
    static long fileMaxSize = 1024 * 1024;
    
    /**
     * 压缩图片
     *
     * @param path 图片地址
     * @return 压缩后的图片地址
     */
    public static File scal(String path, int scalSize) {
        
        File outputFile = new File(path);
        long fileSize = outputFile.length();
        long fileMaxSize = scalSize;
        BitmapFactory.Options op = new BitmapFactory.Options();
        op.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, op);
        int height = op.outHeight;
        int width = op.outWidth;
        LogUtil.e("scal_from_size" + outputFile.length() + "宽度为" + height + "高度为" + height, PhotoUtil.class);
        Bitmap bitmap = null;
        if (fileSize >= fileMaxSize || height > maxHeight || width > maxWidth) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            while (height > maxHeight || width > maxWidth) {// 如果宽度大的话根据宽度固定大小缩放
                double scaleH = (float) height / maxHeight;
                double scaleW = (float) width / maxWidth;
                options.inSampleSize = (int) Math.ceil(Math.max(scaleH, scaleW)); // 1表示不缩放
                BitmapFactory.decodeFile(outputFile.getPath(), options);
                LogUtil.e("宽度压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight + "scal_ok_size" + outputFile.length(), PhotoUtil.class);
            }
            FileOutputStream fos = null;
            int quality = 100;
            try {
                while (fileSize >= fileMaxSize) {//如果图片大小 > 服务器要求大小
                    options.inJustDecodeBounds = false;
                    bitmap = BitmapFactory.decodeFile(path, options);
                    fos = new FileOutputStream(outputFile);
                    quality -= 10;
                    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);
                    fos.close();
                    fileSize = outputFile.length();
                    LogUtil.e("采样率压缩后 宽度为" + options.outWidth + "高度为" + options.outHeight
                            + "scal_ok_size" + outputFile.length(), PhotoUtil.class);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return outputFile;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值