Java generated image verification code used when logging in

Source: Internet
Author: User

 Packagecom.ws.frame.utils;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;/*** Random Verification Code generation rules for page anti-violence hack *@authorYingfu*/ Public classCaptcha { Public Static FinalString Session_key = "CAPTCHA";//the name of the session property    Private Static Final intWIDTH = 80;//Picture Width    Private Static Final intHEIGHT = 26;//Picture Height    Private Static Final intLinesize = 8;//number of interference lines in pictures    Private Static Final intNumber = 4;//number of characters produced    Private Static Final intFONTSIZE = 18;//Picture Text Size    Private Static Final intR = 255; Private Static Final intG = 255; Private Static Final intB = 255; Private Static Final intDefault_font_style = Font.center_baseline;//Default Style    Private Static FinalString default_font_family = Font.sans_serif;//Default Font    /*** Random number generation pool*/    Private Static Final Char[] Charspool = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ',            ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J ',            ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' W ',            ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ',            ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ',            ' X ', ' Y ', ' Z ' }; PrivateRandom random =NewRandom (); /*** Generate random characters *@returnstring*/    Private CharRandomcode () {intLength =charspool.length; intnum =random.nextint (length); returnCharspool[num]; }        /*** Set the style of random colors *@returnColor*/    PrivateColor Colorstyle () {intR =Random.nextint (R); intg =Random.nextint (G); intb =Random.nextint (B); return NewColor (R, G, b); }        /*** Set the basic style of the font *@returnFont*/    PrivateFont FontStyle () {//use for Linux serversSystem.setproperty ("Java.awt.headless", "true"); return NewFont (default_font_family, Default_font_style, FONTSIZE); }        /*** Set the color of the text *@return     */    PrivateColor Fontcolorstyle () {intr = Random.nextint (100); intg = Random.nextint (100); intb = Random.nextint (100); return NewColor (R, G, b); }        /*** Draw Random numbers *@paramG *@paramI *@returnChar*/    Private CharDrawrandomcode (Graphics G,inti) {G.setfont (FontStyle ());//G.setcolor (Colorstyle ());G.setcolor (Fontcolorstyle ()); CharRandomcode =Randomcode (); G.translate (Random.nextint (3), Random.nextint (3)); g.DrawString (String.valueof (randomcode),* I, 16); returnRandomcode; }        /*** Draw Interference lines *@paramg*/    Private voidDrawLine (Graphics g) {intx =Random.nextint (WIDTH); inty =Random.nextint (HEIGHT); intx2 = Random.nextint (WIDTH/2); inty2 = Random.nextint (height/2);    G.drawline (x, y, x2, y2); }        /*** Generate Verification code *@paramSession *@paramResponse*/     Public voidBuildcaptcha (HttpSession session, HttpServletResponse response) {BufferedImage image=Newbufferedimage (WIDTH, HEIGHT,BUFFEREDIMAGE.TYPE_INT_BGR); Graphics g=Image.getgraphics (); G.fillrect (0, 0, WIDTH, HEIGHT);        G.setfont (FontStyle ());        G.setcolor (Colorstyle ());  for(inti=0;i<linesize;i++) {drawLine (g); } StringBuilder Code=NewStringBuilder ();  for(intI=1; i<= number; i++) {code.append (Drawrandomcode (g, i));        } session.removeattribute (Session_key);        Session.setattribute (Session_key, code.tostring ());        G.dispose (); Try{imageio.write (image,"JPEG", Response.getoutputstream ()); } Catch(IOException e) {e.printstacktrace (); }    }}

The color of the font in the picture and the drawing line are randomly generated.

Java generated image verification code used when logging in

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.