javascript
SpringBoot集成Google开源图片处理框架,贼好用!
1、序
在實際開發中,難免會對圖片進行一些處理,比如圖片壓縮之類的,而其中壓縮可能就是最為常見的。最近,我就被要求實現這個功能,原因是客戶那邊嫌速度過慢。借此機會,今兒就給大家介紹一些一下我做這個功能時使用的 Thumbnailator 庫。
Thumbnailator 是一個優秀的圖片處理的 Google 開源 Java 類庫,專門用來生成圖像縮略圖的,通過很簡單的 API 調用即可生成圖片縮略圖,也可直接對一整個目錄的圖片生成縮略圖。兩三行代碼就能夠從現有圖片生成處理后的圖片,且允許微調圖片的生成方式,同時保持了需要寫入的最低限度的代碼量。可毫不夸張的說,它是一個處理圖片十分棒的開源框架。
支持:圖片縮放,區域裁剪,水印,旋轉,保持比例。
Thumbnailator 官網:https://code.google.com/p/thumbnailator/
有了這玩意,就不用在費心思使用 Image I/O API,Java 2D API 等等來生成縮略圖了。
廢話少說,直接上代碼,先來看一個最簡單的例子:
2、代碼示例
2.1. 新建一個springboot項目
2.2. 引入依賴 thumbnailator
<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version> </dependency>2.3. controller
主要實現了如下幾個接口作為測試:
@RestController public?class?ThumbnailsController?{@Resourceprivate?IThumbnailsService?thumbnailsService;/***?指定大小縮放*?@param?resource*?@param?width*?@param?height*?@return*/@GetMapping("/changeSize")public?String?changeSize(MultipartFile?resource,?int?width,?int?height)?{return?thumbnailsService.changeSize(resource,?width,?height);}/***?指定比例縮放*?@param?resource*?@param?scale*?@return*/@GetMapping("/changeScale")public?String?changeScale(MultipartFile?resource,?double?scale)?{return?thumbnailsService.changeScale(resource,?scale);}/***?添加水印?watermark(位置,水印,透明度)*?@param?resource*?@param?p*?@param?shuiyin*?@param?opacity*?@return*/@GetMapping("/watermark")public?String?watermark(MultipartFile?resource,?Positions?p,?MultipartFile?shuiyin,?float?opacity)?{return?thumbnailsService.watermark(resource,?Positions.CENTER,?shuiyin,?opacity);}/***?圖片旋轉?rotate(度數),順時針旋轉*?@param?resource*?@param?rotate*?@return*/@GetMapping("/rotate")public?String?rotate(MultipartFile?resource,?double?rotate)?{return?thumbnailsService.rotate(resource,?rotate);}/***?圖片裁剪*?@param?resource*?@param?p*?@param?width*?@param?height*?@return*/@GetMapping("/region")public?String?region(MultipartFile?resource,?Positions?p,?int?width,?int?height)?{return?thumbnailsService.region(resource,?Positions.CENTER,?width,?height);} }3、功能實現
其實引入了這個 Thumbnailator 類庫后,代碼其實很少,因為我們只需要按照規則調用其 API 來實現即可。就個人而言,挺喜歡這種 API 的方式,簡潔,易懂,明了。
3.1 指定大小縮放
/***?指定大小縮放?若圖片橫比width小,高比height小,放大?*?若圖片橫比width小,高比height大,高縮小到height,圖片比例不變*?若圖片橫比width大,高比height小,橫縮小到width,圖片比例不變?*?若圖片橫比width大,高比height大,圖片按比例縮小,橫為width或高為height*?*?@param?resource??源文件路徑*?@param?width?????寬*?@param?height????高*?@param?tofile????生成文件路徑*/ public?static?void?changeSize(String?resource,?int?width,?int?height,?String?tofile)?{try?{Thumbnails.of(resource).size(width,?height).toFile(tofile);}?catch?(IOException?e)?{e.printStackTrace();} }測試:
3.2 指定比例縮放
/***?指定比例縮放?scale(),參數小于1,縮小;大于1,放大*?*?@param?resource???源文件路徑*?@param?scale??????指定比例*?@param?tofile?????生成文件路徑*/ public?static?void?changeScale(String?resource,?double?scale,?String?tofile)?{try?{Thumbnails.of(resource).scale(scale).toFile(tofile);}?catch?(IOException?e)?{e.printStackTrace();} }測試:
3.3 添加水印
/***?添加水印?watermark(位置,水印,透明度)*?*?@param?resource??源文件路徑*?@param?p?????????水印位置*?@param?shuiyin???水印文件路徑*?@param?opacity???水印透明度*?@param?tofile????生成文件路徑*/ public?static?void?watermark(String?resource,?Positions?p,?String?shuiyin,?float?opacity,?String?tofile)?{try?{Thumbnails.of(resource).scale(1).watermark(p,?ImageIO.read(new?File(shuiyin)),?opacity).toFile(tofile);}?catch?(IOException?e)?{e.printStackTrace();} }測試:
3.4 圖片旋轉
/***?圖片旋轉?rotate(度數),順時針旋轉*?*?@param?resource??源文件路徑*?@param?rotate????旋轉度數*?@param?tofile????生成文件路徑*/
public?static?void?rotate(String?resource,?double?rotate,?String?tofile)?{try?{Thumbnails.of(resource).scale(1).rotate(rotate).toFile(tofile);}?catch?(IOException?e)?{e.printStackTrace();}
}
測試:
3.5 圖片裁剪
/***?圖片裁剪?sourceRegion()有多種構造方法,示例使用的是sourceRegion(裁剪位置,寬,高)*?*?@param?resource??源文件路徑*?@param?p?????????裁剪位置*?@param?width?????裁剪區域寬*?@param?height????裁剪區域高*?@param?tofile????生成文件路徑*/ public?static?void?region(String?resource,?Positions?p,?int?width,?int?height,?String?tofile)?{try?{Thumbnails.of(resource).scale(1).sourceRegion(p,?width,?height).toFile(tofile);}?catch?(IOException?e)?{e.printStackTrace();} }測試:
說明:
1.keepAspectRatio(boolean arg0) ?圖片是否按比例縮放(寬高比保持不變)默認 true
2.outputQuality(float arg0) 圖片質量
3.outputFormat(String arg0) 格式轉換
小結
值得注意的是,若 png、gif 格式圖片中含有透明背景,使用該工具壓縮處理后背景會變成黑色,這是 Thumbnailator 的一個 bug,預計后期版本會解決。
往期推薦MySQL為Null會導致5個問題,個個致命!
編程中的21個坑,你占幾個?
Spring Boot集成Redis,這個坑把我害慘了!
關注我,每天陪你進步一點點!
總結
以上是生活随笔為你收集整理的SpringBoot集成Google开源图片处理框架,贼好用!的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 面试题:为什么Java中的字符串对象是不
 - 下一篇: 第 2-2 课:各种内部类和枚举类 +