utils
/**
* 重新生成图片宽、高
*
* @param srcPath 图片路径
* @param destPath 新生成的图片路径
* @param newWith 新的宽度
* @param newHeight 新的高度
* @param forceSize 是否强制使用指定宽、高,false:会保持原图片宽高比例约束
* @return
* @throws IOException
*/
public static boolean resizeImage(String srcPath, String destPath, int newWith, int newHeight, boolean forceSize) throws IOException {
if (forceSize) {
Thumbnails.of(srcPath).forceSize(newWith, newHeight).toFile(destPath);
} else {
Thumbnails.of(srcPath).width(newWith).height(newHeight).toFile(destPath);
}
return true;
}
依赖
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
@ApiOperation("图片压缩测试")
@PostMapping(value = "/compreePhotoTest")
@ApiImplicitParam(name = "photo", value = "图片")
public AjaxResult compreePhotoTest(@RequestParam(required = false, value = "photo") MultipartFile[] photo, @RequestParam(required = false, value = "annexType") String annexType, @RequestParam(value = "relicNum") String relicNum) {
try {
ResizeImageUtils.resizeImage("C:/Users/Administrator/Desktop/timg.jpg", "D:/testPhoto/test.jpg", 200, 200, true);
return AjaxResult.success();
} catch (Exception e) {
log.info("图片压缩测试失败!", e);
return AjaxResult.error("图片压缩测试失败:" + e.getMessage());
}
}