Java縮略圖生成庫之Thumbnailator應(yīng)用說明
Thumbnailator 是一個為Java界面更流暢的縮略圖生成庫。從API提供現(xiàn)有的圖像文件和圖像對象的縮略圖中簡化了縮略過程,兩三行代碼就能夠從現(xiàn)有圖片生成縮略圖,且允許微調(diào)縮略圖生成,同時保持了需要寫入到最低限度的代碼量。同時還支持根據(jù)一個目錄批量生成縮略圖。
版本: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ù):順時針 負(fù)數(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(流對象)
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)文章
Struts2學(xué)習(xí)筆記(8)-Result常用類型
這篇文章主要介紹Struts2中Result四種常用的類型的用法,希望能給大家做一個參考。2016-06-06
SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問題
這篇文章主要介紹了SpringBoot返回long,前端接收進(jìn)度丟失,@JsonSerialize不生效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
java實現(xiàn)Object轉(zhuǎn)String的4種方法小結(jié)
這篇文章主要介紹了java實現(xiàn)Object轉(zhuǎn)String的4種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
java阿拉伯?dāng)?shù)字轉(zhuǎn)中文數(shù)字
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)換為中文數(shù)字,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
Springboot 整合通用mapper和pagehelper展示分頁數(shù)據(jù)的問題(附github源碼)
這篇文章主要介紹了Springboot 整合通用mapper和pagehelper展示分頁數(shù)據(jù)(附github源碼),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09

