国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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/

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 国产精品欧美一区二区三区 | 精品无码久久久久国产 | 午夜影晥| 欧美成人精品一区二区男人看 | 亚洲黄色在线视频 | 久久久久国产精品免费免费搜索 | 日韩精品一区二区三区中文字幕 | 亚洲日日摸夜夜夜夜夜爽小说 | av国产精品 | 亚洲成人av在线 | 蜜桃一二三区 | 国产精品久久久久久久久福交 | 色视频免费在线观看 | 国产1区2区3区 | 91免费在线视频观看 | 中文字幕在线看 | 国产亚洲精品精品国产亚洲综合 | 亚洲成人av一区二区三区 | 国产精品自拍视频 | h色视频在线观看 | 成人在线小视频 | 免费网站色 | 凹凸日日摸日日碰夜夜爽孕妇 | 成人久久久久久久久 | 日韩一区二区视频 | 婷婷激情久久 | 97色在线观看免费视频 | 欧美一区二区在线播放 | 国产乱码精品一区二区三区中文 | 欧美一区2区 | 99精品99| 成人免费激情视频 | 国产精品久久久久久吹潮 | 国产中文字幕观看 | 亚洲国产一级毛片 | 91中文在线 | 国产精品视频免费看 | 国变精品美女久久久久av爽 | 久久久夜夜夜 | 日本一区二区三区免费观看 | 亚洲一区二区三区视频 |