package net.merge;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.util.ParamUtil;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 将文字用指定的字体,颜色和大小,嵌入指定图片的指定位置,调用参数:
* text: 要嵌的文字
* imageFile: JPG图片的虚拟路径
* x: 文字输出的起始X坐标位置
* y: 文字输出的起始Y坐标位置
* fontColor: 字体颜色(例fontColor=FFFFFF)
* fontSize: 字体大小
* fontStyle: 字体风格(斜体,粗体等)
* fontName: 字体名称(如仿宋体,宋体等)
*/
public class TextIntoImage extends HttpServlet {
private static final String CONTENT_TYPE = "image/jpeg;charset=GB2312";
public void init() throws ServletException {
}
/** Process the HTTP Get request */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
//---------------------------------------------------------------------------------------------
/** Process the HTTP Post request */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String text = ""; //要嵌的文字
String imageFile = ""; //被嵌的图片的虚拟路径
int x = 0; //坐标
int y = 0;
String fontColor = ""; //字体颜色
int fontSize = 0; //字体大小
String fontStyle = ""; //字体风格(斜体,粗体等)
String fontName = ""; //字体名称
try {
//取得参数(ParamUtil类请参看后面附的ParamUtil类代码)
text = ParamUtil.getParameter(request,"text");
imageFile = ParamUtil.getParameter(request,"imageFile");
x = ParamUtil.getIntParameter(request,"x",0);
y = ParamUtil.getIntParameter(request,"y",0);
fontColor = ParamUtil.getParameter(request,"fontColor");
fontSize = ParamUtil.getIntParameter(request,"fontSize",16);
fontStyle = ParamUtil.getParameter(request,"fontStyle");
fontName = ParamUtil.getParameter(request,"fontName");
}
catch(Exception e) {
e.printStackTrace();
}
ServletOutputStream output=response.getOutputStream();
if(imageFile.toLowerCase().endsWith(".jpeg")||imageFile.toLowerCase().endsWith(".jpg")) {
imageFile = getServletContext().getRealPath(imageFile);
System.out.println(imageFile);
InputStream imageIn = new FileInputStream(new File(imageFile));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage image = decoder.decodeAsBufferedImage();
Graphics g=image.getGraphics();
//设置颜色
g.setColor(new Color(Integer.parseInt(fontColor,16)));
//设置字体
Font mFont = new Font(fontName,Font.PLAIN,fontSize);//默认字体
if(fontStyle.equalsIgnoreCase("italic")) mFont=new Font(fontName,Font.ITALIC,fontSize);
if(fontStyle.equalsIgnoreCase("bold")) mFont=new Font(fontName,Font.BOLD,fontSize);
if(fontStyle.equalsIgnoreCase("plain")) mFont=new Font(fontName,Font.PLAIN,fontSize);
g.setFont(mFont);
//输出文字
g.drawString(text,x,y);
//输出数据流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(image);
imageIn.close();
}
output.close();
}
}
评论0
最新资源