Java縮略圖生成庫(kù)之Thumbnailator應(yīng)用說(shuō)明
Thumbnailator 是一個(gè)為Java界面更流暢的縮略圖生成庫(kù)。從API提供現(xiàn)有的圖像文件和圖像對(duì)象的縮略圖中簡(jiǎn)化了縮略過(guò)程,兩三行代碼就能夠從現(xiàn)有圖片生成縮略圖,且允許微調(diào)縮略圖生成,同時(shí)保持了需要寫(xiě)入到最低限度的代碼量。同時(shí)還支持根據(jù)一個(gè)目錄批量生成縮略圖。
版本:thumbnailator-0.4.2.jar
原圖如下:
1、指定大小進(jìn)行縮放
//size(寬度, 高度)
/*
* 若圖片橫比200小,高比300小,不變
* 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變
* 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變
* 若圖片橫比200大,高比300大,圖片按比例縮小,橫為200或高為300
*/
Thumbnails.of("images/a380_1280x1024.jpg")
.size(200, 300)
.toFile("c:/a380_200x300.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(2560, 2048)
.toFile("c:/a380_2560x2048.jpg");
2、按照比例進(jìn)行縮放
//scale(比例)
Thumbnails.of("images/a380_1280x1024.jpg")
.scale(0.25f)
.toFile("c:/a380_25%.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.scale(1.10f)
.toFile("c:/a380_110%.jpg");
3、不按照比例,指定大小進(jìn)行縮放
//keepAspectRatio(false) 默認(rèn)是按照比例縮放的
Thumbnails.of("images/a380_1280x1024.jpg")
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_200x200.jpg");
4、旋轉(zhuǎn)
//rotate(角度),正數(shù):順時(shí)針 負(fù)數(shù):逆時(shí)針
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.rotate(90)
.toFile("c:/a380_rotate+90.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.rotate(-90)
.toFile("c:/a380_rotate-90.jpg");


5、水印
//watermark(位置,水印圖,透明度)
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f)
.toFile("c:/a380_watermark_bottom_right.jpg");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)
.outputQuality(0.8f)
.toFile("c:/a380_watermark_center.jpg");


6、裁剪
//sourceRegion()
//圖片中心400*400的區(qū)域
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(Positions.CENTER, 400,400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_center.jpg");
//圖片右下400*400的區(qū)域
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(Positions.BOTTOM_RIGHT, 400,400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_bootom_right.jpg");
//指定坐標(biāo)
Thumbnails.of("images/a380_1280x1024.jpg")
.sourceRegion(600, 500, 400, 400)
.size(200, 200)
.keepAspectRatio(false)
.toFile("c:/a380_region_coord.jpg");



7、轉(zhuǎn)化圖像格式
//outputFormat(圖像格式)
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.outputFormat("png")
.toFile("c:/a380_1280x1024.png");
Thumbnails.of("images/a380_1280x1024.jpg")
.size(1280, 1024)
.outputFormat("gif")
.toFile("c:/a380_1280x1024.gif");
8、輸出到OutputStream
//toOutputStream(流對(duì)象)
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", new File("c:/a380_1280x1024_BufferedImage.jpg"));
大小: 20.7 KB
大小: 1.1 KB
大小: 2.3 KB
大小: 2.2 KB
大小: 23.2 KB
大小: 23.3 KB
大小: 27.9 KB
大小: 27 KB
相關(guān)文章
深入解析Java的設(shè)計(jì)模式編程中的模板方法模式
這篇文章主要介紹了深入解析Java的設(shè)計(jì)模式編程中的模板方法模式, 模版方法模式由一個(gè)抽象類和一個(gè)(或一組)實(shí)現(xiàn)類通過(guò)繼承結(jié)構(gòu)組成,需要的朋友可以參考下2016-02-02
Struts2學(xué)習(xí)筆記(8)-Result常用類型
這篇文章主要介紹Struts2中Result四種常用的類型的用法,希望能給大家做一個(gè)參考。2016-06-06
SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問(wèn)題
這篇文章主要介紹了SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
java實(shí)現(xiàn)Object轉(zhuǎn)String的4種方法小結(jié)
這篇文章主要介紹了java實(shí)現(xiàn)Object轉(zhuǎn)String的4種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
java阿拉伯?dāng)?shù)字轉(zhuǎn)中文數(shù)字
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)換為中文數(shù)字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Springboot 整合通用mapper和pagehelper展示分頁(yè)數(shù)據(jù)的問(wèn)題(附github源碼)
這篇文章主要介紹了Springboot 整合通用mapper和pagehelper展示分頁(yè)數(shù)據(jù)(附github源碼),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

