压缩图片
来源:互联网 发布:西安软件公寓 编辑:程序博客网 时间: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
- 图片压缩。大小压缩
- 图片压缩按比例压缩
- 压缩图片
- 压缩图片
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 压缩图片
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 压缩图片
- 图片压缩
- MongoDB学习 (四):创建、读取、更新、删除(CRUD)快速入门
- 入学考试
- openGL es2.0 创建和管理shader
- SQL update select语句及应用
- reids客户端 redis-cli用法
- 压缩图片
- ORACLE 数据库名、实例名、ORACLE_SID的区别
- hdu 1102 uva 10397(最小生成树prim)
- HDU 4198 Quick out of the Harbour(优先队列 + bfs)
- Java 泛型小结
- 修改MYSQL提示符
- MongoDB学习 (五):查询操作符(Query Operators).1st
- Object.clone()方法
- 浏览器不同语言切换不同页面