WEB驗證碼類

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* 生成的隨機數字或字母,用於程序登陸時的驗證碼.
* @author wang
*
*/
public class RandomGraphic {
//字符的高度和寬度
private int wordHeight=10;
private int wordWidth=15;
//字符大小
private int fontSize=16;
//最大字符串個數
private static final int MAX_CHARCOUNT=16;
//垂直方向起始位置
private final int initypos=5;
//要生成的字符個數,由工廠方法得到
private int charCount=0;
//顏色數組,繪制字符串時隨機選擇一個
private static final Color[] CHAR_COLOR={Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA};
//隨機數生成器
private Random r=new Random();
//生成的圖象格式為JPG,輸出到頁面時需要設置MIME type為image/jpeg
public static String GRAPHIC_JPEG="JPEG";
//生成的圖象格式為PNG,輸出到頁面時需要設置MIME type為image/png
public static String GRAPHIC_PNG="PNG";
protected RandomGraphic(int charCount){
this.charCount=charCount;
}
/**
* 工廠方法設置驗證碼的個數.
* @param charCount
* @return
* @throws Exception
*/
public static RandomGraphic creatInstance(int charCount) throws Exception{
if(charCount<1||charCount>MAX_CHARCOUNT){
throw new Exception("Invalid paramter charCount should between in 1 and 16");
}
return new RandomGraphic(charCount);
}
/**
* 隨機生成數字驗證碼.
* @param graphicFormat 圖片格式jpeg ,png
* @param out
* @return
* @throws IOException
*/
public String drawNumber(String graphicFormat,OutputStream out) throws IOException{
String charValue="";
charValue=randNumber();
return draw(charValue,graphicFormat,out);
}
/**
* 隨機生成字母驗證碼.
* @param graphicFormat
* @param out
* @return
* @throws IOException
*/
public String drawAlpha(String graphicFormat,OutputStream out) throws IOException{
String charValue="";
charValue=randAlpha();
return draw(charValue,graphicFormat,out);
}
/*
* 生成隨機數字字符串.
*/
protected String randNumber(){
String charValue="";
for(int i=0;i<charCount;i++){
charValue+=String.valueOf(randomInt(0,10));
}
return charValue;
}
/**
* 返回一個from,to之間的一個隨機整數.
* @param from
* @param to
* @return
*/
protected int randomInt(int from,int to){
return from+r.nextInt(to-from);
}
/**
* 繪制圖象.
* @param charValue
* @param graphicFormat
* @param out
* @return
* @throws IOException
*/
protected String draw(String charValue,String graphicFormat,OutputStream out) throws IOException{
//計算圖象的寬和高
int w=(charCount+2)*wordWidth;
int h=wordHeight*3;
//創建內存圖象區
BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g=bi.createGraphics();
//設置背景色
Color backColor=Color.WHITE;
g.setBackground(backColor);
g.fillRect(0, 0, w, h);
//隨機產生155條干擾線
for(int i=0;i<50;i++){
g.setColor(CHAR_COLOR[randomInt(0,CHAR_COLOR.length)]);
int x=r.nextInt(w);
int y=r.nextInt(h);
int xl=r.nextInt(10);
int yl=r.nextInt(10);
g.drawLine(x,y,x+xl,y+yl);
}
//設置font
g.setFont(new Font(null,Font.BOLD,fontSize));
//繪制cahrValue每個字符隨機顏色
for(int i=0;i<charCount;i++){
String c=charValue.substring(i, i+1);
Color color=CHAR_COLOR[randomInt(0,CHAR_COLOR.length)];
g.setColor(Color.BLACK);
int xpos=(i+1)*wordWidth;
//垂直方向隨機
int ypos=randomInt(initypos+wordHeight,initypos+wordHeight*2);
g.drawString(c, xpos, ypos);
}
//圖象生效
g.dispose();
bi.flush();
ImageIO.write(bi,graphicFormat,out);
return charValue;
}
/**
* 生成隨機字母串.
* @return
*/
private String randAlpha(){
String charValue="";
//生成隨機字母串
for(int i=0;i<charCount;i++){
char c=(char)(randomInt(0,26)+'a');
charValue+=String.valueOf(c);
}
return charValue;
}
調用
privaet int charCount=4;//驗證碼的個數
response.setContentType("image/png");
try {
HttpSession session=request.getSession();
String validateCode=RandomGraphic.creatInstance(charCount).drawNumber(RandomGraphic.GRAPHIC_PNG, response.getOutputStream());
session.setAttribute("validateCode", validateCode);
//System.out.println(session.getAttribute("validateCode").toString());
//response.flushBuffer();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



浙公网安备 33010602011771号