Home > Java > javaTutorial > Java generates Chinese character random verification code

Java generates Chinese character random verification code

巴扎黑
Release: 2016-12-02 09:43:13
Original
3936 people have browsed it

The implementation mainly includes two classes, one is to generate the verification code, and the other is to determine whether the verification code input is correct. The implementation principle is relatively simple. Generate pictures of Chinese characters and interference lines and save the Chinese characters to the session. The front desk obtains the verification code picture generated each time. For display, during verification, the value entered in the text box is obtained and sent to the background for comparison with the session value. The function is as simple as that. 1. Class for generating Chinese character verification code
package com.veryCode;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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; javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Generate random Chinese character verification code
* @author feizi
* @Time 2015-1-30 3:16:55 PM
*/
public class DrawVeryCode extends HttpServlet {
private static final long serialVersionUID = -3002087466925846353L;
public static final int WIDTH = 120;
public static final int HEIGHT = 30;
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletRes ponder response)
throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//Create image cache
BufferedImage bi = new BufferedImage (WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
//Get the canvas
Graphics g = bi.getGraphics();
//Set the background color
setBackGroundColor(g);
//Set the border
setBorder(g);
//Draw the interference line
drawRandomLine(g);
//Generate a random Chinese character verification code
String randomChar = drawRandomChar((Graphics2D)g);//Forced conversion
//Save the generated random Chinese character verification code into the session. Save
request.getSession().setAttribute("veryCode", randomChar);
//Set the type of response information to the image type
response.setContentType("image/jpeg");
//Control Http cache and set response headers Information to prevent browser caching
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-Cache");
response.setHeader("Pargma", "no-Cache" ");
//Write the verification code image into the browser
ImageIO.write(bi, "jpg", response.getOutputStream());
}
/**
* Set background color
* @param g
*/
private void setBackGroundColor(Graphics g){
//Set the background color
g.setColor(Color.WHITE);
//Fill the area
g.fillRect(0, 0, WIDTH, HEIGHT);
}
/**
* Set border
* @param g
*/
private void setBorder(Graphics g){
//Set the border color
g.setColor(Color.BLUE);
//Set the border area
g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
}
/**
* Draw interference lines
* @param g
*/
private void drawRandomLine(Graphics g){
//Set the color of the interference line
g.setColor(Color.GREEN);
//Set the number of lines and start drawing the line
for ( int i = 0; i < 40; i++) {
int x1 = new Random().nextInt(WIDTH);
int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random(). nextInt(WIDTH);
int y2 = new Random().nextInt(HEIGHT);
//Start drawing the line
g.drawLine(x1, y1, x2, y2);
}
}
/**
* Draw random Chinese characters
* @param g
*/
private String drawRandomChar(Graphics2D g){
StringBuffer sb = new StringBuffer();
//Set color
g.setColor(Color.RED);
//Set font
g.setFont(new Font("宋体", Font.BOLD, 20));
//Prepare commonly used Chinese character set
String base = "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9" +
"u4e0au7740u4e2au5730u523 0u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548c" +
"u90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628a" +
"u597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837" +
"u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u905 3u5b83" +
"u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56de" + 
"u4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c 11u5019u7ecfu53d1" + 
"u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624b" + 
"u77e5u7406u773cu5fd7u70b9u5fc3 u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403" + 
"u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240" + 
"u654cu4e4bu 6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2d" + 
"u6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199 " + 
"u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522b" + 
"u98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u823 9u671bu65b0u5e26u961f" + 
"u5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7" + 
"u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cd" + 
"u4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu80 05u5e72u77f3" + 
"u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acb" + 
"u6cb3u6751u516bu96beu65e9u8bbau5417u68 39u5171u8ba9u76f8u7814u4ecau5176" + 
"u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627e" + 
"u4e89u9886u 6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27" + 
"u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76 u4f4d" + 
"u5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138" + 
"u7247u79d1u5012u775bu5229u4e16u521au4e14 u7531u9001u5207u661fu5bfcu665a" + 
"u8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523b" + 
"u5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754c" + 
"u62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u 8f6cu753bu9020" + 
"u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dc" + 
"u722cu7761u5174u5f62u91cfu54b1u8 9c2u82e6u4f53u4f17u901au51b2u5408u7834" + 
"u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebf" + 
"u91ceu575au 7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1f" + 
"u80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97 f3" + 
"u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975e" + 
"u4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79c du88c5u9876u6025u6797u505c" + 
"u606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6"; 
int x = 5; 
//控制字数,最多4个字 
for (int i = 0; i < 4; i++) { 
//设置字体旋转角度 
int degree = new Random().nextInt() % 30; 
//截取汉字 
String ch = base.charAt(new Random().nextInt(base.length())) + ""; 
//追加到sb中 
sb.append(ch); 
//顺时针旋转 
g.rotate(degree * Math.PI / 180, x, 20); 
//开始画汉字 
g.drawString(ch, x, 20); 
//逆时针旋转 
g.rotate(-(degree * Math.PI / 180), x, 20); 
x += 30; 

return sb.toString(); 

public static void main(String[] args) { 
String s = "1234565236985"; 
System.out.println(s.charAt(5) + ""); 
System.out.println(new Random().nextInt()); 


复制代码 
二、验证前台页面上输入的验证码信息 
package com.veryCode; 
import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
/**
* Verification code information entered on the verification page
* @author feizi
* @time 2015-2-2 8:14:22 AM
*/ 
public class ResultServlet extends HttpServlet { 
private static final long serialVersionUID = -4023881398470853660L; 
public void doGet(HttpServletRequest request,HttpServletResponse response) 
throws ServletException,IOException{ 
doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset= utf-8");
//The correct verification code value generated
String validateC = (String) request.getSession().getAttribute("veryCode");
//The verification code value entered by the user in the browser
String veryCode = request.getParameter("code");
PrintWriter out = response.getWriter();
if(null == veryCode || "".equals(veryCode)){
out.println("Please enter the verification code. ..");
}else{
if(validateC.equals(veryCode)){
out.print(1);
}else{
out.println("Sorry, the verification code was entered incorrectly...");
}
}
out.flush();
out.close();
}
}

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template