图片等比例缩放

图片的等比例缩放,第一个参数是图片路径,第二个是最终所需要图片的(宽高里取值最大的)的最大值// 限制值MaxSize*(2/3)=实际使用值的比较值IMAGE_MAX_SIZE // 例如:限制图片大小为400,则实际使用的比较值应为400*(2/3) // 260*2/3=390 public static Bitmap decodeFile(String path, int MaxSize) { File f = new File(path); int IMAGE_MAX_SIZE = MaxSize * 2 / 3; Bitmap b = null; try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; //FileInputStream fis = new FileInputStream(f); //BitmapFactory.decodeStream(fis, null, o); //fis.close(); double scale = 1; if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { scale = Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = (int) scale; FileInputStream fis = new FileInputStream(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (FileNotFoundException e) { } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值