Java generates Verification Code Image

Source: Internet
Author: User

 

I haven't written a blog for a long time, and I forget that I have an account here. Okay, don't worry. I 'd like to share with you the image code for generating the verification code.

Import java. awt. color; import java. awt. font; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import java. util. random; import javax. imageio. imageIO; import javax. imageio. stream. fileImageOutputStream; public class ValidateCode {/*** Verification Code attribute name */public static final String VALIDATE_CODE_NAME = "validate_code"; char [] codeSequence = {'0', '1 ', '2 ', '3', '4', '5', '6', '7', '8', '9'};/*** width of the Verification Code image. */Private int width = 75;/*** height of the Verification Code image. */Private int height = 20;/*** number of characters in the Verification Code */private int codeCount = 4;/*** xx */private int xx = 0; /*** fontHeight font height */private int fontHeight;/*** codeY */private int codeY; public ValidateCode () {xx = width/(codeCount + 1 ); fontHeight = height-2; codeY = height-4;} public void getValidateImage () {// defines the image bufferBufferedImage buffImg = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics2 D gd = buffImg. createGraphics (); // create a Random number generator class random Random = new Random (); // fill the image with white gd. setColor (Color. WHITE); gd. fillRect (0, 0, width, height); // create a font. The font size depends on the Image height. Font font = new Font ("Fixedsys", Font. BOLD, fontHeight); // set the Font. Gd. setFont (font); // draw a border. Gd. setColor (Color. BLACK); gd. drawRect (0, 0, width-1, height-1); // generates 160 random interference lines, making it difficult for other programs to detect the authentication code in the image. Gd. setColor (Color. BLACK); for (int I = 0; I <10; 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 is used to save the randomly generated Verification Code, so that the user can perform verification after logon. StringBuffer randomCode = new StringBuffer (); int red = 0, green = 0, blue = 0; // a random codeCount verification code is generated. For (int I = 0; I <4; I ++) {// obtain the randomly generated verification code number. String strRand = String. valueOf (codeSequence [random. nextInt (10)]); // generate a random color component to construct the color value, so that the color value of each output number will be different. Red = random. nextInt (255); green = random. nextInt (255); blue = random. nextInt (255); // draw the verification code into the image with a random color. Gd. setColor (new Color (red, green, blue); gd. drawString (strRand, (I + 1) * xx, codeY); // combine the four random numbers. RandomCode. append (strRand);} try {FileImageOutputStream writeImage = new FileImageOutputStream (new File ("c: \ pic.jpg"); ImageIO. write (buffImg, "jpeg", writeImage); writeImage. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}} public static void main (String [] args) {ValidateCode vc = new ValidateCode (); vc. getValidateImage ();}}

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.