JAVA给图片加水印(无损原图片)

添加水印的类(分添加文字和图片)
package com.zone.aquila.web.upload;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class WaterPress {
private static List logoImgs;

private static List bgImgs;

private static boolean flag;

public static List getBgImgs() {
return bgImgs;
}

/**
* 把图片印刷到图片上
*
* @param pressImg --
* 水印文件
* @param targetImg --
* 目标文件
* @param x
* --x坐标
* @param y
* --y坐标
* @param out
* 输出流(可以来自HttpServletReponse的输出)
*/
public final static void pressImage(String pressImg, String targetImg,
int x, int y, OutputStream out) {
try {
// 目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);

// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao) / 2,
(height - height_biao) / 2, wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 打印文字水印图片
*
* @param pressText
* --文字
* @param targetImg --
* 目标图片
* @param fontName --
* 字体名
* @param fontStyle --
* 字体样式
* @param color --
* 字体颜色
* @param fontSize --
* 字体大小
* @param x --
* 偏移量(从右下角算起)
* @param y --
* 偏移量(从右下角算起)
*
* @param out
* 输出流(可以来自HttpServletReponse的输出)
*/

public static void pressText(String pressText, String targetImg,
String fontName, int fontStyle, int color, int fontSize, int x,
int y, OutputStream out) {
try {
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
//只有图片的宽或高大于200的时候才添加水印(小图片不添加)
if (wideth > 200 || height > 200) {
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
g.setColor(new Color(color));
g.setFont(new Font(fontName, fontStyle, fontSize));
g.drawString(pressText, wideth - fontSize - x, height
- fontSize / 2 - y);
g.dispose();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
}


水印过滤器

package com.zone.aquila.filter;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import com.zone.aquila.web.upload.WaterPress;
import com.zone.aquila.web.upload.WaterMark;

public class WaterMarkFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;

// Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
// 获取请求中的图片路径
String filePath = req.getServletPath();// 这是相对路径(并且包括了上下文路径)
String fileRealPath = filterConfig.getServletContext().getRealPath(
filePath);// 得到绝对路径
// 调用工具类加水印(由于是动态从流中加水印,因此不会修改服务器上原图片)
WaterPress.pressText("http://www.myzone.com", fileRealPath, "宋体",1,Color.ORANGE.getRGB(), 15, 180, 15, response.getOutputStream());
("http://www.myzone.com",fileRealPath,"宋体",2,1,22,50,50);
filterChain.doFilter(request, response);
}

// Clean up resources
@Override
public void destroy() {
}
}

web.xml配置


waterMarkFilter

com.zone.aquila.filter.WaterMarkFilter



waterMarkFilter


/zone_picture/zone_photo/*

你可能感兴趣的:(java)