Android图片压缩

android 图片压缩包括裁剪跟压缩可以分为4种
1:采样率压缩
采样率压缩是通过采样比率间隔读取图片像素达到减少图片占用内存
是android唯一一种可以在读取图片时就减少内存占用的方式,读取大图时必须要用
可以减小图片像素;
eg:

    /**
     * @param srcPath
     * @return
     */
    private Bitmap getimage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 开始读入图片,此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        // 此时返回bm为空
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        // 现在主流手机比较多是720*1280分辨率,所以高和宽我们设置为
        float hh = 1280f;// 这里设置高度为1280f
        float ww = 720f;// 这里设置宽度为720f
        // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
        int be = 1;// be=1表示不缩放
        // if (w > h && w > ww) {
        // 如果宽度大的话根据宽度固定大小缩放
        be = (int) (w/ ww);
         } else if (w < h && h > hh) {
            // 如果高度高的话根据宽度固定大小缩放
         be = (int) (newOpts.outHeight / hh);
         }
        if (be <= 1)
            be = 1;
        newOpts.inSampleSize = be;// 设置缩放比例
        // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return bitmap;
    }

2:质量压缩:
质量压缩主要是通过相同像素填充附近类似像素达到占用少内存,不减少图片像素

 /**
     * 压缩图片质量,返回指定大小的图片
     * @param image
     * @return
     */
    private Bitmap compressImage(Bitmap image,long length) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length> length) { // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();// 重置baos即清空baos
            options -= 10;// 每次都减少10
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 这里压缩options%,把压缩后的数据存放到baos中

        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
        return bitmap;
    }

3:裁剪
裁剪通过矩阵,缩放原图长宽 达到节省内存,可以实际调整图片像素

 /***
     *精确截取图片方法
     * 
     * @param bgimage
     *            :源图片资源
     * @param maxL
     *            缩放后的最大长度/高度

     * @return
     */
    public static Bitmap zoomImage(Bitmap bgimage,int maxL) {
            // 获取这个图片的宽和高
            float width = bgimage.getWidth();
            float height = bgimage.getHeight();
            // 计算宽高缩放率
            float scaleWidth = 1.0f;
            float scaleHeight = 1.0f;
            if (width>=height&&width>maxL) {
                scaleWidth=maxL/width;
                int newH=(int) (height*maxL/width);
                scaleHeight=newH/height;
            }else if (height>=width&&height>maxL) {
                scaleHeight=maxL/height;
                int newW=(int) (width*maxL/height);
                scaleWidth=newW/width;
            }else {
                return bgimage;
            }
            // 创建操作图片用的matrix对象
            Matrix matrix = new Matrix();
            // 缩放图片动作
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
                            (int) height, matrix, true);
            return bitmap;
    }
}

4:NDK 调用JPEG开源c库压缩,android 默认的压缩引擎是阉割版的jpg,压缩率没有原版的好,可以通过ndk调用原生jpg库 进行图片压缩
好处:压缩率高,在图片上传时非常适用
缺陷:读取图片内存占用一样大,需要NDK跟C库支持,使项目变复杂.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值