java从前端获取参数添加到图片后返回整张图片
以后为整个servlet代码,请自行加入到j2ee工程。
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;
/**
*
* java从前端获取参数添加到图片后返回整张图片
*
* @author gu
* @version 1.0
*/
public class PictureServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -2243668400971698202L;
/**
* 日志
*/
private static final Logger logger = Logger.getLogger(PictureServlet.class);
@SuppressWarnings("deprecation")
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8"); //微信服务器POST消息时用的是UTF-8编码,在接收时也要用同样的编码,否则中文会乱码;
response.setCharacterEncoding("UTF-8"); //在响应消息(回复消息给用户)时,也将编码方式设置为UTF-8,原理同上;
PrintWriter writer = response.getWriter();
String imgFile = request.getRealPath("") + "/WEB-INF/1.jpg";
try {
String parameter = request.getParameter("parameter");
String responseHtml=null;
if(parameter!=null&&!parameter.equals("")&&!parameter.equals("undefined")){
String base64Img = getImageStr(imgFile,parameter);
if(base64Img!=null){
responseHtml=(
"中文"
+ "
");
}
}
if(responseHtml==null){
responseHtml="中文"
+ "暂无图片。";
}
writer.write(responseHtml);
logger.info("generate picture success.");
} catch (Exception e) {
logger.error("info error:" + e.getMessage(), e);
}
}
/**
* @Description: 根据图片地址转换为base64编码字符串
* @return
*/
@SuppressWarnings("restriction")
public static String getImageStr(String imgFile, String parameter) {
ByteArrayOutputStream os =null;
try {
// 读取原图片信息
File srcImgFile = new File(imgFile);// 得到文件
BufferedImage image = ImageIO.read(srcImgFile);// 文件转化为图片
Graphics g = image.getGraphics();
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("微软雅黑 Light", Font.BOLD, 30);
g.setFont(font);
g.setColor(Color.black);
font = new Font("Bahnschrift SemiLight", Font.PLAIN, 66);
g.setFont(font);
g.drawString(parameter, 350, 756);
font = new Font("微软雅黑 Light", Font.BOLD, 45);
g.setFont(font);
g.drawString("kg", 535, 633);
g.drawString("cm", 535, 756);
g.dispose();
image.flush();
os = new ByteArrayOutputStream();// 新建流。
ImageIO.write(image, "jpg", os);// 利用ImageIO类提供的write方法,将bi以图片的数据模式写入流。
byte b[] = os.toByteArray();// 从流中获取数据数组。
String base64String = new BASE64Encoder().encode(b);
os.close();
return base64String;
} catch (Exception e) {
logger.error("gererate picture error:"+e.getMessage(),e);
}finally{
try {
os.close();
} catch (IOException e) {
logger.error("close ByteArrayOutputStream error:"+e.getMessage(),e);
}
}
return null;
}
}