Java 实现图片水印之水印图片(SpringMVC + Jsp)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java 实现图片水印之水印图片(SpringMVC + Jsp),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5162字,纯文字阅读大概需要8分钟。
内容图文
图片水印不仅水印文字还可以是图片,本次实现图片水印之水印图片,以后有需要可以用个代码批量处理自己的图片了。
大家先看效果图
代码编写
1.导入相关架包
2.配置XML文件
web.xml
<servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
springmvc.xml
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<context:component-scan base-package="com.wenteryan"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix" value="/"></property>
<propertyname="suffix" value=".jsp"></property>
<propertyname="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<propertyname="defaultEncoding" value="UTF-8"></property>
<propertyname="maxUploadSize" value="10485760000"></property>
<propertyname="maxInMemorySize" value="40960"></property>
</bean>
3.编写服务类接口
ImageMarkService.java
public
static
final String LOGO = "logo.png" ;
public String watermark(CommonsMultipartFile file, String uploadPath,
String realUploadPath) ;
4.编写服务类实现
UploadService.java
@Service
publicclass 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 = newbyte[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() ;
}
}
ImageMarkServiceImpl.java
@Service
public
class
ImageMarkServiceImpl
implements
ImageMarkService {
@Override
public String watermark(CommonsMultipartFile file, String uploadPath, String realUploadPath) {
// TODO Auto-generated method stub
String logoFileName = "logoImage"+file.getOriginalFilename() ;
OutputStream os = null ;
String logoPath = realUploadPath+"/"+LOGO ;
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) ;
File logo = new File(logoPath) ;
Image imageLogo = ImageIO.read(logo) ;
int width1 = imageLogo.getWidth(null) ;
int height1 = imageLogo.getHeight(null) ;
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.drawImage(imageLogo, x, y, null) ;
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;
}
}
5.编写Action
WaterMarkAction.java
@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 = imageMarkService.watermark(file, uploadPath, realUploadPath) ;
ModelAndView ret = new ModelAndView() ;ret.addObject("imageUrl", imageUrl) ;ret.addObject("logoImageUrl", logoImageUrl) ;ret.setViewName("watermark");
return ret;
}
6.编写页面
watermark.jsp
<divclass="panel-body">
<imgclass="img-responsive img-rounded"src="${pageContext.request.contextPath}${imageUrl }"/>
<imgclass="img-responsive img-rounded"src="${pageContext.request.contextPath}${logoImageUrl }"/>
<aclass="btn btn-warning"href="${pageContext.request.contextPath }">返回</a></div>
总结
Java实现水印还是非常方便的,会了水印图片与水印文字,以后有需要可以用这个代码批量处理自己的图片了。棒棒的!!!!!!
原文:http://blog.csdn.net/wenteryan/article/details/51362384
内容总结
以上是互联网集市为您收集整理的Java 实现图片水印之水印图片(SpringMVC + Jsp)全部内容,希望文章能够帮你解决Java 实现图片水印之水印图片(SpringMVC + Jsp)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。