Java Generate Verification Code

Source: Internet
Author: User

 Packagecom.lf.testvity;ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Graphics2D;ImportJava.awt.Robot;ImportJava.awt.image.BufferedImage;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.util.Random;ImportJavax.imageio.ImageIO;Importjavax.security.auth.message.callback.PrivateKeyCallback.Request;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.http.HttpSession;ImportJavax.sound.midi.Patch;Importorg.junit.Test; Public classVitiry {//number of verification code characters    Private Static intCount = 4; //number of lines of interference    Private Static intLines = 2; /*** Randomly generated color *@returnColor*/    Private StaticColor Getrandomcolor () {random random=NewRandom (); Color Color=NewColor (Random.nextint (255) +1, Random.nextint (255) +1, Random.nextint (255) +1); returncolor; }    /*** Get a four-character string *@returnstring*/     Public Staticstring Getforthword () {string string= "23456789ABCDEFGHIJKMNPQRSTUVWXYZ"; StringBuilder Newstr=NewStringBuilder (""); //randomly gets count numbers, resulting in a string based on count random numbersRandom random =NewRandom ();  for(inti = 0; I < count; i++) {            intrannum=Random.nextint (String.Length ());        Newstr.append (String.charat (rannum)); } String str=NewString (NEWSTR); returnstr; }    /*** Painting Verification code *@returnBufferedImage*/     Public Staticbufferedimage productimage () {intwidth = 70; intHeight = 30; //get the picture bufferBufferedImage bi =Newbufferedimage (width, height, bufferedimage.type_int_rgb); //get it in the drawing environment (this picture of the pen)graphics2d g2 =(graphics2d) bi.getgraphics (); //Set ColorG2.setcolor (Color.White); //fills the whole picture (actually setting the background color)G2.fillrect (0, 0, width, height); //Set FontG2.setfont (NewFont ("Arial", Font.Bold, 25)); //Set ColorG2.setcolor (Vitiry.getrandomcolor ()); //write a string to a pictureG2.drawstring (Vitiry.getforthword (), 7, 25); //draw two lines of interferenceRandom random =NewRandom ();  for(inti = 0; i < lines; i++) {G2.drawline (2, Random.nextint (height-10) +10, width-5, Random.nextint (height)); }                returnbi; }    }

Java Generate Verification Code

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.