看之前要先对SpringMVC进行了解打好基础,下面直接先看效果图

代码编写

1.导入相关架包

2.配置文件

web.xml

watermarkspringmvc

dispatcherServlet

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

dispatcherServlet

/

index.jsp

springmvc.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

3.编写action

WatermarkAction .action

package com.wenteryan.watermarkspringmvc;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import org.springframework.web.servlet.ModelAndView;

import com.wenteryan.service.MarkService;

import com.wenteryan.service.UploadService;

@Controller

public class WatermarkAction {

private MarkService mackService ;

private UploadService uploadService ;

@RequestMapping(value="/watermark", method=RequestMethod.POST)

public ModelAndView watermark(

@RequestParam("image")CommonsMultipartFile file, HttpSession session) throws Exception {

String uploadPath = "/images" ;

String realUploadPath = session.getServletContext().getRealPath(uploadPath) ;

String imageUrl = uploadService.uploadImage(file, uploadPath, realUploadPath) ;

String logoImageUrl = mackService.watermark(file, uploadPath, realUploadPath) ;

ModelAndView ret = new ModelAndView() ;

ret.addObject("imageUrl", imageUrl) ;

ret.addObject("logoImageUrl", logoImageUrl) ;

ret.setViewName("watermark");

return ret ;

}

@Autowired

public void setMackService(MarkService mackService) {

this.mackService = mackService;

}

@Autowired

public void setUploadService(UploadService uploadService) {

this.uploadService = uploadService;

}

}

4.编写服务类

MarkService .java

package com.wenteryan.service;

import java.awt.Color;

import java.awt.Font;

import java.io.File;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public interface MarkService {

public static final String MARK_TEXT = "wenteryan" ;

public static final String FONT_NAME = "微软雅黑" ;

public static final int FONT_SIZE = 120 ;

public static final int FONT_STYPE = Font.BOLD ;

public static final Color FONT_COLOR = Color.RED ;

public static final int X = 10 ;

public static final int Y = 10 ;

public static float ALPHA = 0.3F ;

public String watermark(CommonsMultipartFile file, String uploadPath,

String realUploadPath) ;

}

5.编写接口实现类

UploadService .java

package com.wenteryan.service.impl;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Service

