压缩图片

来源:互联网 发布:西安软件公寓 编辑:程序博客网 时间:2024/05/22 11:56

使用:

Bitmap bitmap = compressBySize(picString,(int)Math.round(ScreenUtils.getScreenWidth(ToupicActivity.this)*0.6),(int)Math.round(ScreenUtils.getScreenHeight(ToupicActivity.this)*0.6));saveFile(bitmap, picString);
方法:

//压缩图片尺寸public Bitmap compressBySize(String pathName, int targetWidth,int targetHeight) {BitmapFactory.Options opts = new BitmapFactory.Options();opts.inJustDecodeBounds = true;// 不去真的解析图片,只是获取图片的头部信息,包含宽高等;Bitmap bitmap = BitmapFactory.decodeFile(pathName, opts);// 得到图片的宽度、高度;float imgWidth = opts.outWidth;float imgHeight = opts.outHeight;// 分别计算图片宽度、高度与目标宽度、高度的比例;取大于等于该比例的最小整数;int widthRatio = (int) Math.ceil(imgWidth / (float) targetWidth);int heightRatio = (int) Math.ceil(imgHeight / (float) targetHeight);opts.inSampleSize = 1;if (widthRatio > 1 || widthRatio > 1) {if (widthRatio > heightRatio) {opts.inSampleSize = widthRatio;} else {opts.inSampleSize = heightRatio;}}// 设置好缩放比例后,加载图片进内容;opts.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeFile(pathName, opts);return bitmap;}//存储进SD卡public void saveFile(Bitmap bm, String fileName) throws Exception {File dirFile = new File(fileName);// 检测图片是否存在if (dirFile.exists()) {dirFile.delete(); // 删除原图片}File myCaptureFile = new File(fileName);BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));// 100表示不进行压缩,70表示压缩率为30%bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);bos.flush();bos.close();}



0 0
原创粉丝点击