Thumbnails 压缩图片(缩放图片,编辑,格式转化,流处理)
- maven
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
- 使用
/**
* 指定大小缩放
* 若图片横比200小,高比300小,不变
* 若图片横比200小,高比300大,高缩小到300,图片比例不变
* 若图片横比200大,高比300小,横缩小到200,图片比例不变
* 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
*
* @param sourceFilePath 源图片路径
* @param compressedFilePath 压缩图片保存路径
* @param height 高
* @param width 宽
*/
public static void specifySize(String sourceFilePath, String compressedFilePath, int height, int width) throws IOException {
Thumbnails.of(sourceFilePath).size(height, width).toFile(compressedFilePath);
}
/**
* 不按比例,指定大小进行缩放,keepAspectRatio:默认是按照比例缩放的
*
* @param sourceFilePath 源图片路径
* @param compressedFilePath 压缩图片保存路径
* @param height 高
* @param width 宽
*/
public static void specifySizes(String sourceFilePath, String compressedFilePath, int height, int width) throws IOException {
Thumbnails.of(sourceFilePath).keepAspectRatio(false