图片文件压缩(保真并不超过100KB)

前言:
在android开发中,图片的处理很常见,在获取到拍照或者代码生成的图片,通常会因为图片所占的空间过大而出现OOM,今天给大家共享一种最常见,也是最简单直接的图片压缩方法,不管你的手机像素有多高,图片有多大,最终压缩的图片只有100kb以内,下面直接展示代码:

public class FileCompressUtils {

    @SuppressLint("NewApi")
    public boolean compressImgFile(String path) {
        try {
            Bitmap bitmap = comp(BitmapFactory.decodeFile(path));
            //压缩结束后,将原来的原图删除
            new File(path).delete();
            //创建和将压缩后最新的bitmap写入保存
            File newFile = new File(path);
            FileOutputStream fileOutputStream = new FileOutputStream(newFile);
            压缩好比例大小后再进行质量压缩继续压缩70%
            bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream);
            fileOutputStream.close();
            bitmap.recycle();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @SuppressLint("NewApi")
    private Bitmap comp(Bitmap image) {
        //质量压缩法:
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 70, baos);
        //判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
        if (baos.toByteArray().length / 1024 > 1024) {
            baos.reset();//重置baos即清空baos
            //这里压缩50%,把压缩后的数据存放到baos中
            image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        }

        //图片按比例大小压缩方法(根据路径获取图片并压缩):
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //开始读入图片,此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;

        //这里设置高度为800f
        float hh = 800f;
        //这里设置宽度为480f
        float ww = 480f;
        //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;//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.RGB_565;//降低图片从ARGB888到RGB565
        //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        isBm = new ByteArrayInputStream(baos.toByteArray());
        bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
        try {
            isBm.close();
            baos.close();
            image.recycle();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
}

使用方法:

FileCompressUtils fileCompressUtils = new FileCompressUtils();
boolean compressImgFile =fileCompressUtils.compressImgFile("/sdcard/tmp.jpeg");
Log.d("compressImgFile","压缩是否成功:"+compressImgFile);

目前,这个方法有一个小问题,就是利用bitmap的compress()方法会导致压缩时间上延迟变长1到2秒钟,如果你有好的建议或者方法请给我留言哦! ^_^!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值