Android图片压缩 内存优化 图片优化 质量压缩 尺寸压缩 native压缩

82 篇文章 1 订阅

Bitmap类

Bitmap.compress();

Write a compressed version of the bitmap to the specified outputstream. If this returns true, the bitmap can be 
reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream(). Note: not all Formats support all
 bitmap configs directly, so it is possible that the returned bitmap from BitmapFactory could be in a different bitdepth, 
 and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque pixels).
Params:
format – The format of the compressed image
quality – Hint to the compressor, 0-100. The value is interpreted differently depending on the Bitmap.CompressFormat.
stream – The outputstream to write the compressed data.
Returns:
true if successfully compressed to the specified stream.

public boolean compress(CompressFormat format, int quality, OutputStream stream) {
}

图片压缩有三种解决方案

质量压缩

/**
 * 改变图片的透明度,减小
 * @param context
 */
public static void qualityCompress(Context context){
    try {
        open = context.getAssets().open("pic1.jpg");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(open);

    int quality = 50;
    try {
        // PNG无损压缩 背景透明必须使用PNG格式,如果使用JPEG则透明背景为黑色
        // JPEG 有损压缩
        fos = new FileOutputStream(new File(context.getFilesDir(), String.valueOf(System.currentTimeMillis())+"_quality.jpeg"));
        bitmap.compress(Bitmap.CompressFormat.JPEG,quality, fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

压缩到100K

/**
     * 改变图片的透明度,减小
     *
     * @param context
     */
    public static void qualityCompressTo100K(Context context) {
        try {
            open = context.getAssets().open("pic1.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(open);

        //
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int quality = 60;
        try {
            // PNG无损压缩 背景透明必须使用PNG格式,如果使用JPEG则透明背景为黑色
            // JPEG 有损压缩
            fos = new FileOutputStream(new File(context.getFilesDir(), String.valueOf(System.currentTimeMillis()) + "_quality.jpeg"));
            Log.i(TAG, "starting compression");
            do {
                bos.reset();
                quality = quality - 5;
                bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
                Log.i(TAG, "bos.toByteArray().length:" + bos.toByteArray().length);
            } while ((bos.toByteArray().length > 1024 * 100 && quality > 5));
            fos.write(bos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

输出结果

2023-04-11 14:11:34.770 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: starting compression
2023-04-11 14:11:34.820 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:346061
2023-04-11 14:11:34.857 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:328705
2023-04-11 14:11:34.895 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:311990
2023-04-11 14:11:34.932 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:292827
2023-04-11 14:11:34.968 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:274492
2023-04-11 14:11:35.004 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:252523
2023-04-11 14:11:35.039 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:227915
2023-04-11 14:11:35.074 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:201450
2023-04-11 14:11:35.109 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:170260
2023-04-11 14:11:35.143 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:134575
2023-04-11 14:11:35.179 18135-18421/cn.jj.myapplication I/JJWorld.CompressUtils: bos.toByteArray().length:91796

尺寸压缩

Native压缩

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学知识拯救世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值