图片上传时,经常会遇到压缩图片的需求,下面介绍如何通过开源库thumbnailator进行压缩。
依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
使用
public class ThumbnailsTest {
@Test
public void toFile() throws IOException {
Thumbnails.of(new File("D:\\photo\\ChMkJ1bKxmyIR.jpg"))
// 缩放比例,越接近1越接近原图
.scale(0.5D)
// 缩放质量,越接近1越接近原图
.outputQuality(0.3D)
.toFile("D:\\photo\\test.jpg");
}
}
通过工具类Thumbnails
进行相关操作。
本文由 xinQing 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2018/04/24 08:01
骚波(> <)