09-16 压缩图片
来源:互联网 发布:apache tomcat怎么解决 编辑:程序博客网 时间:2024/04/28 23:49
压缩图片
public class ZipImage {
public static void zipImage(String savePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savePath, options);
options.inSampleSize = computeInitialSampleSize(options, 480, 480 * 960);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(savePath, options);
try {
FileOutputStream fos = new FileOutputStream(savePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
bitmap.recycle();
bitmap = null;
System.gc();
}
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math .sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength), Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; }}
}
- 09-16 压缩图片
- 图片压缩。大小压缩
- 图片压缩按比例压缩
- 压缩图片
- 压缩图片
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 压缩图片
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 压缩图片
- iOS UITableViewCell 多线程 网络+沙盒的图片加载以及第三方框架SDWebImage方式加载
- androidday01_html
- navicat无法连接Linux虚拟机中MySQL
- Android Bitmap
- EntityFramework 性能优化之查询编译
- 09-16 压缩图片
- CSS 水平垂直居中 方法一
- Redis学习12之jedis的set时间测试
- ES6详解三:class
- HTML float浮动标签和background 背景标签
- Gray Code
- weblogic初始(一)
- Hashmap的简单实用(成果展示)
- setAttibute和getParameter的区别