图片压缩小结

1. 图片压缩1

     /**
     * 图片按比例压缩方法一
     * @param filePath 文件路径
     * @param outWidth 输出文件宽度
     * @param outHeight 输出文件高度
     * @return
     */
    public static Bitmap compressImageByScale(String filePath, float outWidth, float outHeight,int limitMaxSize){

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        BitmapFactory.decodeFile(filePath, options);

        options.inSampleSize = countInSampleSize(options,outWidth,outHeight);
        options.inJustDecodeBounds = false;

        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

        return bitmap;
    }

2. 计算图片比例的缩放值

    /**
     * 计算图片比例的缩放值
     * @param options
     * @param outWidth
     * @param outHeight
     * @return
     */
    public static int countInSampleSize(BitmapFactory.Options options,float outWidth,float outHeight){
        //获取原始图片宽高
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        int inSampleSize = 1;
        // 如果宽度大的话根据宽度固定大小缩放  否则根据高度固定大小缩放
        if(srcWidth > outWidth && srcWidth > srcHeight){
            inSampleSize = (int) (srcWidth /  outWidth);
        }else if(srcHeight > outHeight && srcHeight > srcWidth){
            inSampleSize = (int) (srcHeight /  outHeight);
        }
        return inSampleSize < 0 ? 1 : inSampleSize;
    }

3. 图片压缩2

     /**
     * 图片按比例压缩方法二 输出文件宽高写死
     * @param filePath 文件路径
     * @return
     */
    public static Bitmap compressImageByScale(String filePath,int limitMaxSize){
        float outWidth = 720f;
        float outHeight = 1280f;
        return compressImageByScale(filePath,outWidth,outHeight,limitMaxSize);
    }

4. 图片压缩3

    /**
     * 图片按比例压缩方法三
     * @param resId  文件资源id
     * @param outWidth 输出文件宽度
     * @param outHeight 输出文件高度
     * @return
     */
    public static Bitmap compressImageByScale(Context context,int resId, float outWidth, float outHeight,int limitMaxSize){

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        BitmapFactory.decodeResource(context.getResources(), resId, options);

        options.inSampleSize = countInSampleSize(options,outWidth,outHeight);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId, options);
        return bitmap;
    }

5. 图片压缩4

/**
     * 图片按比例压缩方法四
     * 输出文件宽高写死
     * @param resId  文件资源id
     * @return
     */
    public static Bitmap compressImageByScale(Context context,int resId,int limitMaxSize){
        float outWidth = 720f;
        float outHeight = 1280f;
        return compressImageByScale(context,resId,outWidth,outHeight,limitMaxSize);
    }

6. 质量压缩 不过没啥用 感觉

 /**
     * 图片质量压缩
     * @param bitmap
     * @param limitMaxSize
     * @return
     */
    public static Bitmap compressImageByQuality(Bitmap bitmap, int limitMaxSize){
        //进行有损压缩
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int options = 100; //0-100 100表示不压缩
        bitmap.compress(Bitmap.CompressFormat.JPEG,options,baos);
        //循环判断如果压缩后图片是否大于limitMaxSize,大于继续压缩
        int baosLength = baos.toByteArray().length;
        while ((baosLength / 1024) > limitMaxSize){
            //重置baos即让下一次的写入覆盖之前的内容
            baos.reset();
            options = options - 10 > 0 ? options - 10 : 0;
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);
            if(options == 0){
                break;
            }
        }
        Log.e("compressImage options",options+"");
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap outBitmap = BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片
        return outBitmap;
    }

7. 质量压缩与图片压缩结合 效果最好

 /**
     * 质量压缩结合比例压缩
     * @param bitmap
     * @return
     */
    public static Bitmap compressImageByScale(Bitmap bitmap){

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
        //判断如果图片大于1M,进行压缩避免在生成图片
        if(baos.toByteArray().length / 1024 > 1024){
            baos.reset();
            //这里压缩60%,把压缩后的数据存放到baos中
            bitmap.compress(Bitmap.CompressFormat.JPEG,60,baos);
        }

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        BitmapFactory.Options options = new BitmapFactory.Options();
        //开始读入图片,此时把options.inJustDecodeBounds 设回true了
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(bais,null,options);
        int srcWidth = options.outWidth;
        int srcHeight = options.outHeight;
        //目标宽度和高度
        float outWidth = 720f;
        float outHeight = 1280f;
        //压缩比例
        int scale = 1;
        if(srcWidth > outWidth && srcWidth > srcHeight){
            scale = (int) (srcWidth / outWidth);
        }else if(srcHeight > outHeight && srcHeight > srcWidth){
            scale = (int) (srcHeight / outHeight);
        }
        if(scale < 0) {
            scale = 1;
        }

        options.inSampleSize = scale;
        options.inJustDecodeBounds = false;
        //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
        bais = new ByteArrayInputStream(baos.toByteArray());
        Bitmap outBitmap = BitmapFactory.decodeStream(bais, null, options);
        return outBitmap;
    }

8. bitmap与String互相转换

 /**
     * 把bitmap转换成String
     * @param bitmap
     * @return
     */
    public static String bitmapToString(Bitmap bitmap) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] b = baos.toByteArray();
        return Base64.encodeToString(b, Base64.DEFAULT);
    }

    /**
     * 把String转Bitmap
     * @param str
     * @return
     */
    public static Bitmap stringToBitmap(String str){
        Bitmap bitmap = null;
        try{
            byte[] bitmapArray;
            bitmapArray = Base64.decode(str, Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
                            bitmapArray.length);
        }catch (Exception e) {

        }finally {
            return bitmap;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值