图片水印不仅水印文字还可以是图片,本次实现图片水印之水印图片,以后有需要可以用个代码批量处理自己的图片了。
大家先看效果图
代码编写
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">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="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
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() ;
}
}
ImageMarkService .java
public interface ImageMarkService {
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 static final String LOGO = "logo.png" ;
public String watermark(CommonsMultipartFile file, String uploadPath,
String realUploadPath) ;
}
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
<div class="panel-body">
<img class="img-responsive img-rounded" src="${pageContext.request.contextPath}${imageUrl }"/>
<img class="img-responsive img-rounded" src="${pageContext.request.contextPath}${logoImageUrl }"/>
<a class="btn btn-warning" href="${pageContext.request.contextPath }">返回</a>
</div>
总结
Java实现水印还是非常方便的,会了水印图片与水印文字,以后有需要可以用这个代码批量处理自己的图片了。棒棒的!!!!!!