激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

香港云服务器
服務器之家 - 編程語言 - Java教程 - Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作實例代碼

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

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

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

概述

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

本文內(nèi)容腦圖如下:

Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作實例代碼
本文內(nèi)容腦圖

搭建 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); // 設置水印圖片的旋轉(zhuǎn)度
 
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 ) { // 循環(huán)添加多個水印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;
}
 
}

代碼思路解釋如下:

可以對照代碼中的標示數(shù)字和下面的解釋進行理解:

① 創(chuàng)建緩存圖片

② 創(chuàng)建繪圖工具

③ 將原圖繪制到緩存圖片

④ 將水印logo繪制到緩存圖片

⑤ 創(chuàng)建圖像編碼工具類

⑥ 編碼緩存圖像生成目標圖片

可見思路清晰易懂!

編寫 圖片上傳/處理 控制器

我們在該控制器代碼中將上述的 圖片上傳服務 和 圖片加水印服務 給用起來:

?
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工具來輔助我們發(fā)出 localhost:9999/watermarktest 請求,進行圖片上傳的操作:

Spring Boot實現(xiàn)圖片上傳/加水印一把梭操作實例代碼
postman發(fā)請求進行圖片上傳

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

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

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

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

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

總結

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

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

延伸 · 閱讀

精彩推薦
698
主站蜘蛛池模板: 日日cao | 国产黄色一区二区 | 亚洲成人福利电影 | 毛片视频网站在线观看 | 久久久精品网 | 国产91久久久久久 | 久久艳片 | 欧美一级高潮片免费的 | 久久亚色 | 久久久无码精品亚洲日韩按摩 | 色屁屁xxxxⅹ免费视频 | fc2国产成人免费视频 | 91成人在线免费 | 亚洲极色| 欧美成人精品h版在线观看 国产一级淫片在线观看 | 亚洲小视频在线 | 日本不卡视频在线观看 | 国产一级桃视频播放 | 欧美一级做 | 欧美a在线观看 | 欧产日产国产精品乱噜噜 | 日韩不卡一区二区 | 亚洲第一成人久久网站 | 一级黄色免费 | 另类亚洲孕妇分娩网址 | 久久91亚洲精品久久91综合 | 九九热免费视频在线观看 | 麻豆911| 黄色成年在线观看 | 精品国产一区二区三区久久久蜜月 | 久久久久久99| 国产精品久久久久久久久久久久久久久 | 国产jjizz一区二区三区视频 | 亚洲免费毛片基地 | 欧美日韩在线免费观看 | 一级黄片毛片免费看 | www.精品一区 | 黄视频在线网站 | 一区二区三区日韩 | 久久成人在线观看 | 成人综合区一区 |