maven项目添加依赖
<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator --> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.16</version> </dependency>
直接上代码
public static void main(String[] args) {
try {
//复制
Thumbnailator.createThumbnail(new File("2.png"), new File("21.png"), 300, 200);
//scale缩略比例,rotate旋转角度,
Thumbnails.of(new File("2.png")).scale(0.8).rotate(60).toFile(new File("22.png"));
//outputFormat转换格式,.size大小
Thumbnails.of("2.png").outputFormat("gif").size(100, 100).toFile("23.gif");
//sourceRegion裁剪
Thumbnails.of("2.png").scale(1.0).sourceRegion(0, 0, 300, 300).toFile("24.png");
} catch (IOException e) {
e.printStackTrace();
}
}