服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

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/

延伸 · 阅读

精彩推荐