當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot 集成 thumbnailator (图片缩放,区域裁剪,水印,旋转,保持比例)保姆级教程(含代码)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 集成 thumbnailator (图片缩放,区域裁剪,水印,旋转,保持比例)保姆级教程(含代码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Thumbnailator 是 Google 開源的圖片處理庫,支持:圖片縮放,區(qū)域裁剪,水印,旋轉(zhuǎn),保持比例。詳細介紹可以百度或官網(wǎng),話不多說,直接上代碼,具體要結(jié)合自己的業(yè)務需要進行使用(有些復雜場景比如 旋轉(zhuǎn)+縮放+裁剪+水印 難嗎?)。
1 代碼示例
1.1 新建一個 springboot 項目
1.2 引入依賴 thumbnailator
<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version></dependency>1.3 controller
@RestController public class ThumbnailsController {@Resourceprivate IThumbnailsService thumbnailsService;/*** 指定大小縮放*/@GetMapping("/changeSize")public String changeSize(MultipartFile resource, int width, int height) {String toFile = "C:\\Users\\Administrator\\Desktop\\thumbnailator\\changeSize";return thumbnailsService.changeSize(resource, width, height, toFile);}/*** 指定比例縮放*/@GetMapping("/changeScale")public String changeScale(MultipartFile resource, double scale) {String toFile = "C:\\Users\\Administrator\\Desktop\\thumbnailator\\changeScale";return thumbnailsService.changeScale(resource, scale, toFile);}/*** 添加水印 watermark(位置,水印,透明度)*/@GetMapping("/watermark")public String watermark(MultipartFile resource, Positions center, MultipartFile watermark, float opacity) {String toFile = "C:\\Users\\Administrator\\Desktop\\thumbnailator\\watermark";return thumbnailsService.watermark(resource, Positions.CENTER, watermark, opacity, toFile);}/*** 圖片旋轉(zhuǎn) rotate(度數(shù)),順時針旋轉(zhuǎn)*/@GetMapping("/rotate")public String rotate(MultipartFile resource, double rotate) {String toFile = "C:\\Users\\Administrator\\Desktop\\thumbnailator\\rotate";return thumbnailsService.rotate(resource, rotate, toFile);}/*** 圖片裁剪*/@GetMapping("/region")public String region(MultipartFile resource, Positions center, int width, int height) {String toFile = "C:\\Users\\Administrator\\Desktop\\thumbnailator\\region";return thumbnailsService.region(resource, Positions.CENTER, width, height, toFile);} }2 功能實現(xiàn)
這是實現(xiàn)類里每個方法的代碼+postman測試(測試結(jié)果不再貼出來了,大家可以自行測試)。
2.1 指定大小縮放
/*** 指定大小縮放 若圖片橫比width小,高比height小,放大 * 若圖片橫比width小,高比height大,高縮小到height,圖片比例不變* 若圖片橫比width大,高比height小,橫縮小到width,圖片比例不變 * 若圖片橫比width大,高比height大,圖片按比例縮小,橫為width或高為height* * @param resource 源文件路徑* @param width 寬* @param height 高* @param tofile 生成文件路徑*/@Overridepublic String changeSize(MultipartFile resource, int width, int height, String toFile) {try {Thumbnails.of(resource.getInputStream()).size(width, height).outputFormat("jpg").toFile(toFile);} catch (IOException e) {e.printStackTrace();}return "changeSize";}2.2 指定比例縮放
/*** 指定比例縮放 scale(),參數(shù)小于1,縮小;大于1,放大* * @param resource 源文件路徑* @param scale 指定比例* @param tofile 生成文件路徑*/@Overridepublic String changeScale(MultipartFile resource, double scale, String toFile) {try {Thumbnails.of(resource.getInputStream()).scale(scale).toFile(toFile);} catch (IOException e) {e.printStackTrace();}return "changeScale";}2.3 添加水印
/*** 添加水印 watermark(位置,水印,透明度)* * @param resource 源文件路徑* @param center 水印位置* @param shuiyin 水印文件路徑* @param opacity 水印透明度* @param tofile 生成文件路徑*/@Overridepublic String watermark(MultipartFile resource, Positions center, MultipartFile watermark, float opacity, String toFile) {try {Thumbnails.of(resource.getInputStream()).scale(1).watermark(center, ImageIO.read(watermark.getInputStream()), opacity).toFile(toFile);} catch (IOException e) {e.printStackTrace();}return "watermark";}2.4 圖片旋轉(zhuǎn)
/*** 圖片旋轉(zhuǎn) rotate(度數(shù)),順時針旋轉(zhuǎn)* * @param resource 源文件路徑* @param rotate 旋轉(zhuǎn)度數(shù)* @param tofile 生成文件路徑*/@Overridepublic String rotate(MultipartFile resource, double rotate, String toFile) {try {Thumbnails.of(resource.getInputStream()).scale(1).rotate(rotate).toFile(toFile);} catch (IOException e) {e.printStackTrace();}return "rotate";}2.5 圖片裁剪
/*** 圖片裁剪 sourceRegion()有多種構(gòu)造方法,示例使用的是sourceRegion(裁剪位置,寬,高)* * @param resource 源文件路徑* @param center 裁剪位置* @param width 裁剪區(qū)域?qū)? @param height 裁剪區(qū)域高* @param tofile 生成文件路徑*/@Overridepublic String region(MultipartFile resource, Positions center, int width, int height, String toFile) {try {Thumbnails.of(resource.getInputStream()).scale(1).sourceRegion(center, width, height).toFile(toFile);} catch (IOException e) {e.printStackTrace();}return "region";}
說明:
- 1.keepAspectRatio(boolean arg0) 圖片是否按比例縮放(寬高比保持不變)默認 true
- 2.outputQuality(float arg0) 圖片質(zhì)量
- 3.outputFormat(String arg0) 格式轉(zhuǎn)換
小結(jié)
需要注意的是,若 png、gif 格式圖片中含有透明背景,使用該工具壓縮處理后背景會變成黑色。
總結(jié)
以上是生活随笔為你收集整理的SpringBoot 集成 thumbnailator (图片缩放,区域裁剪,水印,旋转,保持比例)保姆级教程(含代码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【MyBatis使用】mapper.xm
- 下一篇: SpringBoot 项目war包部署