java加水印 盖章_java实现图片上加文字水印(SpringMVC + Jsp)

本文介绍如何使用Java在SpringMVC环境中添加图片水印,包括代码配置、服务类与页面交互,详细展示了从上传图片到添加文字水印的完整过程。
摘要由CSDN通过智能技术生成

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

5a7c797f495a7334698c71b0b1c2aaa5.png

代码编写

1.导入相关架包

5acb14d3b844084ea1a46ff81780987f.png

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程序设计有所帮助。

希望与广大网友互动??

点此进行留言吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值