首页 > 代码库 > 图片自动加水印
图片自动加水印
1 package cn.tfri.filter.watermark; 2 3 import java.io.IOException; 4 5 import javax.servlet.Filter; 6 import javax.servlet.FilterChain; 7 import javax.servlet.FilterConfig; 8 import javax.servlet.ServletException; 9 import javax.servlet.ServletRequest; 10 import javax.servlet.ServletResponse; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 public class WaterMarkFilter implements Filter{ 15 16 private boolean enabled; 17 private String waterMarkFile; 18 19 public boolean isEnabled() { 20 return enabled; 21 } 22 public void setEnabled(boolean enabled) { 23 this.enabled = enabled; 24 } 25 public String getWaterMarkFile() { 26 return waterMarkFile; 27 } 28 public void setWaterMarkFile(String waterMarkFile) { 29 this.waterMarkFile = waterMarkFile; 30 } 31 32 public void destroy() {} 33 34 public void doFilter(ServletRequest req, ServletResponse resp, 35 FilterChain chain) throws IOException, ServletException { 36 HttpServletRequest request = (HttpServletRequest)req; 37 HttpServletResponse response = (HttpServletResponse)resp; 38 if(this.isEnabled()){ 39 WaterMarkResponseWrapper waterMarkRes = new WaterMarkResponseWrapper(response,this.getWaterMarkFile()); 40 chain.doFilter(request, waterMarkRes); 41 waterMarkRes.finishResponse(); 42 } 43 chain.doFilter(request, response); 44 } 45 46 public void init(FilterConfig filterConfig) throws ServletException { 47 this.setEnabled("true".equalsIgnoreCase(filterConfig.getInitParameter("enabled").trim())); 48 String file = filterConfig.getInitParameter("waterMarkFile"); 49 this.setWaterMarkFile(filterConfig.getServletContext().getRealPath(file)); 50 } 51 52 }
1 package cn.tfri.filter.watermark; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletOutputStream; 6 import javax.servlet.http.HttpServletResponse; 7 import javax.servlet.http.HttpServletResponseWrapper; 8 9 import cn.tfri.hibernate.utils.ImageUtil; 10 11 public class WaterMarkResponseWrapper extends HttpServletResponseWrapper{ 12 private String waterMarkFile; 13 private HttpServletResponse response; 14 private WaterMarkOutputStream waterMarkOutputStream = new WaterMarkOutputStream(); 15 16 public String getWaterMarkFile() { 17 return waterMarkFile; 18 } 19 public void setWaterMarkFile(String waterMarkFile) { 20 this.waterMarkFile = waterMarkFile; 21 } 22 public HttpServletResponse getResponse() { 23 return response; 24 } 25 public void setResponse(HttpServletResponse response) { 26 this.response = response; 27 } 28 public WaterMarkOutputStream getWaterMarkOutputStream() { 29 return waterMarkOutputStream; 30 } 31 public void setWaterMarkOutputStream(WaterMarkOutputStream waterMarkOutputStream) { 32 this.waterMarkOutputStream = waterMarkOutputStream; 33 } 34 35 public WaterMarkResponseWrapper(HttpServletResponse response,String waterMarkFile){ 36 super(response); 37 this.setResponse(response); 38 this.setWaterMarkFile(waterMarkFile); 39 } 40 41 public ServletOutputStream getOutputStream(){ 42 return this.getWaterMarkOutputStream(); 43 } 44 @Override 45 public void flushBuffer() throws IOException { 46 this.getWaterMarkOutputStream().flush(); 47 } 48 49 public void finishResponse() throws IOException{ 50 byte[] imageData = http://www.mamicode.com/this.getWaterMarkOutputStream().getByteArrayOutputStream().toByteArray(); 51 byte[] image = ImageUtil.waterMark(imageData, this.getWaterMarkFile()); 52 response.setContentLength(image.length); 53 response.getOutputStream().write(image); 54 this.getWaterMarkOutputStream().close(); 55 } 56 }
1 package cn.tfri.filter.watermark; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 6 import javax.servlet.ServletOutputStream; 7 8 public class WaterMarkOutputStream extends ServletOutputStream{ 9 private ByteArrayOutputStream byteArrayOutputStream; 10 11 public ByteArrayOutputStream getByteArrayOutputStream() { 12 return byteArrayOutputStream; 13 } 14 public void setByteArrayOutputStream(ByteArrayOutputStream byteArrayOutputStream) { 15 this.byteArrayOutputStream = byteArrayOutputStream; 16 } 17 18 public WaterMarkOutputStream(){ 19 this.setByteArrayOutputStream(new ByteArrayOutputStream()); 20 } 21 22 @Override 23 public void write(int b) throws IOException { 24 this.getByteArrayOutputStream().write(b); 25 } 26 @Override 27 public void close() throws IOException { 28 this.getByteArrayOutputStream().close(); 29 } 30 @Override 31 public void flush() throws IOException { 32 this.getByteArrayOutputStream().flush(); 33 } 34 @Override 35 public void write(byte[] b, int off, int len) throws IOException { 36 this.getByteArrayOutputStream().write(b, off, len); 37 } 38 @Override 39 public void write(byte[] b) throws IOException { 40 this.getByteArrayOutputStream().write(b); 41 } 42 }
1 package cn.tfri.hibernate.utils; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.Image; 7 import java.awt.image.BufferedImage; 8 import java.io.ByteArrayOutputStream; 9 import java.io.File; 10 import java.io.IOException; 11 12 import javax.imageio.ImageIO; 13 import javax.swing.ImageIcon; 14 15 import com.sun.image.codec.jpeg.JPEGCodec; 16 import com.sun.image.codec.jpeg.JPEGImageEncoder; 17 18 public class ImageUtil { 19 public static byte[] waterMark(byte[] imageData,String waterMarkFiler) throws IOException{ 20 int paddingRight = 10; 21 int paddingBottom = 10; 22 23 Image image = new ImageIcon(imageData).getImage(); 24 int imageWidth = image.getWidth(null); 25 int imageHeight = image.getHeight(null); 26 27 Image waterMark = ImageIO.read(new File(waterMarkFiler)); 28 int waterMarkWidth = waterMark.getWidth(null); 29 int waterMarkHeight = waterMark.getHeight(null); 30 31 if(imageWidth < waterMarkWidth + 2*paddingRight || imageHeight < waterMarkHeight + 2*paddingBottom){ 32 return imageData; 33 } 34 35 BufferedImage bufferedImage = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB); 36 Graphics g = bufferedImage.createGraphics(); 37 g.drawImage(image, 0, 0, imageWidth, imageHeight, null); 38 g.drawImage(waterMark,imageWidth - waterMarkWidth - paddingRight,imageHeight - waterMarkHeight - paddingBottom,waterMarkWidth,waterMarkHeight,null); 39 //设置图形上下文的当前颜色为红色 40 g.setColor(Color.red); 41 //创建新的字体 42 Font font = new Font("华文行楷",Font.BOLD,12); 43 //设置图形上下文的字体为指定字体 44 g.setFont(font); 45 //在图片上绘制文字,文字的颜色为图形上下文的当前颜色,即红色 46 g.drawString("上海烛龙-古剑奇谭", imageWidth - 200, imageHeight - 200); 47 g.dispose(); 48 49 ByteArrayOutputStream out = new ByteArrayOutputStream(); 50 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 51 encoder.encode(bufferedImage); 52 53 byte[] data =http://www.mamicode.com/ out.toByteArray(); 54 out.close(); 55 return data; 56 } 57 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。