The java source code is following:
/*
* @#ValidateCodeImage.java-根据初始参数设置生成随即码图片
*/
package com.hib.img;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 根据初始参数设置生成随即码图片
* @author wengm
* @version 1.0.1
*
*/
public class ValidateCodeImage extends HttpServlet {
/**
* 序列号
*/
private static final long serialVersionUID = 1L;
/**
* 随机码名称,存储到会话中用作校验
*/
private static String name;
/**
* 图片宽度
*/
private int width;
/**
* 图片高度
*/
private int height;
/**
* 随即码长度-位数
*/
private int length;
/**
* 干扰线条数
*/
private int interlinenum;
/**
* 最大字体
*/
private int maxfontsize;
/**
* 最小字体
*/
private int minfontsize;
/**
* 随即码字符串
*/
private String randomCode;
/**
* 初始化
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
name = config.getInitParameter("name");
width = Integer.parseInt(config.getInitParameter("width"));
height = Integer.parseInt(config.getInitParameter("height"));
length = Integer.parseInt(config.getInitParameter("length"));
maxfontsize = Integer.parseInt(config.getInitParameter("maxfontsize"));
minfontsize = Integer.parseInt(config.getInitParameter("minfontsize"));
interlinenum = Integer.parseInt(config.getInitParameter("interlinenum"));
}
/**
* get请求
*/
public void doGet(HttpServletRequest req, HttpServletResponse res) {
//set necessary response infomation
res.setContentType("image/jpeg");
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "No-cache");
res.setDateHeader("Expires", 0L);
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics graph = image.getGraphics();
//set background color and fill the rectangle in specified color
graph.setColor(ImageUtil.getRandomColor(200,55));
graph.fillRect(0, 0, width, height);
//set interrupt line's color and draw random line in specifed color
graph.setColor(ImageUtil.getRandomColor(100,27));
for (int i = 0; i < interlinenum; i++) {
ImageUtil.drawRandomLine(graph, width, height);
}
//set the random code color and draw random code
graph.setColor(ImageUtil.getRandomColor(60,3));
drawRandomCode(graph,width,height,length);
//set session value
HttpSession session = req.getSession();
session.setAttribute(name, this.randomCode);
//release the resource of the image
graph.dispose();
//output the image to the page
try {
ImageIO.write(image, "JPEG", res.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* post请求
*/
public void doPost(HttpServletRequest req, HttpServletResponse res){
doGet(req,res);
}
/**
* 绘制随即码-显示的数字是随即的、字体是随机的、数字显示的位置(纵坐标是随即的)
* @param graph graphics context
* @param width width of the range of the graphics context
* @param height height of the range of the graphics context
* @param length length of the random code
*/
private void drawRandomCode(Graphics graph,int width,int height,int length){
StringBuffer sb = new StringBuffer();
String eachCode = null;
int fontsize = minfontsize;
int y = 0;
Random r = new Random(new Date().getTime());
for(int index=0;index<length;index++){
//random code
eachCode = String.valueOf(r.nextInt(10));
//constrct complete random code
sb.append(eachCode);
//random fontsize
fontsize = minfontsize + r.nextInt(maxfontsize-minfontsize);
//random y axes
y = height/2 + r.nextInt(height/2);
//set font and draw random code
graph.setFont(new Font("Times New Roman", Font.BOLD, fontsize));
graph.drawString(eachCode, (int)(index*((double)width/(double)length)), y);
}
this.randomCode = sb.toString();
}
/**
* @return the name
*/
public static String getName() {
return name;
}
}
package com.hib.img;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Date;
import java.util.Random;
public class ImageUtil {
public static final int MAX_COLOR_VALUE = 255;
private static Random r = new Random(new Date().getTime());
/**
* 获取随即色
*
* @return Color 随即色
*/
public static Color getRandomColor(int base,int range) {
if(range > MAX_COLOR_VALUE){
range = MAX_COLOR_VALUE;
}
int red = base + r.nextInt(range);
int green = base + r.nextInt(range);
int blue = base + r.nextInt(range);
return new Color(red, green, blue);
}
/**
* 绘制随即线
*
* @param graph graphics context
* @param width width of the range of the graphics context
* @param height height of the range of the graphics context
*/
public static void drawRandomLine(Graphics graph, int width, int height) {
int sx = r.nextInt(width);
int ex = r.nextInt(width);
int sy = r.nextInt(height);
int ey = r.nextInt(height);
graph.drawLine(sx, sy, ex, ey);
}
}
And I think how to extract the content of the image is not concerned with how to create the image.But then,if you consider that it is not the case,you can refer above codes.