一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Boot實現圖片上傳/加水印一把梭操作實例代碼

Spring Boot實現圖片上傳/加水印一把梭操作實例代碼

2021-06-14 14:37王 帥 Java教程

這篇文章主要給大家介紹了關于Spring Boot實現圖片上傳/加水印一把梭操作的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

概述

很多網站的圖片為了版權考慮都加有水印,尤其是那些圖片類網站。自己正好最近和圖片打交道比較多,因此就探索了一番基于 spring boot這把利器來實現從 圖片上傳 → 圖片加水印 的一把梭操作!

本文內容腦圖如下:

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 請求,進行圖片上傳的操作:

Spring Boot實現圖片上傳/加水印一把梭操作實例代碼
postman發請求進行圖片上傳

之后我們再去項目的資源目錄下查看上傳的原圖 和 加完水印后圖片的效果如下:

Spring Boot實現圖片上傳/加水印一把梭操作實例代碼原圖

Spring Boot實現圖片上傳/加水印一把梭操作實例代碼
加完水印后的圖片

喔唷,這水印 logo是不是打的有點多…

不過這下終于不用害怕別人對您的圖片侵權啦 !

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.codesheep.cn/2018/11/15/springbt-fileupload-wartermark/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 二区三区在线观看 | 亚洲欧美一 | 国产精品美女福利视频免费专区 | 啾咪成人漫画免费 | 国产精品探花一区在线观看 | 欧美精品三区 | 喜爱夜蒲2三级做爰 | pornohd老师18| 九九热在线视频观看这里只有精品 | 国产在线99| 国产精品久久久久毛片真精品 | 好舒服好爽再快点视频 | 青青成人在线 | 玩逼逼| caoporn草棚在线视频 | 日韩欧美国产一区 | 好猛好紧好硬使劲好大刺激视频 | 日本花季传媒2020旧版安卓 | 91赵邦贺 | www一区| 小小水蜜桃3视频在线观看 小鸟酱喷水 | 猛男深夜狂cao小男生 | 日本激情在线 | 国产乱子伦在线观看不卡 | www.87福利| 我半夜摸妺妺的奶C了她 | 性xxxxbbbbxxxx中国| 99在线视频精品 | 国产好痛疼轻点好爽的视频 | 亚洲欧美日韩国产综合专区 | 女教师被学生糟蹋三天 | 九九精品免视频国产成人 | 思思玖玖玖在线精品视频 | 亚洲日本va午夜中文字幕 | 日韩精品中文字幕久久 | 欧美另类亚洲 | 日韩理论片在线看免费观看 | 亚洲福利天堂网福利在线观看 | 精品手机在线1卡二卡3卡四卡 | xxxx野外性xxxx| 女八把屁股扒开让男生添 |