Android图片压缩、加水印

本文介绍了Android中两种图片压缩方法:指定图片文件大小压缩和指定图片宽高大小压缩。前者能有效减小文件体积,但可能导致失真,后者适用于生成缩略图,避免失真。同时,文章还讨论了如何添加图标和文字水印。
摘要由CSDN通过智能技术生成

指定图片文件大小压缩

实现

public Bitmap compressAndGenImage(Bitmap image, String outPath, int maxSize)
            throws IOException {
        Bitmap bitmap;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        // scale
        int options = 100;
        // Store the bitmap into output stream(no compress)
        image.compress(Bitmap.CompressFormat.JPEG, options, os);
        // Compress by loop
        while (os.toByteArray().length / 1024 > maxSize) {
            // Clean up os
            os.reset();
            // 每次质量下降10
            options -= 10;
            image.compress(Bitmap.CompressFormat.JPEG, options, os);
        }
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        bitmap = BitmapFactory.decodeStream(is);
        // Generate compressed image file
        FileOutputStream fos = new FileOutputStream(outPath);
        fos.write(os.toByteArray());
        fos.flush();
        fos.close();
        is.close();
        return bitmap;
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值