thumbnailator可以实现图片的压缩、旋转、添加水印
文档
依赖
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.17</version>
</dependency>
代码示例
压缩图片大小
package com.example.demo;
import net.coobird.thumbnailator.Thumbnails;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
Thumbnails.of("img/image.jpg")
.outputQuality(0.5f)
.toFile("img/image-out.jpg");
}
}
图片处理前后大小对比
267K image.jpg
140K image-out.jpg