1全封、生成驗(yàn)證碼
public class CodeUtil {
private static int width = 100;// 定義圖片的width
private static int height = 40;// 定義圖片的height
private static int codeCount = 4;// 定義圖片上顯示驗(yàn)證碼的個(gè)數(shù)
private static int xx = 18;
private static int fontHeight = 20;
private static int codeY = 27;
private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R','T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '6', '7', '8', '9' };
/**
* 生成一個(gè)map集合
* code為生成的驗(yàn)證碼
* codePic為生成的驗(yàn)證碼BufferedImage對(duì)象
* @return
*/
public static Map<String,Object> generateCodeAndPic() {
// 定義圖像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Graphics2D gd = buffImg.createGraphics();
// Graphics2D gd = (Graphics2D) buffImg.getGraphics();
Graphics gd = buffImg.getGraphics();
// 創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類
Random random = new Random();
// 將圖像填充為白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
// 創(chuàng)建字體端壳,字體的大小應(yīng)該根據(jù)圖片的高度來定川梅。
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
// 設(shè)置字體召衔。
gd.setFont(font);
// 畫邊框骑疆。
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
// 隨機(jī)產(chǎn)生40條干擾線鲫凶,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到寸爆。
gd.setColor(Color.BLACK);
for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}
// randomCode用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼礁鲁,以便用戶登錄后進(jìn)行驗(yàn)證。
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
// 隨機(jī)產(chǎn)生codeCount數(shù)字的驗(yàn)證碼而昨。
for (int i = 0; i < codeCount; i++) {
// 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字救氯。
String code = String.valueOf(codeSequence[random.nextInt(30)]);
// 產(chǎn)生隨機(jī)的顏色分量來構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同歌憨。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
// 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中着憨。
gd.setColor(new Color(red, green, blue));
gd.drawString(code, (i + 1) * xx, codeY);
// 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。
randomCode.append(code);
}
Map<String,Object> map =new HashMap<String,Object>();
//存放驗(yàn)證碼
map.put("code", randomCode);
//存放生成的驗(yàn)證碼BufferedImage對(duì)象
map.put("codePic", buffImg);
return map;
}
}
2务嫡、輸出驗(yàn)證碼
@RequestMapping("/code")
@ResponseBody
public void doGet(HttpServletRequest req, HttpServletResponse resp){
// 調(diào)用工具類生成的驗(yàn)證碼和驗(yàn)證碼圖片
Map<String, Object> codeMap = CodeUtil.generateCodeAndPic();
// 將四位數(shù)字的驗(yàn)證碼保存到Session中甲抖。
HttpSession session = req.getSession();
session.setAttribute("code", codeMap.get("code").toString());
// 禁止圖像緩存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", -1);
resp.setContentType("image/jpeg");
// 將圖像輸出到Servlet輸出流中心铃。
ServletOutputStream sos;
try {
sos = resp.getOutputStream();
ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}