概述
很多網站的圖片為了版權考慮都加有水印,尤其是那些圖片類網站。自己正好最近和圖片打交道比較多,因此就探索了一番基于 spring boot這把利器來實現從 圖片上傳 → 圖片加水印 的一把梭操作!
本文內容腦圖如下:
本文內容腦圖
搭建 spring boot基礎工程
過程不再贅述了,這里給出 pom中的關鍵依賴:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version> 2.5 </version> </dependency> </dependencies> |
編寫文件上傳服務
主要就是編寫 imageuploadservice 服務
里面僅一個上傳圖片的方法:uploadimage 方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 功能:上傳圖片 * @param file 文件 * @param uploadpath 服務器上上傳文件的路徑 * @param physicaluploadpath 服務器上上傳文件的物理路徑 * @return 上傳文件的 url相對地址 */ public string uploadimage( multipartfile file, string uploadpath, string physicaluploadpath ) { string filepath = physicaluploadpath + file.getoriginalfilename(); try { file targetfile= new file(filepath); fileutils.writebytearraytofile(targetfile, file.getbytes()); } catch (ioexception e) { e.printstacktrace(); } return uploadpath + "/" + file.getoriginalfilename(); } } |
編寫圖片加水印服務
編寫 imagewatermarkservice 服務
里面就一個主要的 watermarkadd方法,代碼后面寫有詳細解釋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
@service public class imagewatermarkservice { /** * imgfile 圖像文件 * imagefilename 圖像文件名 * uploadpath 服務器上上傳文件的相對路徑 * realuploadpath 服務器上上傳文件的物理路徑 */ public string watermarkadd( file imgfile, string imagefilename, string uploadpath, string realuploadpath ) { string imgwithwatermarkfilename = "watermark_" + imagefilename; outputstream os = null ; try { image image = imageio.read(imgfile); int width = image.getwidth( null ); int height = image.getheight( null ); bufferedimage bufferedimage = new bufferedimage(width,height,bufferedimage.type_int_rgb); // ① graphics2d g = bufferedimage.creategraphics(); // ② g.drawimage(image, 0 , 0 , width,height, null ); // ③ string logopath = realuploadpath + "/" + const .logo_file_name; // 水印圖片地址 file logo = new file(logopath); // 讀取水印圖片 image imagelogo = imageio.read(logo); int markwidth = imagelogo.getwidth( null ); // 水印圖片的寬度和高度 int markheight = imagelogo.getheight( null ); g.setcomposite( alphacomposite.getinstance(alphacomposite.src_atop, const .alpha) ); // 設置水印透明度 g.rotate(math.toradians(- 10 ), bufferedimage.getwidth()/ 2 , bufferedimage.getheight()/ 2 ); // 設置水印圖片的旋轉度 int x = const .x; int y = const .y; int xinterval = const .x_interval; int yinterval = const .y_interval; double count = 1.5 ; while ( x < width*count ) { // 循環添加多個水印logo y = -height / 2 ; while ( y < height*count ) { g.drawimage(imagelogo, x, y, null ); // ④ y += markheight + yinterval; } x += markwidth + xinterval; } g.dispose(); os = new fileoutputstream(realuploadpath + "/" + imgwithwatermarkfilename); jpegimageencoder en = jpegcodec.createjpegencoder(os); // ⑤ en.encode(bufferedimage); // ⑥ } catch (exception e) { e.printstacktrace(); } finally { if (os!= null ){ try { os.close(); } catch (ioexception e) { e.printstacktrace(); } } } return uploadpath + "/" + imgwithwatermarkfilename; } } |
代碼思路解釋如下:
可以對照代碼中的標示數字和下面的解釋進行理解:
① 創建緩存圖片
② 創建繪圖工具
③ 將原圖繪制到緩存圖片
④ 將水印logo繪制到緩存圖片
⑤ 創建圖像編碼工具類
⑥ 編碼緩存圖像生成目標圖片
可見思路清晰易懂!
編寫 圖片上傳/處理 控制器
我們在該控制器代碼中將上述的 圖片上傳服務 和 圖片加水印服務 給用起來:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@restcontroller public class watermarkcontroller { @autowired private imageuploadservice imageuploadservice; @autowired private imagewatermarkservice watermarkservice; @requestmapping (value = "/watermarktest" , method = requestmethod.post) public imageinfo watermarktest( @requestparam ( "file" ) multipartfile image ) { imageinfo imginfo = new imageinfo(); string uploadpath = "static/images/" ; // 服務器上上傳文件的相對路徑 string physicaluploadpath = getclass().getclassloader().getresource(uploadpath).getpath(); // 服務器上上傳文件的物理路徑 string imageurl = imageuploadservice.uploadimage( image, uploadpath, physicaluploadpath ); file imagefile = new file(physicaluploadpath + image.getoriginalfilename() ); string watermarkaddimageurl = watermarkservice.watermarkadd(imagefile, image.getoriginalfilename(), uploadpath, physicaluploadpath); imginfo.setimageurl(imageurl); imginfo.setlogoimageurl(watermarkaddimageurl); return imginfo; } } |
實際實驗與效果展示
我們用 postman工具來輔助我們發出 localhost:9999/watermarktest 請求,進行圖片上傳的操作:
postman發請求進行圖片上傳
之后我們再去項目的資源目錄下查看上傳的原圖 和 加完水印后圖片的效果如下:
原圖
加完水印后的圖片
喔唷,這水印 logo是不是打的有點多…
不過這下終于不用害怕別人對您的圖片侵權啦 !
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.codesheep.cn/2018/11/15/springbt-fileupload-wartermark/