使用Thumbnails实现图片指定大小压缩
作者:DcForever 时间:2021-11-14 11:07:34
项目中有个要求,对上传服务器的图片大小进行判断,大于500k的图片要进行压缩处理,让其小于500k后在上传。
可以通过java api的ImageIO实现图片压缩,但是看了网上的博客普遍都说bug比较多,会有OOM内存溢出的现象。
Thumbnails插件是Google的插件,能指定不同的参数进行压缩操作。
比如:宽高(size),缩放(scale),制定质量比(outputQuality)等。
插件使用的jar包为:
thumbnailator-0.4.8.jar
代码如下:
/**
*
* @param srcPath 原图片地址
* @param desPath 目标图片地址
* @param desFileSize 指定图片大小,单位kb
* @param accuracy 精度,递归压缩的比率,建议小于0.9
* @return
*/
public static String commpressPicForScale(String srcPath,String desPath,
long desFileSize , double accuracy){
try {
File srcFile = new File(srcPath);
long srcFilesize = srcFile.length();
System.out.println("原图片:"+srcPath + ",大小:" + srcFilesize/1024 + "kb");
//递归压缩,直到目标文件大小小于desFileSize
commpressPicCycle(desPath, desFileSize, accuracy);
File desFile = new File(desPath);
System.out.println("目标图片:" + desPath + ",大小" + desFile.length()/1024 + "kb");
System.out.println("图片压缩完成!");
} catch (Exception e) {
e.printStackTrace();
}
return desPath;
}
public static void commpressPicCycle(String desPath , long desFileSize,
double accuracy) throws IOException{
File imgFile = new File(desPath);
long fileSize = imgFile.length();
//判断大小,如果小于500k,不压缩,如果大于等于500k,压缩
if(fileSize <= desFileSize * 500){
return;
}
//计算宽高
BufferedImage bim = ImageIO.read(imgFile);
int imgWidth = bim.getWidth();
int imgHeight = bim.getHeight();
int desWidth = new BigDecimal(imgWidth).multiply(
new BigDecimal(accuracy)).intValue();
int desHeight = new BigDecimal(imgHeight).multiply(
new BigDecimal(accuracy)).intValue();
Thumbnails.of(desPath).size(desWidth, desHeight).outputQuality(accuracy).toFile(desPath);
//如果不满足要求,递归直至满足小于1M的要求
commpressPicCycle(desPath, desFileSize, accuracy);
}
然后压缩图片大小:
commpressPicForScale(filePath, filePath, 500, 0.8);
压缩完成:
来源:https://blog.csdn.net/DCFANS/article/details/97806265
标签:Thumbnails,图片压缩
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#多线程之Thread中Thread.IsAlive属性用法分析
2023-07-11 11:44:12
安卓(Android)游戏开发音效代码
2023-11-15 14:47:58
Spring IOC与DI核心重点分析
2023-11-12 14:35:55
![](https://img.aspxhome.com/file/2023/4/58604_0s.png)
Android实现拼图小游戏
2023-03-01 11:25:46
![](https://img.aspxhome.com/file/2023/6/93326_0s.jpg)
java.nio.file.WatchService 实时监控文件变化的示例代码
2021-10-03 01:52:53
![](https://img.aspxhome.com/file/2023/8/64108_0s.jpg)
Java基于Tcp的基础聊天功能实例
2023-11-25 05:26:56
如何在mapper文件中使用in("str1","str2")
2023-07-10 10:40:51
![](https://img.aspxhome.com/file/2023/0/57610_0s.jpg)
spring-boot使用Admin监控应用的方法
2023-06-28 19:58:39
![](https://img.aspxhome.com/file/2023/5/114055_0s.png)
Spring框架实现AOP添加日志记录功能过程详解
2023-05-31 01:18:19
![](https://img.aspxhome.com/file/2023/2/81962_0s.png)
Spring中propagation的7种事务配置及说明
2023-01-02 09:47:26
![](https://img.aspxhome.com/file/2023/9/70539_0s.jpg)
SpringBoot+Redis实现数据字典的方法
2022-08-03 14:22:29
![](https://img.aspxhome.com/file/2023/4/65364_0s.jpg)
Java实时获取基金收益项目源码分享
2023-11-28 17:30:47
![](https://img.aspxhome.com/file/2023/4/85894_0s.png)
RabbitMQ消息中间件示例详解
2021-12-10 23:09:58
JWT在OpenFeign调用中进行令牌中继详解
2023-02-07 04:19:15
常用Maven库,镜像库及maven/gradle配置(小结)
2023-11-20 23:44:00
Java中List集合去除重复数据的方法汇总
2021-07-05 14:57:21
EasyValidate优雅地校验提交数据完整性
2022-03-30 11:54:46
![](https://img.aspxhome.com/file/2023/3/96603_0s.jpg)
C#与C++之间类型的对应知识点总结
2023-09-07 21:02:56
Springboot启动后立即某个执行方法的四种方式
2022-12-29 16:35:52
java程序员必须要学会的linux命令总结(推荐)
2021-12-11 00:47:47