Register the image verification code and java image verification code in java
Package org. servlet. demo;
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. nio. charset. Charset;
Import java. util. Random;
Import javax. imageio. ImageIO;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class DrawImageServlet extends HttpServlet {
/**
* On the user logon page, a random digit verification is required to prevent malicious attacks.
*
*/
// Define the image width and height
Public static finals int WIDTH = 200;
Public static final int HEIGHT = 50;
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
// Create an image BufferedImage. TYPE_INT_RGB to set the image type
BufferedImage image = new BufferedImage (WIDTH, HEIGHT,
BufferedImage. TYPE_INT_RGB );
// Get the image's paint brush
Graphics g = image. getGraphics ();
// Set the background color of the image
SetBackGround (g );
// Set the border of the image
SetBorder (g );
// Sketch the interference lines on the Image
DrawRandomLine (g );
// Write a random number to the image
WriteRandomNum (g );
// Tell the browser the write type
Resp. setContentType ("image/jpeg ");
// Write the image to the browser
ImageIO. write (image, "jpg", resp. getOutputStream ());
// Do not cache the browser with header Control
Resp. setDateHeader ("expries",-1 );
Resp. setHeader ("Cache-Control", "no-cache ");
Resp. setHeader ("Pragma", "no-cache ");
}
Private void writeRandomNum (Graphics graphics ){
/** [\ U4e00-\ u9fa5] is the interval Regular Expression of Chinese Characters
\ U indicates that an uncode table exists.
It means that all Chinese characters are in the \ U (uncode) code table between 4e00 and 9fa5 */
String base =
"\ U7684 \ u4e00 \ u4e86 \ u662f \ u6211 \ Users \ u5728 \ Users \ u6765 \ u4ed6 \ Users \ u7740 \ u4e2a \ u5730 \ u5230 \ u5927 \ u91cc \ u8bf4 \ u5c31 \ u53bb \ u5b50 \ u5f97 "+
"\ Users \ u548c \ u90a3 \ u8981 \ Users \ u770b \ Users \ u65f6 \ Users \ u51fa \ Users \ u4e48 \ Users \ u90fd \ u628a \ u597d \ u8fd8 \ u591a \ u6ca1 \ u4e3a \ u53c8 \ u53ef \ u5bb6 \ u5b66 "+
"\ U53ea \ Users \ u6837 \ u5e74 \ u60f3 \ u751f \ Users \ u8001 \ Users \ u5341 \ u4ece \ u81ea \ u9762 \ u524d \ u5934 \ u9053 \ Users \ u540e \ u7136 \ u8d70 \ u5f88 \ u50cf \ u89c1 "+
"\ U4e24 \ u7528 \ u5979 \ u56fd \ u52a8 \ Alibaba \ u6210 \ u56de \ Alibaba \ u5bf9 \ Alibaba \ u800c \ Alibaba \ u73b0 \ u5c71 \ u6c11 \ u5019 \ u7ecf \ u53d1 \ u5de5 \ u5411 \ u4e8b "+
"\ U547d \ u7ed9 \ u957f \ u6c34 \ Users \ u4e49 \ Users \ u58f0 \ Users \ u77e5 \ u7406 \ u773c \ Users \ u5fc3 \ u6218 \ Users \ u95ee \ u4f46 \ u8eab \ u65b9 \ u5b9e "+
"\ U5403 \ u505a \ u53eb \ u5f53 \ Users \ u542c \ u9769 \ Users \ u5462 \ Users \ u5168 \ u624d \ u56db \ Users \ u6240 \ Users \ u6700 \ u5149 \ u4ea7 \ u60c5 \ u8def \ u5206 \ u603b \ u6761 "+
"\ U767d \ u8bdd \ u4e1c \ u5e2d \ Users \ u5982 \ u88ab \ u82b1 \ u53e3 \ u653e \ Users \ u7b2c \ Users \ u5199 \ u519b \ u5427 \ u6587 \ u8fd0 \ u518d \ u679c "+
"\ U600e \ u5b9a \ u8bb8 \ u5feb \ u660e \ u884c \ Users \ u98de \ u5916 \ u6811 \ u00009 \ Users \ u8239 \ u671b \ u65b0 \ u5e26 \ u961f \ u5148 \ u529b "+
"\ Users \ u5374 \ u7ad9 \ u4ee3 \ u5458 \ u673a \ u66f4 \ Users \ u60a8 \ Users \ u98ce \ u7ea7 \ Users \ u4e07 \ u5c11 \ u76f4 \ u610f \ u591c \ u6bd4 \ u9636 "+
"\ U8fde \ u8f66 \ u91cd \ Alibaba \ u6597 \ Alibaba \ u54ea \ u5316 \ Alibaba \ u793e \ Alibaba \ u58eb \ u8005 \ Alibaba \ u77f3 \ u6ee1 \ u65e5 \ u51b3 \ u767e \ u539f \ u62ff \ u7fa4 "+
"\ U7a76 \ u5404 \ u516d \ u672c \ u601d \ u89e3 \ Users \ u6751 \ Users \ u96be \ u65e9 \ Users \ u5417 \ u6839 \ Users \ u76f8 \ u7814 \ u4eca \ u5176 \ u4e66 \ u5750 "+
"\ U63a5 \ u5e94 \ u5173 \ u4fe1 \ u89c9 \ audio \ u53cd \ audio \ u627e \ audio \ u9886 \ u6216 \ audio \ u7ed3 \ u5757 \ u8dd1 \ u8c01 \ u8349 \ u8d8a \ u5b57 \ u52a0 \ u811a "+
"\ U51c6 \ Users \ u56e2 \ Users \ u79bb \ u8272 \ u8138 \ u7247 \ u79d1 \ u5012 \ u775b \ u5229 \ Users \ u521a \ Users \ u7531 \ u9001 \ Users \ u5bfc \ u665a \ u8868 \ u591f \ u6574 \ u8ba4 "+
"\ U544a \ u754c \ u62c9 \ u540d \ u5440 \ Users \ u9633 \ Users \ u53f2 \ u6539 \ Users \ u753b \ u9020 \ u5634 \ u6b64 \ u6cbb \ u5317 \ u5fc5 \ u670d "+
"\ Alibaba \ u8bc6 \ Alibaba \ u4f20 \ u4e1a \ u83dc \ Alibaba \ u7761 \ Alibaba \ u5f62 \ u91cf \ u54b1 \ u89c2 \ u82e6 \ u4f53 \ Alibaba \ u901a \ u51b2 \ u5408 \ u7834 "+
"\ U53cb \ u5ea6 \ u672f \ u996d \ u516c \ u65c1 \ Alibaba \ u6781 \ Alibaba \ u67aa \ Alibaba \ u6c99 \ Alibaba \ u91ce \ Alibaba \ u6536 \ u7b97 \ u81f3 \ u653f \ u57ce "+
"\ U52b3 \ u843d \ u94b1 \ u7279 \ u56f4 \ Alibaba \ u80dc \ u6559 \ u70ed \ Alibaba \ u5305 \ u6b4c \ Alibaba \ u6570 \ Alibaba \ u547c \ u6027 \ u97f3 \ u7b54 \ u54e5 "+
"\ U9645 \ u65e7 \ u795e \ u5ea7 \ Alibaba \ u7cfb \ Alibaba \ u975e \ u4f55 \ Alibaba \ u53d6 \ Alibaba \ u5cb8 \ u6562 \ u6389 \ u5ffd \ u79cd \ u88c5 "+
"\ Users \ u7231 \ Users \ u4e60 \ u9635 \ u6015 \ u6708 \ u9752 \ Users \ u706b \ Users \ u9898 \ Users \ u8d76 \ Users \ u5531 \ u6d77 \ Users \ u5973 \ u4efb \ u4ef6 \ u611f "+
"\ U9876 \ u6025 \ u6797 \ u505c \ u606f \ u53e5 \ u533a \ u8863 \ u822c \ u62a5 \ u53f6 \ Users \ u53d4 \ u80cc \ u7ec6" +
"\ U54cd \ u96ea \ u6d41 \ u672a \ u573a \ u8be5 \ u5e76 \ Alibaba \ u63d0 \ Alibaba \ u4logy \ u8f7b \ u8bb2 \ u519c \ u53e4 \ u9ed1 ";
// The method in Graphics2D can be used to set the rotation radians.
Graphics2D g = (Graphics2D) graphics;
// Set the font color
G. setColor (Color. RED );
// Set the Font Style
G. setFont (new Font ("", Font. BOLD, 30 ));
// Write four men
Int x = 10;
For (int I = 0; I <4; I ++ ){
// Set the font radians
Double d = new Random (). nextInt () % 30;
String target = String. valueOf (base. charAt (new Random (). nextInt (base. length ())));
// Set the font radians
G. rotate (d * Math. PI/180, x, 35 );
// The font is written in the rectangle, and the font space and the font coordinates need to be considered. The y mark remains unchanged.
G. drawString (target, x, 35 );
// Callback the font radians. Otherwise, the next font will be rotated.
G. rotate (-d * Math. PI/180, x, 35 );
// The font size is 20, with no interval of 10.
X + = 45;
}
}
// Sketch the interference lines on the Image
Private void drawRandomLine (Graphics g ){
G. setColor (Color. GREEN); // sets the Color of the interfering line.
// Sketch 10 interference lines
For (int I = 0; I <10; I ++ ){
// The initial Coordinate Position of the sketch line, which must be within the image size
Int x1 = new Random (). nextInt (WIDTH );
Int y1 = new Random (). nextInt (HEIGHT );
// End Coordinate
Int x2 = new Random (). nextInt (WIDTH );
Int y2 = new Random (). nextInt (HEIGHT );
G. drawLine (x1, y1, x2, y2 );
}
}
// Set the border color
Private void setBorder (Graphics g ){
G. setColor (Color. BLUE );
G. drawRect (1, 1, WIDTH-2, HEIGHT-2 );
}
// Set the background color of the image.
Private void setBackGround (Graphics g ){
G. setColor (Color. WHITE); // WHITE
G. fillRect (0, 0, WIDTH, HEIGHT); // fill in the image color
}
@ Override
Protected void doPost (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
DoGet (req, resp );
}
}
---------------------- Effect ---