图片自动加水印


 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 = 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 = out.toByteArray();
54 out.close();
55 return data;
56 }
57 }

图片自动加水印,码迷,mamicode.com

时间: 2024-10-07 08:54:05

图片自动加水印的相关文章

织梦/dedecms 当文章转载时不需要设置图片水印的设置,取消’图片是否加水印‘的复选框,并且修改如下文件即可生效

当想添加水印是选中“图片是否加水印”复选框即可. 找到include/helpers/image.helper.php这个文件,在里面找到中的if( isset($GLOBALS['needwatermark']) )在下面添加如下代码:else{   $photo_markup=$photo_markdown='0';}

用js实现图片自动加载的瀑布流效果

向下滑动网页的时候能够自动加载图片并显示. 盛放图片的盒子模型如下: <div class="box"> <div class="box_img"> <img src="Img/8.jpg"> </div> </div> 设置img-width为150px,然后box_img添加内边距和阴影效果,box的外边距为0,添加内边距.盒子的宽度是由img-width和边距撑开的.也就是说盒子之

java:给图片上加水印

import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import javax.swing.ImageIcon; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.J

Dedecms文章图片自动加标题 alt标签的方法

找到目录:include中的arc.archives.class.php搜索: //设置全局环境变量 $this->Fields['typename'] = $this->TypeLink->TypeInfos['typename']; @SetSysEnv($this->Fields['typeid'],$this->Fields['typename'],$this->Fields['id'],$this->Fields['title'],'archives')

php之图片处理类缩略图加水印

用到两个image系统函数 imagecopymerge — 拷贝并合并图像的一部分 imagecopyresampled — 重采样拷贝部分图像并调整大小 /* 如何知道图片的大小和类型 无法确认调用函数:Imagecreatefrompng/jpeg…… 可以独处图片的宽和高 相当于宽高是已知的 一个重要的函数getimagesize() */ /* 想操作图片 先把图片的大小,类型信息得到 水印:就是把指定的水印复制到目标上,并加透明效果 缩略图:就是把大图片复制到小尺寸画面上 */ cl

ps技术--批量给图片加水印

在日常的办公过程中,对于一些比较重要的文件的扫描件需要特殊处理,这时我们就需要给它们加上水印,保证它们的用途唯一,而这些扫描件很多,不可能一一给他们加水印,所以为提高工作效率,我们就可以使用一些小软件处理,或者使用ps的批处理功能,接下来,给大家分享一些批量水印的制作过程. 在photoshop这款软件中,通过新建动作组和新建动作并记录为单张图片添加水印的步骤流程,可保持该动作记录并一次性为图片批量添加水印,所添加水印图片最好保持大小一致以防出现部分图片水印过大的现象.(借用一下百度的图片,向百

ASP.NET图片上传,加水印文字和水印图片!

看了清清月儿的这篇文章让自己受益匪浅,但是觉得还有一些问题.上传图片后还有原来的图片文件存在,觉得这样很不爽,调用file类的delete方法删除原来没有生成水印的图片另外自己又加了一个限制图片大小的函数 1.最简单的单文件上传(没花头) 效果图:说明:这是最基本的文件上传,在asp.net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用.其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本

thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印

今天分享一下thinkphp 3.2.3整合ueditor 1.4,给上传的图片加水印.博主是新手,在这里卡住了很久(>_<) thinkphp 3.2.3整合ueditor 1.4 下载地址:https://github.com/Nintendov/Ueditor-thinkphp 下载下来,看着配置就可以了. 下面就是给上传图片加水印: (在做这步前,请确保ueditor已经正常工作) 我的工程目录如下: fonts里面的fz.fft为水印字体 images里面的logo.png为水印图片

java 图片加水印,设置透明度。说明很详细

package com.yidao.common; import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.Out