/** * 按比例压缩 */ 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; }
图片压缩按比例压缩
最新推荐文章于 2022-12-29 23:48:56 发布