实现1
package com.geosoft.util.validate;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ValidationCode extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -4572484854814017263L;
private Random random = new Random();
/**
* 获得随机颜色。
* @param fc
* @param bc
* @return
*/
private Color getRandomColor(int fc, int bc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
/**
* 获得随机大写字母。
* @param length 长度
* @return 字符数组。
*/
private char[] getRandomUpperedLetter(int length) {
char[] cs = new char[length];
for (int i = 0; i < cs.length; i++) {
cs[i] = (char) (65 + random.nextInt(26));
}
return cs;
}
char getRandomChar() {
while (true) {
int i = 48 + (int) ((123 - 48) * Math.random());
if (i <= 57 || (i >= 65 && i <= 90) || (i >= 97 && i <= 122)) {
return (char) i;
// Character.isDigit()
}
}
}
/**
* 创建验证码图片
* @param width 宽度
* @param height 高度
* @param cs 大写字母的字符数组。
* @return 验证码图片。
*/
private BufferedImage createImage(int width, int height, char[] cs) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandomColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandomColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
int start = 6;
int offset;
for (int i = 0; i < cs.length; i++) {
g.setColor(getRandomColor(20, 130));
if (Character.isUpperCase(cs[i])) {
if (cs[i] == 'W' || cs[i] == 'M')
offset = 19;
else
offset = 15;
} else {
if (cs[i] == 'w' || cs[i] == 'm')
offset = 15;
else
offset = 12;
}
g.drawString(cs[i] + "", start, 16);
start += offset;
}
g.dispose();
return image;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 验证码长度
int length = 4;
String len = request.getParameter("len");
if (len == null || len.trim().equals("")) {
length = 4;
} else {
try {
length = Integer.parseInt(len);
if (length < 1)
length = 4;
} catch (Exception e) {
}
}
char[] cs = getRandomUpperedLetter(length);//getRandomChars(length);
BufferedImage image = createImage(70, 20, cs);
String sRand = new String(cs);
request.getSession().setAttribute("randcode", sRand);
ImageIO.write(image, "JPEG", response.getOutputStream());
}
}