什麼是Thumbnailator?
Thumbnailator是Java的縮略圖生成庫。
爲什麼使用Thumbnailator?
用Java製作高質量的縮略圖可能是一項相當困難的任務。
學習如何使用圖像I / O API,Java 2D API,圖像處理,圖像縮放技術,但不要擔心!Thumbnailator將爲您處理所有這些事情!
Thumbnailator是單個JAR文件,不依賴於外部庫,從而使開發和部署變得簡單而容易。它也可以在Maven中央存儲庫中找到,以便輕鬆包含在Maven項目中。
使用示例:
首先maven引入jar文件
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
示例1:
在目錄中創建圖像文件的JPEG縮略圖,將它們全部調整爲最大尺寸爲640像素乘480像素,同時保留原始圖像的長寬比。
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator";
Thumbnails.of(new File(filePath).listFiles())
.size(640, 480)
.outputFormat("jpg")
.toFiles(Rename.PREFIX_DOT_THUMBNAIL);
}
效果
Thumbnailator提供的流暢接口簡化了將縮略圖製作爲單個方法調用的任務!
無需訪問Image I / O API並BufferedImage通過Graphics2D對象手動操作。Thumbnailator爲您完成所有這些工作。
示例二:
從圖像文件創建縮略圖:將Img1.jpg調整圖片的大小,然後保存到Img1_thumbnail.jpg。
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img1.jpg";
String newFilePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img1_thumbnail.jpg";
Thumbnails.of(new File(filePath))
.size(640, 480)
.toFile(new File(newFilePath));
}
或者,Img1_thumbnail將接受文件名作爲String。File不需要使用對象來指定圖像文件:
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img1.jpg";
String newFilePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img1_thumbnail.jpg";
Thumbnails.of(filePath)
.size(640, 480)
.toFile(newFilePath);
在編寫快速的原型代碼時,或者在腳本語言中使用Thumbnailator時,此格式很有用。
示例三:
創建帶有旋轉和水印的縮略圖
String filePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img1.jpg";
String newFilePath = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img1_thumbnail.jpg";
String watermark = "C:\\Users\\shunli\\Desktop\\Thumbnailator\\Img2.jpg";
Thumbnails.of(new File(filePath))
.size(640, 480)
.rotate(90)//順時針旋轉90度
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermark)), 0.7f)//水印
.outputQuality(0.8)
.toFile(new File(newFilePath));
}
在此示例中,將Img1.jpg調整圖片的大小,然後將其順時針旋轉90度,然後在右下角放置半透明(0.7f)的水印,然後將其保存爲Img1_thumbnail.jpg 80%的壓縮質量設置。
示例四:
創建縮略圖並寫入 OutputStream
OutputStream os = ...;
Thumbnails.of("large-picture.jpg")
.size(200, 200)
.outputFormat("png")
.toOutputStream(os);
在此示例中,文件中的圖像large-picture.jpg被調整爲最大尺寸爲200 x 200(保持原始圖像的縱橫比),並將該圖像寫入指定OutputStream的PNG圖像中。
示例五
創建固定大小的縮略圖
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.asBufferedImage();
上面的代碼使用拍攝圖像,originalImage並使用創建一個200像素乘200像素的縮略圖,並將結果存儲在中thumbnail。
示例六:
按給定因子縮放圖像
BufferedImage originalImage = ImageIO.read(new File("original.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.scale(0.25)
.asBufferedImage();
上面的代碼將圖像originalImage導入,並創建一個佔原始圖像25%的縮略圖,並使用默認的縮放技術以製作存儲在中的縮略圖thumbnail。
示例七:
創建縮略圖時旋轉圖像
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.rotate(90)
.asBufferedImage();
上面的代碼獲取原始圖像並創建一個縮略圖,該縮略圖將順時針旋轉90度。
示例八:
創建帶有水印的縮略圖
BufferedImage originalImage = ImageIO.read(new File("original.jpg"));
BufferedImage watermarkImage = ImageIO.read(new File("watermark.png"));
BufferedImage thumbnail = Thumbnails.of(originalImage)
.size(200, 200)
.watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.5f)
.asBufferedImage();
如圖所示,可以通過調用該watermark方法將水印添加到縮略圖。
可以從Positions枚舉中選擇位置。
可以通過更改最後一個參數來調整縮略圖的不透明度(或相反,透明度),最後一個參數0.0f是縮略圖完全透明,而1.0f水印則完全不透明。
示例九:
將縮略圖寫入特定目錄
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);
本示例將獲取源圖像,並將縮略圖作爲文件寫到destinationDir(path/to/output目錄),同時thumbnail.以文件名開頭重命名它們。
因此,縮略圖將作爲文件寫入:
path/to/output/thumbnail.apple.jpg
path/to/output/thumbnail.banana.jpg
path/to/output/thumbnail.cherry.jpg
在寫入指定目錄時,還可以保留原始文件名:
File destinationDir = new File("path/to/output");
Thumbnails.of("apple.jpg", "banana.jpg", "cherry.jpg")
.size(200, 200)
.toFiles(destinationDir, Rename.NO_CHANGE);
在上面的代碼中,縮略圖將被寫入:
path/to/output/apple.jpg
path/to/output/banana.jpg
path/to/output/cherry.jpg