public class UploadService {

public String uploadImage(CommonsMultipartFile file, String uploadPath, String realUploadPath) {

InputStream is = null ;

OutputStream os = null ;

try {

is = file.getInputStream() ;

os = new FileOutputStream(realUploadPath+"/"+file.getOriginalFilename()) ;

byte[] buffer = new byte[1024] ;

int len = 0 ;

while((len=is.read(buffer))>0) {

os.write(buffer) ;

}

} catch(Exception e) {

e.printStackTrace() ;

} finally {

if(is!=null) {

try {

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if(os!=null) {

try {

os.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return uploadPath+"/"+file.getOriginalFilename() ;

}

}

MarkServiceImpl .java

package com.wenteryan.service.impl;

import java.awt.AlphaComposite;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

import com.wenteryan.service.MarkService;

@Service

public class MarkServiceImpl implements MarkService {

@Override

public String watermark(CommonsMultipartFile file, String uploadPath, String realUploadPath) {

// TODO Auto-generated method stub

String logoFileName = "logo"+file.getOriginalFilename() ;

OutputStream os = null ;

try {

Image image2 = ImageIO.read(file.getInputStream()) ;

int width = image2.getWidth(null) ;

int height = image2.getHeight(null) ;

BufferedImage bufferImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) ;

Graphics2D g = bufferImage.createGraphics() ;

g.drawImage(image2, 0, 0, width, height, null) ;

g.setFont(new Font(FONT_NAME,FONT_STYPE,FONT_SIZE));

g.setColor(FONT_COLOR) ;

int width1 = FONT_SIZE*getTextLength(MARK_TEXT) ;

int height1 = FONT_SIZE ;

int widthDiff = width-width1 ;

int heightDiff = height-height1 ;

int x = X ;

int y = Y ;

if(x>widthDiff) {

x = widthDiff ;

}

if(y>heightDiff) {

y=heightDiff ;

}

g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, ALPHA));

g.drawString(MARK_TEXT, x, y+FONT_SIZE) ;

g.dispose() ;

os = new FileOutputStream(realUploadPath+"/"+logoFileName) ;

JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os) ;

en.encode(bufferImage) ;

} catch(Exception e) {

e.printStackTrace() ;

} finally {

if(os!=null) {

try {

os.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return uploadPath+"/"+logoFileName;

}

public int getTextLength(String text) {

int length = text.length();

for(int i=0; i

String s = String.valueOf(text.charAt(i)) ;

if(s.getBytes().length>1) {

length++ ;

}

}

length = length%2==0?length/2:length/2+1 ;

return length ;

}

}

6.编写页面

index.jsp

请选择上传的图片

开始上传

watermark.jsp

返回

总结

Java有专门Image的处理包,同样应该可以实现水印功能,查了资料小试下来发现java实现水印还是非常方便的,水印可以是图片或者文字,后期会有水印图片水印,以后有需要可以写个代码批量处理自己的图片了。

以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。

jsp text 水印_java实现图片上加文字水印(SpringMVC + Jsp)相关推荐

  1. VUE 图片上加文字水印

    我这里用的是uniapp上传方法 上传的方法 uni.chooseImage({count: 6, //默认9sizeType: ['original', 'compressed'], //可以指定是 ...

  2. 不用PS,如何给多图片批量加文字水印?

    给图片加上文字水印是我们在工作中经常遇到的事情,比如将图片上传到网站上,为了防止别人盗用自己的图片,我们都习惯在图片上加上水印,文字水印或者图片水印,我就经常在工作中需要给图片加水印. 一个图片加上文 ...

  3. PHP 合成图片并在图片上加文字

    /*** Info: PHP把一张图片作为背景和另一张图片合成新图片*/public function createImage(){$path_1 = 'XXXXX/attachment/images ...

  4. php 图片上加文字,php使用GD库实现图片上添加文字的方法(代码)

    本篇文章给大家带来的内容是关于php使用GD库实现图片上添加文字的方法(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 我们可以利用php的gd库扩展来对我们的图片进行处理,例 ...

  5. Python 给图片上加文字

    import PIL from PIL import ImageFont import PIL from PIL import ImageFont from PIL import Image from ...

  6. html图片上加水印,如何在图片上加水印(给照片和视频加上自己的专属水印)...

    加百思特网水印现在真的太重要了,自百思特网己辛辛苦苦拍的照片或视频,被别人随便一转,不注明出处和原作者,就非常容易误导观者这些照片是"转载者"拍的,这对原作者实在太不公平了.如果不 ...

  7. python怎么画简单图片-初学Python-简单的在图片上加文字

    场景 在用户运营中,拉新是第一步.我们产品打算先再小范围试验一下效果,不动用开发哥哥,自己制作邀请海报. 没错,就是最简单的,邀请领奖励活动. UI妹妹把海报模版做出来,邀请码根据用户ID自行填上. ...

  8. Java实现在图片上添加文字(水印)

    今天分享一个:通过Java代码,给图片添加文字. 比如下面这个图片,我们在左下角就添加了一个文字版的水印,那么这是如何实现的呢 ? 目录 [1]获取原图片对象 (1.1)读取本地图片 (1.2)读取网 ...

  9. python写文字方法_初学Python-简单的在图片上加文字

    场景 在用户运营中,拉新是第一步.我们产品打算先再小范围试验一下效果,不动用开发哥哥,自己制作邀请海报. 没错,就是最简单的,邀请领奖励活动. UI妹妹把海报模版做出来,邀请码根据用户ID自行填上. ...

最新文章

  1. 自动生成低精度深度学习算子
  2. 【数据安全案例】车管信息再遭窃取,数据安全缺乏保障
  3. 1.2 初窥输入输出、控制语句
  4. Angular 学习笔记——service constant
  5. mfs分布式存储+master端高可用
  6. 创纪录!浪潮云海完成OpenStack Rocky版本全球最大规模单一集群测试!
  7. python三层设计_python 三层架构说明
  8. java验证ie版本的正则表达式_JS 浏览器类型判断,IE版本类型判断,正则表达式判断。...
  9. 【Python】指定【微信】好友自动发送消息
  10. 如何正确下载安全无毒的局域网、内网即时通讯软件
  11. 战胜25名医生:AI真能成为医疗界的“擂主”?
  12. 经验谈:调查问卷问题设计“六忌”
  13. requests+selenium 爬取企查查网,1000+条数据轻轻松松
  14. linux ---vim编辑用户管理权限
  15. Three.js实现的网站页面金字塔模型显示
  16. 外观模式(Facade)----设计模式
  17. App自动化02-元素定位、显示与隐式等待、常见控件交互方法
  18. [C++题目] 根据快递商品的重量计算应付运费用
  19. mysql 根据身份证号更新年龄
  20. DSS:甲基化差异分析

热门文章

  1. 基于微信在线电子书阅读小程序设计与实现开题答辩PPT
  2. 深度学习平台-百度飞桨
  3. 分布式共识算法丨Raft丨Raft-Extended 论文翻译
  4. CCF201809-4 再卖菜
  5. 电热玻璃水壶CE认证检测标准介绍
  6. 【知识图谱】语义网络,语义网,链接数据和知识图谱
  7. 基于matlab山脊线,教你如何利用水文,分析提取山脊线山谷线
  8. 求过圆心直线与圆的两个交点
  9. 每天一个医药小故事②神农和药王
  10. 绘制热力图seaborn.heatmap,cmap设置颜色的参数