圖片處理是當今軟件開發中非常重要的一環,然而處理圖片的開源框架卻并不多。現金網上流傳的java處理圖片的代碼,雖然可對圖片進行簡單處理,但效果并不理想。雖然也有些其他解決方案,但都擺脫不了繁瑣,使用起來十分不方便。
為了解決這個問題,我也是在網上找了好久,看了很多資料,功夫不負有心人,最終找到了一個處理圖片十分棒的開源框架。特此拿出來與大家分享。
thumbnailator 是一個優秀的圖片處理的google開源java類庫。處理效果遠比java api的好。從api提供現有的圖像文件和圖像對象的類中簡化了處理過程,兩三行代碼就能夠從現有圖片生成處理后的圖片,且允許微調圖片的生成方式,同時保持了需要寫入的最低限度的代碼量。還支持對一個目錄的所有圖片進行批量處理操作。
支持的處理操作:圖片縮放,區域裁剪,水印,旋轉,保持比例。
另外值得一提的是,thumbnailator至今仍不斷更新,怎么樣,感覺很有保障吧!
下面我們介紹下如何使用thumbnailator
原圖:
1、指定大小進行縮放
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//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、按照比例進行縮放
1
2
3
4
5
6
7
8
|
//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、不按照比例,指定大小進行縮放
1
2
3
4
5
|
//keepaspectratio(false)默認是按照比例縮放的 thumbnails.of( "images/a380_1280x1024.jpg" ) .size( 200 , 200 ) .keepaspectratio( false ) .tofile( "c:/a380_200x200.jpg" ); |
4、旋轉
1
2
3
4
5
6
7
8
9
10
|
//rotate(角度),正數:順時針負數:逆時針 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、水印
1
2
3
4
5
6
7
8
9
10
11
12
|
//watermark(位置,水印圖,透明度) thumbnails.of( "images/a380_1280x1024.jpg" ) .size( 1280 , 1024 ) .watermark(positions.bottom_right,imageio.read(newfile( "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(newfile( "images/watermark.png" )), 0 .5f) .outputquality( 0 .8f) .tofile( "c:/a380_watermark_center.jpg" ); |
6、裁剪
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//sourceregion() //圖片中心400*400的區域 thumbnails.of( "images/a380_1280x1024.jpg" ) .sourceregion(positions.center, 400 , 400 ) .size( 200 , 200 ) .keepaspectratio( false ) .tofile( "c:/a380_region_center.jpg" ); //圖片右下400*400的區域 thumbnails.of( "images/a380_1280x1024.jpg" ) .sourceregion(positions.bottom_right, 400 , 400 ) .size( 200 , 200 ) .keepaspectratio( false ) .tofile( "c:/a380_region_bootom_right.jpg" ); //指定坐標 thumbnails.of( "images/a380_1280x1024.jpg" ) .sourceregion( 600 , 500 , 400 , 400 ) .size( 200 , 200 ) .keepaspectratio( false ) .tofile( "c:/a380_region_coord.jpg" ); |
7、轉化圖像格式
1
2
3
4
5
6
7
8
9
10
|
//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
1
2
3
4
5
|
//tooutputstream(流對象) outputstreamos=newfileoutputstream( "c:/a380_1280x1024_outputstream.png" ); thumbnails.of( "images/a380_1280x1024.jpg" ) .size( 1280 , 1024 ) .tooutputstream(os); |
9、輸出到bufferedimage
1
2
3
4
5
|
//asbufferedimage()返回bufferedimage bufferedimagethumbnail=thumbnails.of( "images/a380_1280x1024.jpg" ) .size( 1280 , 1024 ) .asbufferedimage(); imageio.write(thumbnail, "jpg" ,newfile( "c:/a380_1280x1024_bufferedimage.jpg" )); |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。