java authentication code generation tools

package utils;



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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CodeUtils {
    //得到随机数
    private static char[] = Chars "abdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789" .toCharArray ();
     Private  static the Random Random = new new the Random ();
     Private  static String the getCode ( int length) { 
        the StringBuffer SB = new new the StringBuffer ();
         for ( int I = 0; I <length; ++ I ) { 
            sb.append (chars [random.nextInt (chars.length)]); 
        } 
        return sb.toString (); 
    } 
    
    / * 
     * @ DESCRIPTION: design codes and writes the image file 
     * / 
    public  static  voidthe createCode (the HttpServletRequest REQ, the HttpServletResponse RESP) {
         the try {
             // 1. Create canvas 
            the BufferedImage BufferedImage = new new the BufferedImage (120, 34 is , BufferedImage.TYPE_INT_RGB);
             // 2. Create brush 
            the Graphics2D Graphics2D = bufferedImage.createGraphics ();
             // . 6 setting the background color, the first color set the brush in the painted canvas 
            graphics2d.setColor ( new new color (249, 250, 108 ));
             // 7. the painted canvas 
            graphics2d.fillRect (0, 0, 120, 34 is );
             // 8. update pen color 
            graphics2d.setColor (new Color(62, 128, 27));
            //9.设置字体
            graphics2d.setFont(new Font("Dope Crisis", Font.PLAIN, 40));
            String code = getCode(4);
            //12.保存到sesssion中
            HttpSession session=req.getSession();
            session.setAttribute("code", code);
            session.setMaxInactiveInterval(1000);
            //3.写字
            graphics2d.drawString(code, 25,30);
            //4.收笔
            graphics2d.dispose();
            //5. saved to the server 
            ImageIO.write (BufferedImage, "JPG" , resp.getOutputStream ()); 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
    } 
    
    public  static  void main (String [] args) throws Exception {
         // 1. Create canvas 
        the BufferedImage BufferedImage = new new the BufferedImage (120, 34 is , BufferedImage.TYPE_INT_RGB);
         // 2. Create brush 
        the Graphics2D Graphics2D = bufferedImage.createGraphics ();
         // 6. The background color setting, to set the pen color, in the canvas painted 
        graphics2d.setColor ( new newColor (20 is, 123, 228 ));
         // 7. The painted canvas 
        graphics2d.fillRect (0, 0, 120, 34 is );
         // 8. The pen color update 
        graphics2d.setColor ( new new Color (246, 129, 50 ) );
         // 9. set Font 
        graphics2d.setFont ( new new the Font ( "Dope Crisis", Font.PLAIN, 40 )); 
        String NUM = the getCode (. 4 );
         // 3. write 
        graphics2d.drawString (num, 25,30 );
         // 4. pen collection 
        graphics2d.dispose ();
         // 5. the saved to the hard disk 
        ImageIO.write (BufferedImage, "JPG", new newFile ( "C: \\ abc.jpg" )); 
    } 
    // 11. The saved on a server 
}

 

Guess you like

Origin www.cnblogs.com/wxldlxt/p/11310978.html
Recommended