图片压缩--质量压缩法&边界压缩法

最好的实现方式应该是app告诉服务端想要的图片尺寸,然后服务端获取图片再将压缩后的图片返回给app,这样既节省了用户流量又降低了app的内存耗费
/**
 * 压缩图片-质量压缩法
 *
 * @param image 压缩前的bitmap
 * @param kb    限制压缩后bitmap的大小,单位kb
 */
public static Bitmap compressImage(Bitmap image, int kb) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    int options = 100;
    while (baos.toByteArray().length / 1024 > kb) {    //循环判断如果压缩后图片是否大于kb,大于继续压缩
        baos.reset();//重置baos即清空baos
        if (options <= 10) {
            options -= 3;//每次压缩3%
        } else {
            options -= 10;//每次压缩10%
        }
        if (options <= 0) break;
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
    return BitmapFactory.decodeStream(isBm, null, null);

}

  /**
     * 从图片路径获取图片并压缩-边界压缩法
     *
     * @param srcPath 图片路径
     */
    public static Bitmap compressImageFromFile(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;//只读边,不读内容
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//只获取边界信息,此时返回bitmap为null
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = 1920f;
        float ww = 1080f;
        int be = 1;
        if (w > h && w > ww) {
            be = (int) (newOpts.outWidth / ww);
        } else if (w < h && h > hh) {
            be = (int) (newOpts.outHeight / hh);
        }
        if (be <= 0) be = 1;
        newOpts.inSampleSize = be;//设置采样率,缩放后图片是原图片大小的几分之几

        newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//该模式是默认的,可不设
        newOpts.inPurgeable = true;// 同时设置才会有效
        newOpts.inInputShareable = true;//当系统内存不够时候图片自动被回收
        newOpts.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(srcPath, newOpts);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值