关于图片压缩
来源:互联网 发布:立体模拟软件 编辑:程序博客网 时间:2024/05/17 02:00
最近项目中用到图片压缩,总结一下,要不时间长忘得快.
看来看去也就两种方法,一种尺寸压缩,一种质量压缩.
先看下代码,尺寸压缩
public static Bitmap resetImgSize(Bitmap bitMap, int size) { int width = bitMap.getWidth(); int height = bitMap.getHeight(); // 设置想要的大小// int newWidth = 200;// int newHeight = 200; int newWidth = size; int newHeight = size; // 计算缩放比例 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片 bitMap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true); return bitMap; }
通过设置的宽度和高度和图片本身的宽高,得到想要比例,然后通过矩阵缩放,创建新图片.
质量压缩
public static Bitmap compressBmpFromBmp(Bitmap image) {// BitmapFactory.Options op = new BitmapFactory.Options();// op.inPreferredConfig = Bitmap.Config.RGB_565; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 100; image.compress(Bitmap.CompressFormat.JPEG, 100, baos); while (baos.toByteArray().length > 1024 * 100 && options > 10) { baos.reset(); options -= 10; image.compress(Bitmap.CompressFormat.JPEG, options, baos); } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; }
主要通过bitmap.compress()方法进行压缩,第一个参数格式,第二个压缩比例0–100,100代表不压缩, 第三个参数是一个输出流,其中的数据被写入一个 byte 数组.
0 0
- Android关于图片压缩
- android 关于图片压缩
- 关于图片压缩
- 关于图片压缩
- 关于图片压缩
- 关于图片压缩
- Android关于图片压缩
- 关于图片压缩的总结
- 关于图片压缩上传问题;
- Android 关于图片的压缩
- 关于IOS中压缩图片
- iOS - 关于图片压缩处理
- 关于图片压缩的实现
- 关于android自带的图片压缩
- 关于iOS上图图片压缩问题解决办法
- Android中关于图片压缩的小结
- Android关于图片如何压缩上传
- 关于图片压缩的三两句话
- iOS播放远程网络音乐的核心技术点
- Android4.1添加新的按键(3)
- (23)成员访问运算符、条件运算符、逗号运算符
- 关于MyBatis一些小错误,元素内容必须由格式正确的字符数据或标记组成.
- Visual Studio制作安装程序教程
- 关于图片压缩
- Android4.1添加新的按键(4)
- 【VB6】实现VB6中类的静态方法
- linux下离线安装tesseract-ocr
- 编程中无穷大的量的使用技巧
- 读取手机联系人
- CintaNotes:怎么恢复误删的笔记
- uva10881 Piotr's Ants
- abap中利用se95还原程序到原始版本