maven依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
使用接口例子 https://github.com/coobird/thumbnailator/wiki/Examples
1、指定大小进行缩放
Thumbnails.of("images/a380_1280x1024.jpg").size(200, 300).toFile("c:/a380_200x300.jpg");
2、按照比例进行缩放
Thumbnails.of("images/a380_1280x1024.jpg").scale(0.25f).toFile("c:/a380_25%.jpg");
3、不按照比例,指定大小进行缩放
//keepAspectRatio(false) 默认是按照比例缩放的
Thumbnails.of("images/a380_1280x1024.jpg").size(200,200).keepAspectRatio(false).toFile("c:/a380_200x200.jpg");
4、旋转
//rotate(角度),正数:顺时针负数:逆时针
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).rotate(90).toFile("c:/a380_rotate+90.jpg");
5、水印
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).watermark(Positions.BOTTOM_RIGHT,ImageIO.read(newFile("images/watermark.png")),0.5f).outputQuality(0.8f).toFile("c:/a380_watermark_bottom_right.jpg");
6、裁剪
//图片中心400*400的区域
Thumbnails.of("images/a380_1280x1024.jpg").sourceRegion(Positions.CENTER,400,400).size(200,200).keepAspectRatio(false).toFile("c:/a380_region_center.jpg");
7、转化图像格式
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).outputFormat("png").toFile("c:/a380_1280x1024.png");
8、输出到OutputStream
//toOutputStream(流对象)
OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");
Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).toOutputStream(os);
9、输出到BufferedImage
//asBufferedImage()返回BufferedImage
BufferedImage thumbnail=Thumbnails.of("images/a380_1280x1024.jpg").size(1280,1024).asBufferedImage();
ImageIO.write(thumbnail,"jpg",newFile("c:/a380_1280x1024_BufferedImage.jpg"));
原文地址:https://www.cnblogs.com/grasp/p/9689669.html