做web开发,很多时间需要大量生成难证码,写一个验证码是一件很容易的事,但是写得好不会是一件易事。
以下是我写的一个验证码生成方法,经改进行,性能比初时(我第一次写时)被时提升了40%以上,仅供参考。
Java代码
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 该方法主要是生成验证码,并返4位回验证码
*
* @author Jacky *
*/
public final class ImageCode {
/**
* 经常用到的作静态常量处理,这样会加快处理速度,减少变量第次都要生成的内存空间浪费,用final使程序不用录找该类是否被继承,减少加载时间
*
*/
private static final Random random = new Random();
// 预定义定义四种字体,并将随机得到
private static final Font[] CODEFONT = { new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25),
new Font("宋体", Font.BOLD, 25) };
// 预定义定义字体颜色,并将随机得到色
private static final Color[] FONTCOLOR = { Color.ORANGE, Color.RED, Color.PINK, Color.BLUE, Color.GREEN };
// 预定义 图片的背景颜色,并将随机得到
private static final Color[] BGCOLOR = { Color.BLACK, Color.WHITE };
// 预定义干扰线的颜色
private static final Color LINECOLOR = new Color(242, 234, 22);
// 预定义生随机的字符,用数组取值比String类型取一个值快
private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
// 返回的验证码
private static StringBuffer CONDENUMBER = null;
// 预定义图片大小
private static final int WIDTH = 100, HEIGHT = 25;
/**
* 生成验证码输出,并输出
*
* @param request
* @param response
* @return codeNumber
*/
public final String image(HttpServletRequest request, HttpServletResponse response) {
CONDENUMBER = new StringBuffer();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(BGCOLOR[random.nextInt(2)]);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < 4; i++) {
drawCode(g, i);
}
drawNoise(g, 8);
g.setColor(Color.gray);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
g.dispose();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
ImageIO.write(image, "png", sos);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return CONDENUMBER.toString();
}
/**
* 画验证码
*
* @param request
* @param response
* @return void
*/
private final void drawCode(Graphics graphics, int i) {
String number = CODE[random.nextInt(36)];
graphics.setFont(CODEFONT[random.nextInt(4)]);
graphics.setColor(FONTCOLOR[random.nextInt(5)]);
graphics.drawString(number, 10 + i * 20, 20);
CONDENUMBER.append(number);
}
/**
* 画干扰线
*
* @param graphics
* @param void
*/
private final void drawNoise(Graphics graphics, int lineNumber) {
graphics.setColor(LINECOLOR);
int pointX1, pointY1, pointX2, pointY2;
for (int i = 0; i < lineNumber; i++) {
pointX1 = 1 + (int) (Math.random() * WIDTH);
pointY1 = 1 + (int) (Math.random() * HEIGHT);
pointX2 = 1 + (int) (Math.random() * WIDTH);
pointY2 = 1 + (int) (Math.random() * HEIGHT);
graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
}
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 该方法主要是生成验证码,并返4位回验证码
*
* @author Jacky *
*/
public final class ImageCode {
/**
* 经常用到的作静态常量处理,这样会加快处理速度,减少变量第次都要生成的内存空间浪费,用final使程序不用录找该类是否被继承,减少加载时间
*
*/
private static final Random random = new Random();
// 预定义定义四种字体,并将随机得到
private static final Font[] CODEFONT = { new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25),
new Font("宋体", Font.BOLD, 25) };
// 预定义定义字体颜色,并将随机得到色
private static final Color[] FONTCOLOR = { Color.ORANGE, Color.RED, Color.PINK, Color.BLUE, Color.GREEN };
// 预定义 图片的背景颜色,并将随机得到
private static final Color[] BGCOLOR = { Color.BLACK, Color.WHITE };
// 预定义干扰线的颜色
private static final Color LINECOLOR = new Color(242, 234, 22);
// 预定义生随机的字符,用数组取值比String类型取一个值快
private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
// 返回的验证码
private static StringBuffer CONDENUMBER = null;
// 预定义图片大小
private static final int WIDTH = 100, HEIGHT = 25;
/**
* 生成验证码输出,并输出
*
* @param request
* @param response
* @return codeNumber
*/
public final String image(HttpServletRequest request, HttpServletResponse response) {
CONDENUMBER = new StringBuffer();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(BGCOLOR[random.nextInt(2)]);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < 4; i++) {
drawCode(g, i);
}
drawNoise(g, 8);
g.setColor(Color.gray);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
g.dispose();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
ImageIO.write(image, "png", sos);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return CONDENUMBER.toString();
}
/**
* 画验证码
*
* @param request
* @param response
* @return void
*/
private final void drawCode(Graphics graphics, int i) {
String number = CODE[random.nextInt(36)];
graphics.setFont(CODEFONT[random.nextInt(4)]);
graphics.setColor(FONTCOLOR[random.nextInt(5)]);
graphics.drawString(number, 10 + i * 20, 20);
CONDENUMBER.append(number);
}
/**
* 画干扰线
*
* @param graphics
* @param void
*/
private final void drawNoise(Graphics graphics, int lineNumber) {
graphics.setColor(LINECOLOR);
int pointX1, pointY1, pointX2, pointY2;
for (int i = 0; i < lineNumber; i++) {
pointX1 = 1 + (int) (Math.random() * WIDTH);
pointY1 = 1 + (int) (Math.random() * HEIGHT);
pointX2 = 1 + (int) (Math.random() * WIDTH);
pointY2 = 1 + (int) (Math.random() * HEIGHT);
graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
}
}
}
一个调用的例子:
Java代码
public void image(HttpServletRequest request, HttpServletResponse response) {
ImageCode ic = new ImageCode();
String imageCode = ic.image(request, response);
request.getSession().removeAttribute("imageCode");
//转为小写,从页面输入也转为小写,以达到不分大小写作用
request.getSession().setAttribute("imageCode", imageCode.toLowerCase());
}
public void image(HttpServletRequest request, HttpServletResponse response) {
ImageCode ic = new ImageCode();
String imageCode = ic.image(request, response);
request.getSession().removeAttribute("imageCode");
//转为小写,从页面输入也转为小写,以达到不分大小写作用
request.getSession().setAttribute("imageCode", imageCode.toLowerCase());
}
以下是我写的一个验证码生成方法,经改进行,性能比初时(我第一次写时)被时提升了40%以上,仅供参考。
Java代码
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 该方法主要是生成验证码,并返4位回验证码
*
* @author Jacky *
*/
public final class ImageCode {
/**
* 经常用到的作静态常量处理,这样会加快处理速度,减少变量第次都要生成的内存空间浪费,用final使程序不用录找该类是否被继承,减少加载时间
*
*/
private static final Random random = new Random();
// 预定义定义四种字体,并将随机得到
private static final Font[] CODEFONT = { new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25),
new Font("宋体", Font.BOLD, 25) };
// 预定义定义字体颜色,并将随机得到色
private static final Color[] FONTCOLOR = { Color.ORANGE, Color.RED, Color.PINK, Color.BLUE, Color.GREEN };
// 预定义 图片的背景颜色,并将随机得到
private static final Color[] BGCOLOR = { Color.BLACK, Color.WHITE };
// 预定义干扰线的颜色
private static final Color LINECOLOR = new Color(242, 234, 22);
// 预定义生随机的字符,用数组取值比String类型取一个值快
private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
// 返回的验证码
private static StringBuffer CONDENUMBER = null;
// 预定义图片大小
private static final int WIDTH = 100, HEIGHT = 25;
/**
* 生成验证码输出,并输出
*
* @param request
* @param response
* @return codeNumber
*/
public final String image(HttpServletRequest request, HttpServletResponse response) {
CONDENUMBER = new StringBuffer();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(BGCOLOR[random.nextInt(2)]);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < 4; i++) {
drawCode(g, i);
}
drawNoise(g, 8);
g.setColor(Color.gray);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
g.dispose();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
ImageIO.write(image, "png", sos);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return CONDENUMBER.toString();
}
/**
* 画验证码
*
* @param request
* @param response
* @return void
*/
private final void drawCode(Graphics graphics, int i) {
String number = CODE[random.nextInt(36)];
graphics.setFont(CODEFONT[random.nextInt(4)]);
graphics.setColor(FONTCOLOR[random.nextInt(5)]);
graphics.drawString(number, 10 + i * 20, 20);
CONDENUMBER.append(number);
}
/**
* 画干扰线
*
* @param graphics
* @param void
*/
private final void drawNoise(Graphics graphics, int lineNumber) {
graphics.setColor(LINECOLOR);
int pointX1, pointY1, pointX2, pointY2;
for (int i = 0; i < lineNumber; i++) {
pointX1 = 1 + (int) (Math.random() * WIDTH);
pointY1 = 1 + (int) (Math.random() * HEIGHT);
pointX2 = 1 + (int) (Math.random() * WIDTH);
pointY2 = 1 + (int) (Math.random() * HEIGHT);
graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
}
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 该方法主要是生成验证码,并返4位回验证码
*
* @author Jacky *
*/
public final class ImageCode {
/**
* 经常用到的作静态常量处理,这样会加快处理速度,减少变量第次都要生成的内存空间浪费,用final使程序不用录找该类是否被继承,减少加载时间
*
*/
private static final Random random = new Random();
// 预定义定义四种字体,并将随机得到
private static final Font[] CODEFONT = { new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25),
new Font("宋体", Font.BOLD, 25) };
// 预定义定义字体颜色,并将随机得到色
private static final Color[] FONTCOLOR = { Color.ORANGE, Color.RED, Color.PINK, Color.BLUE, Color.GREEN };
// 预定义 图片的背景颜色,并将随机得到
private static final Color[] BGCOLOR = { Color.BLACK, Color.WHITE };
// 预定义干扰线的颜色
private static final Color LINECOLOR = new Color(242, 234, 22);
// 预定义生随机的字符,用数组取值比String类型取一个值快
private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
// 返回的验证码
private static StringBuffer CONDENUMBER = null;
// 预定义图片大小
private static final int WIDTH = 100, HEIGHT = 25;
/**
* 生成验证码输出,并输出
*
* @param request
* @param response
* @return codeNumber
*/
public final String image(HttpServletRequest request, HttpServletResponse response) {
CONDENUMBER = new StringBuffer();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(BGCOLOR[random.nextInt(2)]);
g.fillRect(0, 0, WIDTH, HEIGHT);
for (int i = 0; i < 4; i++) {
drawCode(g, i);
}
drawNoise(g, 8);
g.setColor(Color.gray);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
g.dispose();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
ServletOutputStream sos = null;
try {
sos = response.getOutputStream();
ImageIO.write(image, "png", sos);
sos.flush();
sos.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return CONDENUMBER.toString();
}
/**
* 画验证码
*
* @param request
* @param response
* @return void
*/
private final void drawCode(Graphics graphics, int i) {
String number = CODE[random.nextInt(36)];
graphics.setFont(CODEFONT[random.nextInt(4)]);
graphics.setColor(FONTCOLOR[random.nextInt(5)]);
graphics.drawString(number, 10 + i * 20, 20);
CONDENUMBER.append(number);
}
/**
* 画干扰线
*
* @param graphics
* @param void
*/
private final void drawNoise(Graphics graphics, int lineNumber) {
graphics.setColor(LINECOLOR);
int pointX1, pointY1, pointX2, pointY2;
for (int i = 0; i < lineNumber; i++) {
pointX1 = 1 + (int) (Math.random() * WIDTH);
pointY1 = 1 + (int) (Math.random() * HEIGHT);
pointX2 = 1 + (int) (Math.random() * WIDTH);
pointY2 = 1 + (int) (Math.random() * HEIGHT);
graphics.drawLine(pointX1, pointY1, pointX2, pointY2);
}
}
}
一个调用的例子:
Java代码
public void image(HttpServletRequest request, HttpServletResponse response) {
ImageCode ic = new ImageCode();
String imageCode = ic.image(request, response);
request.getSession().removeAttribute("imageCode");
//转为小写,从页面输入也转为小写,以达到不分大小写作用
request.getSession().setAttribute("imageCode", imageCode.toLowerCase());
}
public void image(HttpServletRequest request, HttpServletResponse response) {
ImageCode ic = new ImageCode();
String imageCode = ic.image(request, response);
request.getSession().removeAttribute("imageCode");
//转为小写,从页面输入也转为小写,以达到不分大小写作用
request.getSession().setAttribute("imageCode", imageCode.toLowerCase());
}