图片压缩按比例压缩

    /**
     * 按比例压缩
     */
    public Bitmap zipImageChiCun(String imgPath){
        //创建图片的参数,用来获取图片的信息
        BitmapFactory.Options options = new BitmapFactory.Options();
        // 开始读入图片,此时把options.inJustDecodeBounds 设回true,只读入尺寸
        options.inJustDecodeBounds = true;
            //读取图片的尺寸
            Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
            options.inJustDecodeBounds = false;//正式读取图片,供后面使用
            //得到图片的尺寸
            int w = options.outWidth;//得到宽
            int h = options.outHeight;//得到高
            //设置按尺寸压缩,要压缩到的分辨率
            float x = 100;//宽
            float y = 200;//高
            int be = 1;//缩放比例,1表示不缩放
            if(w > h && w>x){//按宽缩放
                be = (int)(w/x);//计算缩放比例
            }else{
                be = (int)(h/y);//计算缩放比例
            }
            if(be <= 0){
            be = 1;
        }
        options.inSampleSize = be;//设置缩放比例
        //按计算好的尺寸,处理图片,按尺寸压缩
        Bitmap bitmap1 = BitmapFactory.decodeFile(imgPath,options);
        return bitmap1;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值