java生成圖片驗證碼實例代碼

java生成圖片驗證碼實例代碼



生成圖片驗證碼類



package spring_mvc_test.utils;



import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;


import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




/**
 * 圖片驗證碼生成類
 *
 */
public class ImgVerifyCodeUtils extends HttpServlet{


/**

*/
private static final long serialVersionUID = 1L;


/**
*  驗證碼圖片的寬度
*/
private int width = 70;

/**
* 驗證碼圖片的高度
*/
private int height = 30;

/**
* 驗證碼字符個數
*/
private int codeCount = 5;

/**
* 生成隨機數的水平距離
*/
private int xx = 0;

/**
* 生成隨機數的數字高度
*/
private int fontHeight;

/**
* 生成隨機數的垂直距離
*/
private int codeY;

/**
* 驗證碼序列
*/
private String[] codeSequence = {"1" , "2" , "3" , "4" , "5" ,"6","7","8","9","A","a","B","b","c","C"
      ,"D","d","E","e","F","f","G","g","z","X","Q","v"};

public ImgVerifyCodeUtils(int width, int height, int codeCount, int xx, int fontHeight, int codeY,
String[] codeSequence) {
super();
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.xx = xx;
this.fontHeight = fontHeight;
this.codeY = codeY;
this.codeSequence = codeSequence;
}


public ImgVerifyCodeUtils() {
super();
}


public int getWidth() {
return width;
}


public void setWidth(int width) {
this.width = width;
}


public int getHeight() {
return height;
}


public void setHeight(int height) {
this.height = height;
}


public int getCodeCount() {
return codeCount;
}


public void setCodeCount(int codeCount) {
this.codeCount = codeCount;
}


public int getXx() {
return xx;
}


public void setXx(int xx) {
this.xx = xx;
}


public int getFontHeight() {
return fontHeight;
}


public void setFontHeight(int fontHeight) {
this.fontHeight = fontHeight;
}


public int getCodeY() {
return codeY;
}


public void setCodeY(int codeY) {
this.codeY = codeY;
}


public String[] getCodeSequence() {
return codeSequence;
}


public void setCodeSequence(String[] codeSequence) {
this.codeSequence = codeSequence;
}


public static long getSerialversionuid() {
return serialVersionUID;
}


/**
* 初始化驗證圖片屬性
*/
public void init() throws ServletException {
//高度
String strWidth = width + "";
//寬度
String strHeight = height + "";
//字符個數
String strCodeCount = codeCount + "";

//此處這麼寫,代表可以隨時更改告訴,在程序運行時改變
try{
if (strWidth != null && strWidth.length() != 0) {
       width = Integer.parseInt(strWidth);
     }
     if (strHeight != null && strHeight.length() != 0) {
       height = Integer.parseInt(strHeight);
     }
     if (strCodeCount != null && strCodeCount.length() != 0) {
       codeCount = Integer.parseInt(strCodeCount);
     }
}catch(Exception e){
e.printStackTrace();
}
//生成隨機數的水平距離
xx = width/(codeCount + 2);
//生成隨機數的數字高度
fontHeight = height - 12;
//生成隨機數的垂直距離
codeY = height - 8;

}

public String createImageCode(HttpServletRequest req , HttpServletResponse resp) throws Exception{
init();
//自定義圖像buffer
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D gd = buffImg.createGraphics();

Random random = new Random();
//將圖片填充爲白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);
//創建字體,字體的大小根據圖片的高度來定
Font font = new Font("Fixedsys",Font.PLAIN,fontHeight);
//設置字體
gd.setFont(font);
//畫邊框
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);
//隨機產生4條幹擾線,使圖像中的認證碼不易被其他程序探測到。
gd.setColor(Color.BLACK);
for(int i = 0 ; i < 4; 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用於保存隨機產生的驗證碼,以便用戶登錄後進行驗證。
StringBuffer randomCode = new StringBuffer();
int red = 0;
int green = 0;
int blue = 0;

//隨機產生codeCount數字的驗證碼。
for(int i = 0 ; i < codeCount ; i ++){
//得到隨機產生的驗證碼數字。
String strRand = String.valueOf(codeSequence[random.nextInt(27)]);
//產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
red = random.nextInt(125);
green = random.nextInt(255);
blue = random.nextInt(200);

//用隨機產生的顏色將驗證碼繪製到圖像中。
gd.setColor(new Color(red,green,blue));
gd.drawString(strRand, (i + 1) * xx, codeY);

//將產生的四個隨機數組合在一起
randomCode.append(strRand);
}
//將四位數字的驗證碼保存到Session中。
HttpSession session = req.getSession();
session.setAttribute("validataCode", randomCode.toString());

//禁止圖像緩存
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);

resp.setContentType("image/jpeg");

//將圖像輸出到Servlet輸出流中
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();

return null;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章