/**
* 方法描述:<b>给图片增加水印.</b></br>
* 备 注: 在图片上写字符串
* 创 建 人: bo.gaobo</br>
* 创建日期: 2012-09-07</br>
* @param originalUrl 原始图片存储路径
* @param oldImg 原图片
* @param str 增加的字符串
* @param xLocation x坐标
* @param yLocation y坐标
* @param fontColor 颜色
* @param fontSize 字号
* @param typeFace 字体
* @param fileType 文件类型
*/
public static BufferedImage addStringToImg(String originalUrl, BufferedImage oldImg,String str,int xLocation,int yLocation, Color fontColor, int fontSize, String typeFace, String fileType) throws IOException{
FileOutputStream output = new FileOutputStream(originalUrl)
BufferedImage buffImg = oldImg
Graphics2D g = buffImg.createGraphics()
g = buffImg.createGraphics()
g.drawImage(buffImg, null, 0, 0)
g.setColor(fontColor)//设置字体颜色
g.setFont(new Font(typeFace, Font.PLAIN, fontSize))//设置字体和字号
g.drawString(str, xLocation, yLocation)//把字符串放在对应的坐标处
g.dispose()
ImageIO.write(buffImg, fileType, output)//设置文件类型
output.close()
return buffImg
}
用过滤器,等我上线给你,不要结问题哦package com.jc.ts.services
import java.awt.Color
import java.awt.Graphics
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.IOException
import javax.imageio.ImageIO
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import com.sun.image.codec.jpeg.ImageFormatException
import com.sun.image.codec.jpeg.JPEGCodec
import com.sun.image.codec.jpeg.JPEGImageEncoder
/**
*添加图片水印的服务类
* */
public class WaterMark {
/**
* @param sizeContext添加水印文字
* @param request 请求流对象
* @param request 响应流对象
* */
@SuppressWarnings("deprecation")
public static void createMarkSize(String sizeContext,HttpServletRequest request,HttpServletResponse response) {
try {
String path=request.getRealPath(request.getServletPath())
FileInputStream in=new FileInputStream(path)
Image src=ImageIO.read(in)
int w=src.getWidth(null)
int h=src.getHeight(null)
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB)//构建画板
Graphics g=img.getGraphics()//得到画笔
g.drawImage(src,0,0,w,h,null)//把源图片写入画板
g.setColor(Color.red)
g.drawString(sizeContext,10,5) // 添加文字
g.dispose()//生成图片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream())
e.encode(img)
response.getOutputStream().close()
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
/**
* @param localPath 添加水印LOGO路径
* @param request 请求流对象
* @param request 响应流对象
**/
@SuppressWarnings("deprecation")
public static void createMarkLogo(String localPath,HttpServletRequest request,HttpServletResponse response) {
try {
FileInputStream file=new FileInputStream(localPath)
Image fimg=ImageIO.read(file)
int fw=fimg.getWidth(null)
int fh=fimg.getHeight(null)
String path=request.getRealPath(request.getServletPath())
FileInputStream in=new FileInputStream(path)
Image src=ImageIO.read(in)
int w=src.getWidth(null)
int h=src.getHeight(null)
BufferedImage img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB)//构建画板
Graphics g=img.getGraphics()//得到画笔
g.drawImage(src,0,0,w,h,null)//把原图片写入画板
g.drawImage(fimg,w-20,h-15,fw,fh,null)//把水印图片写入画板
g.dispose()//生成图片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream())
e.encode(img)
response.getOutputStream().close()
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
/**
* @param localPath 添加水印图片路径
* @param request 请求流对象
* @param request 响应流对象
* @param width 水印图片的宽度
* @param height 水印图片的长度
**/
@SuppressWarnings("deprecation")
public static void createMarkLogo(String localPath,HttpServletRequest request,HttpServletResponse response,int width,int height) {
try {
FileInputStream file=new FileInputStream(localPath)
Image fimg=ImageIO.read(file)
int fw=fimg.getWidth(null)
int fh=fimg.getHeight(null)
String path=request.getRealPath(request.getServletPath())
FileInputStream in=new FileInputStream(path)
Image src=ImageIO.read(in)
int w=src.getWidth(null)//w为你过滤图片的宽度
int h=src.getHeight(null)//h为你过滤图片的长度
BufferedImage img=new BufferedImage(w+width,h+height,BufferedImage.TYPE_INT_RGB)//构建画板(画板的宽度为两个图片之和)
Graphics g=img.getGraphics()//得到画笔
g.drawImage(src,0,0,w,h,null)//把原图片写入画板
g.drawImage(fimg,width,height,fw,fh,null)//把水印图片写入画板
g.dispose()//生成图片
JPEGImageEncoder e=JPEGCodec.createJPEGEncoder(response.getOutputStream())
e.encode(img)
response.getOutputStream().close()
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (ImageFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
注意第三个方法的注释地方g.drawImage(fimg,width,height,fw,fh,null)根据参数你在调调(放原图下面的)
过滤器调用
package com.jc.ts.filter
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.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import com.jc.ts.services.WaterMark
public class WaterFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)arg0
HttpServletResponse response=(HttpServletResponse)arg1
//WaterMark.createMarkSize("南京ts", request, response)
//WaterMark.createMarkLogo("D:\\workspace\\mybook\\WebRoot\\images\\logo\\book.jpg", request, response)
WaterMark.createMarkLogo("D:\\workspace\\mybook\\WebRoot\\images\\logo\\book.jpg", request, response,20,30)
//注意路径为绝对路径且三个效果不能同时执行
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
web.xml配置(写在servlet上面)
<filter>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<filter-name>WaterFilter</filter-name>
<filter-class>com.jc.ts.filter.WaterFilter</filter-class>
</filter>
希望你能满意。。。。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)