java生成圖片驗證碼實例代碼。本站提示廣大學習愛好者:(java生成圖片驗證碼實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java生成圖片驗證碼實例代碼正文
關於java圖片驗證碼的文章比來更新了很多,贊助年夜家控制java驗證碼的生成技巧,下文為年夜家分享了java生成圖片驗證碼最簡略的辦法,供年夜家參考。
如今各行業在定制體系時都邑斟酌到機械注冊,如今最有用的方法就是輸出驗證。如今的驗證方法有許多種:
1、成績驗證,其實也是圖片驗證,在圖片上生成成績,然後輸出框輸出謎底。
2、圖片驗證,輸出圖片上展現的文字信息。
3、短信驗證,比擬復雜,用戶也不怎樣愛好。
4、還有就是百度最新的驗證方法。圖片上生成文字,然後湧現一個文字點擊框,選擇你在驗證圖片上看到的文字。
如今就分享一下java生成驗證碼的代碼,這是一個基本的代碼。可以直接在進修中應用,假如須要較為龐雜的驗證可本身添加邏輯驗證。
@Controller
public class ImgVerifyCode extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 驗證碼圖片的寬度。
*/
private int width = 70;
/**
* 驗證碼圖片的高度。
*/
private int height =30;
/**
* 驗證碼字符個數
*/
private int codeCount = 5;
/**
* xx
*/
private int xx = 0;
/**
* 字體高度
*/
private int fontHeight;
/**
* codeY
*/
private int codeY;
/**
* codeSequence
*/
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 void init() throws ServletException {
// 從web.xml中獲得初始信息
// 寬度
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 (NumberFormatException e) {
e.printStackTrace();
}
xx = width / (codeCount + 2); //生成隨機數的程度間隔
fontHeight = height - 12; //生成隨機數的數字高度
codeY = height - 8; //生成隨機數的垂直間隔
}
protected String images(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException {
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, green = 0, 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("validateCode", 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;
}
}
這個代碼就是生成驗證圖片的基本辦法。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,年夜家也能夠檢查之前的文章停止深刻進修。