The Java code to achieve

Example:

/**
 * Created by hzllb
 */

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class VerifyCodeUtil {
    private static int width = 90;// 定义图片的width
    private static int height = 20;//Definition image height 
    Private  static  int codeCount =. 4; // definition image display of the number of codes 
    Private  static  int XX = 15 ;
     Private  static  int FontHeight = 18 is ;
     Private  static   int , codeY = 16 ;
     Private  static  char [] = codeSequence { 'A', 'B' , 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', ' M ',' N ',' O ',' P ',' Q ',' R & lt ' ,
             ' S ',' T ',' the U-',' V ',' W is', 'X-', 'the Y' ,'The Z', '0', '. 1', '2', '. 3', '. 4', '. 5', '. 6', '. 7', '. 8', '. 9' }; 

    / ** 
     * generated a map set 
     * code to generate a verification code
     * CodePic BufferedImage object codes is generated 
     * @return 
     * / 
    public  static the Map <String, Object> generateCodeAndPic () {
         // definition of the image Buffer 
        BufferedImage buffImg = new new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
         // the Graphics2D Gd buffImg.createGraphics = ();
         // the Graphics2D Gd = (the Graphics2D) buffImg.getGraphics (); 
        Graphics Gd = buffImg.getGraphics ();
         // a random number generator class creates 
        the random random = new new the random ();
         // a The image filled with white 
         gd.setColor (Color.WHITE);
        gd.fillRect ( 0, 0;, Width, height) 

        // create the font, font size should be set according to the height of the picture. 
        Font = font new new font ( "Fixedsys" , Font.BOLD, FontHeight);
         // set the font. 
        gd.setFont (font); 

        // draw the border. 
        gd.setColor (as Color.BLACK.); 
        gd.drawRect ( 0, 0, width -. 1, height -. 1 ); 

        // randomly generated interference line 40, the authentication code so that the image is not easy to be detected other programs. 
        gd.setColor (as Color.BLACK.);
         for ( int I = 0; I <30; I ++ ) {
             int X = random.nextInt (width);
             int Y = intrandom.nextInt (height);
             XL = random.nextInt (12 is );
             int YL = random.nextInt (12 is ); 
            gd.drawLine (X, Y, X + XL, Y + YL); 
        } 

        // randomCode for storing randomly generated verification code to verify the user logon. 
        = RandomCode the StringBuffer new new the StringBuffer ();
         int Red = 0, 0 = Green, Blue = 0 ; 

        // randomly generated numbers codeCount codes. 
        for ( int I = 0; I <codeCount; I ++ ) {
             // get the digital codes generated at random. 
            String.valueOf code = String (codeSequence [random.nextInt (36 )]);
             //Generating a random color component values to construct a color, the color value of each digit to thus output are different. 
            random.nextInt = Red (255 ); 
            Green = random.nextInt (255 ); 
            Blue = random.nextInt (255 ); 

            // color code will be randomly generated into the image drawing. 
            gd.setColor ( new new Color (Red, Green, Blue)); 
            gd.drawString (code, (I +. 1) * XX,, codeY); 

            // four combined random number generated. 
            randomCode.append (code); 
        } 
        the Map <String, Object> = Map new new the HashMap <String, Object> ();
         // store codes 
        map.put ( "code", RandomCode);
         // store BufferedImage object codes generated 
        map.put ( "codePic" , buffImg);
         return Map; 
    } 

    public  static  void main (String [] args) throws Exception {
         // create a file output stream object 
        OutputStream out = new new a FileOutputStream ( "D: \\" + System.currentTimeMillis () +. "JPG" ); 
        the Map <String, Object> Map = VerifyCodeUtil.generateCodeAndPic (); 
        ImageIO.write ((the RenderedImage) as map.get ( "codePic ")," JPEG " , OUT); 
        System.out.println ( " code verification is: "+ map.get ("code"code"));
    }
}

effect:

 

Guess you like

Origin www.cnblogs.com/wuba/p/11516925.html