美文网首页
图片压缩

图片压缩

作者: on_i_on | 来源:发表于2019-12-02 16:14 被阅读0次
方法一 compress
        // 资源文件转成bitmap
        Bitmap bitmapTest = BitmapFactory.decodeResource(getResources(), R.mipmap.test);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        //100%压缩
        bitmapTest.compress(Bitmap.CompressFormat.JPEG, 100, output);
        //图片字节程度长度
        int lengthBefore = output.toByteArray().length;
        L.showError("lengthBefore====" + lengthBefore);
        ivBefore.setImageBitmap(bitmapTest);
        //将图片保存到本地sd卡
        String pathBefore = fileUtils.saveImage(bitmapTest);
        //获取sd卡图片获取图片字节大小
        long fileLengthBefore = fileUtils.getImagesAbsolutePathFile(pathBefore).length();
        L.showError("fileLengthBefore====" + fileLengthBefore);
        //重置大小
        output.reset();
        //1%压缩
        bitmapTest.compress(Bitmap.CompressFormat.JPEG, 1, output);
        //1%压缩之后图片的大小
        int lengthAfter = output.toByteArray().length;
        L.showError("lengthAfter====" + lengthAfter);
        // 解码生成图片
        Bitmap bitmapAfter = BitmapFactory.decodeByteArray(output.toByteArray(), 0, output.toByteArray().length);
        ivAfter.setImageBitmap(bitmapAfter);
        String pathAfter = fileUtils.saveImage(bitmapAfter);
        // 重置
        output.reset();
        // 解码生成图片的大小
        bitmapAfter.compress(Bitmap.CompressFormat.JPEG, 100, output);
        L.showError("output.toByteArray().length====" + output.toByteArray().length);
        //获取sd卡图片获取图片字节大小
        long fileLengthAfter = fileUtils.getImagesAbsolutePathFile(pathAfter).length();
        L.showError("fileLengthAfter====" + fileLengthAfter);

打印出来的日志如下。
2019-12-02 15:17:02.089 861-861/com.cc.studyall E/studydemo: lengthBefore====5219019
2019-12-02 15:17:02.474 861-861/com.cc.studyall E/studydemo: fileLengthBefore====5219019
2019-12-02 15:17:02.673 861-861/com.cc.studyall E/studydemo: lengthAfter====115186
2019-12-02 15:17:03.242 861-861/com.cc.studyall E/studydemo: output.toByteArray().length====302535
2019-12-02 15:17:03.243 861-861/com.cc.studyall E/studydemo: fileLengthAfter====302535

画质也出现很明显的差别。

缩放图片大小
    // 资源文件转成bitmap
    Bitmap bitmapTest = BitmapFactory.decodeResource(getResources(), R.mipmap.test);
    mIvBefore.setImageBitmap(bitmapTest);
    //将图片保存到本地sd卡
    String filePath = mFileUtils.saveImage(bitmapTest);
    // 根据路径获得突破并压缩返回bitmap用于显示
        try {
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeFile(filePath, options);
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, ConvertUtils.dip2px(this,120), ConvertUtils.dip2px(this,120));
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            Bitmap bitmap =  BitmapFactory.decodeFile(filePath, options);
            L.showError("bitmap.getWidth()====" + bitmap.getWidth() + "bitmap.getHeight()====" + bitmap.getHeight());
            mIvAfter.setImageBitmap(bitmap);
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            //100%压缩
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
            //图片字节程度长度
            int lengthDecode = output.toByteArray().length;
            L.showError("lengthDecode====" + lengthDecode);
            File file = mFileUtils.getImagesTempFile(System.currentTimeMillis() + ".jpg");
            OutputStream outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        } catch (Exception e) {
            L.showError(e.toString());
        }

// 计算图片的缩放值int reqWidth, int reqHeight这两个参数表示图片希望压缩到多大
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;//原图片的高度
    final int width = options.outWidth;//原图片的宽度
    int inSampleSize = 1;
    L.showError("options.outWidth====" + options.outWidth + "options.outHeight====" + options.outHeight);
    if (height > reqHeight || width > reqWidth) {
        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);//四舍五入, 原图高度除以希望压缩到的高度
        final int widthRatio = Math.round((float) width / (float) reqWidth);//四舍五入, 原图宽度除以以往压缩到的宽度
        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;//选择较小的那个比例, 保证宽和高压缩后的大小都不会小于期望的大小
    }
    return inSampleSize;
}
打印日志如下:
2019-12-02 16:05:40.353 11095-11095/com.cc.studyall E/studydemo:   options.outWidth====4896options.outHeight====3672
2019-12-02 16:05:40.479 11095-11095/com.cc.studyall E/studydemo: bitmap.getWidth()====489bitmap.getHeight()====367
2019-12-02 16:05:40.485 11095-11095/com.cc.studyall E/studydemo: lengthDecode====105259
compress压缩图片对比.jpg 比例压缩.jpg

相关文章

  • 图片压缩组件

    图片压缩 图片压缩

  • iOS 图片压缩方法

    两种图片压缩方法 两种图片压缩方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 通过...

  • iOS 图片压缩限制大小最优解

    iOS 图片压缩限制大小最优解 图片的两种压缩方法 1.1 压缩图片质量 1.2 压缩图片尺寸 压缩图片使图片文件...

  • iOS 图片压缩限制大小最优解

    概要: 图片的两种压缩方法1.1 压缩图片质量1.2 压缩图片尺寸压缩图片使图片文件小于指定大小2.1 压缩图片质...

  • iOS 图片压缩限制大小

    一、两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量...

  • iOS 图片压缩方法

    两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 N...

  • 图片压缩方法

    两种图片压缩方法 压缩图片质量(quality)、压缩图片尺寸(size) 压缩图片质量 或 前者可以控制压缩比例...

  • 图片懒加载之高斯模糊

    压缩原始图片 将原始图片压缩至1~2kb甚至更小的图片nature.jpg 压缩 java 图片压缩natur...

  • iOS 图片压缩方法

    图片压缩可以通过两种方式实现,压缩图片质量和压缩图片尺寸。如果对图片清晰度有要求的,可以先压缩图片质量,在压缩图片...

  • iOS 图片压缩方法

    iOS 图片压缩方法 更多图片处理方法见图片组件 BBWebImage iOS 图片压缩方法 两种图片压缩方法 两...

网友评论

      本文标题:图片压缩

      本文链接:https://www.haomeiwen.com/subject/avffgctx.html