概述
Thumbnailator 是一個(gè)開源的 Java 項(xiàng)目,它提供了非常簡單的 API 來對圖片進(jìn)行縮放、旋轉(zhuǎn)以及加水印的處理。
有多簡單呢?簡單到一行代碼就可以完成圖片處理。形式如下:
1
2
3
4
|
Thumbnails.of( new File( "path/to/directory" ).listFiles()) .size( 640 , 480 ) .outputFormat( "jpg" ) .toFiles(Rename.PREFIX_DOT_THUMBNAIL); |
當(dāng)然,Thumbnailator 還有一些使用細(xì)節(jié),下面我會(huì)一一道來。
核心 API
Thumbnails
Thumbnails 是使用 Thumbnailator 創(chuàng)建縮略圖的主入口。
它提供了一組初始化 Thumbnails.Builder
的接口。
先看下這組接口的聲明:
1
2
3
4
5
6
7
8
9
10
11
12
|
// 可變長度參數(shù)列表 public static Builder<File> of(String... files) {...} public static Builder<File> of(File... files) {...} public static Builder<URL> of(URL... urls) {...} public static Builder<? extends InputStream> of(InputStream... inputStreams) {...} public static Builder<BufferedImage> of(BufferedImage... images) {...} // 迭代器(所有實(shí)現(xiàn) Iterable 接口的 Java 對象都可以,當(dāng)然也包括 List、Set) public static Builder<File> fromFilenames(Iterable<String> files) {...} public static Builder<File> fromFiles(Iterable<File> files) {...} public static Builder<URL> fromURLs(Iterable<URL> urls) {...} public static Builder<InputStream> fromInputStreams(Iterable<? extends InputStream> inputStreams) {...} public static Builder<BufferedImage> fromImages(Iterable<BufferedImage> images) {...} |
很顯然,Thumbnails 允許通過傳入文件名、文件、網(wǎng)絡(luò)圖的URL、圖片流、圖片緩存多種方式來初始化構(gòu)造器。
因此,你可以根據(jù)實(shí)際需求來靈活的選擇圖片的輸入方式。
需要注意一點(diǎn):如果輸入是多個(gè)對象(無論你是直接輸入容器對象或使用可變參數(shù)方式傳入多個(gè)對象),則輸出也必須選用輸出多個(gè)對象的方式,否則會(huì)報(bào)異常。
Thumbnails.Builder
Thumbnails.Builder
是 Thumbnails 的內(nèi)部靜態(tài)類。它用于設(shè)置生成縮略圖任務(wù)的相關(guān)參數(shù)。
注:Thumbnails.Builder
的構(gòu)造函數(shù)是私有函數(shù)。所以,它只允許通過 Thumbnails 的實(shí)例化函數(shù)來進(jìn)行初始化。
設(shè)置參數(shù)的函數(shù)
Thumbnails.Builder
提供了一組函數(shù)鏈形式的接口來設(shè)置縮放圖參數(shù)。
以設(shè)置大小函數(shù)為例:
1
2
3
4
5
6
7
8
9
10
11
|
public Builder<T> size( int width, int height) { updateStatus(Properties.SIZE, Status.ALREADY_SET); updateStatus(Properties.SCALE, Status.CANNOT_SET); validateDimensions(width, height); this .width = width; this .height = height; return this ; } |
通過返回this指針,使得設(shè)置參數(shù)函數(shù)可以以鏈?zhǔn)秸{(diào)用的方式來使用,形式如下:
1
2
3
4
5
6
|
Thumbnails.of( new File( "original.jpg" )) .size( 160 , 160 ) .rotate( 90 ) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read( new File( "watermark.png" )), 0 .5f) .outputQuality( 0.8 ) .toFile( new File( "image-with-watermark.jpg" )); |
好處,不言自明:那就是大大簡化了代碼。
輸出函數(shù)
Thumbnails.Builder
提供了一組重載函數(shù)來輸出生成的縮放圖。
函數(shù)聲明如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 返回圖片緩存 public List<BufferedImage> asBufferedImages() throws IOException {...} public BufferedImage asBufferedImage() throws IOException {...} // 返回文件列表 public List<File> asFiles(Iterable<File> iterable) throws IOException {...} public List<File> asFiles(Rename rename) throws IOException {...} public List<File> asFiles(File destinationDir, Rename rename) throws IOException {...} // 創(chuàng)建文件 public void toFile(File outFile) throws IOException {...} public void toFile(String outFilepath) throws IOException {...} public void toFiles(Iterable<File> iterable) throws IOException {...} public void toFiles(Rename rename) throws IOException {...} public void toFiles(File destinationDir, Rename rename) throws IOException {...} // 創(chuàng)建輸出流 public void toOutputStream(OutputStream os) throws IOException {...} public void toOutputStreams(Iterable<? extends OutputStream> iterable) throws IOException {...} |
工作流
Thumbnailator 的工作步驟十分簡單,可分為三步:
-
輸入:Thumbnails 根據(jù)輸入初始化構(gòu)造器——
Thumbnails.Builder
。 -
設(shè)置:
Thumbnails.Builder
設(shè)置縮放圖片的參數(shù)。 -
輸出:
Thumbnails.Builder
輸出圖片文件或圖片流。
更多詳情可以參考: Thumbnailator 官網(wǎng)javadoc
實(shí)戰(zhàn)
前文介紹了 Thumbnailator 的核心 API,接下來我們就可以通過實(shí)戰(zhàn)來看看 Thumbnailator 究竟可以做些什么。
Thumbnailator 生成什么樣的圖片,是根據(jù)設(shè)置參數(shù)來決定的。
安裝
maven項(xiàng)目中引入依賴:
1
2
3
4
5
|
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>[ 0.4 , 0.5 )</version> </dependency> |
Thumbnails.Builder
的 size 函數(shù)可以設(shè)置新圖片精確的寬度和高度,也可以用 scale 函數(shù)設(shè)置縮放比例。
1
2
3
4
5
6
7
8
9
10
11
|
Thumbnails.of( "oldFile.png" ) .size( 16 , 16 ) .toFile( "newFile_16_16.png" ); Thumbnails.of( "oldFile.png" ) .scale( 2.0 ) .toFile( "newFile_scale_2.0.png" ); Thumbnails.of( "oldFile.png" ) .scale( 1.0 , 0.5 ) .toFile( "newFile_scale_1.0_0.5.png" ); |
oldFile.png
newFile_scale_1.0_0.5.png
圖片旋轉(zhuǎn)
Thumbnails.Builder
的 size 函數(shù)可以設(shè)置新圖片的旋轉(zhuǎn)角度。
1
2
3
4
5
6
7
8
9
|
Thumbnails.of( "oldFile.png" ) .scale( 0.8 ) .rotate( 90 ) .toFile( "newFile_rotate_90.png" ); Thumbnails.of( "oldFile.png" ) .scale( 0.8 ) .rotate( 180 ) .toFile( "newFile_rotate_180.png" ); |
newFile_rotate_90.png
加水印
Thumbnails.Builder
的 watermark 函數(shù)可以為圖片添加水印圖片。第一個(gè)參數(shù)是水印的位置;第二個(gè)參數(shù)是水印圖片的緩存數(shù)據(jù);第三個(gè)參數(shù)是透明度。
1
2
3
4
5
|
BufferedImage watermarkImage = ImageIO.read( new File( "wartermarkFile.png" )); Thumbnails.of( "oldFile.png" ) .scale( 0.8 ) .watermark(Positions.BOTTOM_LEFT, watermarkImage, 0 .5f) .toFile( "newFile_watermark.png" ); |
wartermarkFile.png
newFile_watermark.png
批量處理圖片
下面以批量給圖片加水印來展示一下如何處理多個(gè)圖片文件。
1
2
3
4
5
6
7
|
BufferedImage watermarkImage = ImageIO.read( new File( "wartermarkFile.png" )); File destinationDir = new File( "D:\\watermark\\" ); Thumbnails.of( "oldFile.png" , "oldFile2.png" ) .scale( 0.8 ) .watermark(Positions.BOTTOM_LEFT, watermarkImage, 0 .5f) .toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL); |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用java能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:http://www.cnblogs.com/jingmoxukong/p/6293877.html