Android 压缩图片

图片压缩技巧
本文介绍了一种图片压缩的方法,包括如何按指定尺寸缩放图片及如何通过调整图片质量来进一步减小文件大小。此方法适用于需要优化图片占用空间的应用场景。

   先压缩到指定尺寸,再压缩到指定大小

     /**

     * 计算图片的缩放值
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int width = options.outWidth;
        final int height = options.outHeight;
        Log.d(TAG ,"source height=" + height + " width=" + width);
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
                 final int heightRatio = Math.round((float) height/ (float) reqHeight);
                 final int widthRatio = Math.round((float) width / (float) reqWidth);
                 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        Log.d(TAG ,"inSampleSize=" + inSampleSize);
        return inSampleSize;
    }
    
    /**
     * 按质量压缩
     * @param image
     * @return
     */
    public static void compressImageByQuality(Bitmap image, String outPath) throws Exception{
        File file = new File(outPath);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中  
        int options = 90;  
        while (baos.toByteArray().length > (1024L * 100L)) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩         
            baos.reset();
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中  
            options -= 10;
            if(options < 0){
                break;
            }          
        }  
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        FileOutputStream os = new FileOutputStream(file);
        os.write(baos.toByteArray());
        os.close();    
    }  
    

    
    /**
     * 从文件压缩
     * @param srcPath
     * @return
     */
    public static void compressImageFromFile(String srcPath, String outPath, int reqWidth, int reqHight) throws Exception{  
        Log.d(TAG , "srcPath=" + srcPath + "\n outPath=" + outPath + "\n reqWidth=" + reqWidth + " reqHight=" + reqHight);
        try {
            BitmapFactory.Options newOpts = new BitmapFactory.Options();  
            newOpts.inJustDecodeBounds = true;//只读边,不读内容  
            Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
            newOpts.inSampleSize = calculateInSampleSize(newOpts, reqWidth, reqHight);
            newOpts.inJustDecodeBounds = false;  
            newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设  
            newOpts.inPurgeable = true;// 同时设置才会有效  
            newOpts.inInputShareable = true;//当系统内存不够时候图片自动被回收  
            bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
            compressImageByQuality(bitmap, outPath);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("Image compress error, by " + e.getMessage());
        }
    } 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值