Verification code tools 【Copy】

public class getRandomPictrue extends BaseAction implements IgnoreAction { 
	
	private ByteArrayInputStream image; // image   
    private String str; // verification code   
    private static final int WIDTH = 80;   
    private static final int HEIGHT = 20; 
	/ ** 
	 * 
	 * / 
	private static final long serialVersionUID = -6098322779401546263L; 

	public String getRandomPictrue () {   
		int width = 120;   
        int height = 30; 
        
     // Step 1 draws a picture in memory   
        BufferedImage bufferedImage = new BufferedImage (width, height,   
                BufferedImage.TYPE_INT_RGB); 
        
     // Step 2 picture Draw background color-through drawing objects   
        Graphics graphics = bufferedImage. getGraphics (); // Get the drawing object --- brush
        
     // Before drawing any graphics, you must specify a color   
        graphics.setColor (getRandColor (200, 250));   
        graphics.fillRect (0, 0, width, height);    
     // Step 3 draw a border   
        graphics.setColor (Color.WHITE) ;   
        graphics.drawRect (0, 0, width-1, height-1); 
     // Step 4 Four random numbers   
        Graphics2D graphics2d = (Graphics2D) graphics;      
      
     // Set the output font   
        graphics2d.setFont (new Font ("宋体", Font.BOLD, 18));   
     
        String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; 
        // String words = "1234567890"; 
        Random random = new Random (); // Generate random numbers 
        
     // Define StringBuffer    
        StringBuffer sb = new StringBuffer (); 
     // Define x coordinate   
        int x = 10;     
       
        for (int i = 0; i <4; i ++) {   
            // Random color   
            graphics2d.setColor (new Color (20 + random.nextInt (110), 20 + random   
                    .nextInt (110), 20 + random.nextInt (110)));   
            // Rotate -30 --- 30 degrees   
            int jiaodu = random.nextInt (60)-30;   
            // Convert radians   
            double theta = jiaodu * Math .PI / 180;   
 
            // Generate a random number   
            int index = random.nextInt (words.length ()); // Generate a random number 0 to length-1   
            // Get the alphanumeric   
            char c = words.charAt (index);   
            sb.append (c);  
            // Output c to picture   
            graphics2d.rotate (theta, x, 20);   
            graphics2d.drawString (String.valueOf (c), x, 20);   
            graphics2d.rotate (-theta, x, 20);   
            x + = 30 ;   
        }   
        
     // Store the generated letters in the session   
        ServletActionContext.getRequest (). GetSession (). SetAttribute ("checkcode", sb.toString ()); 
        
     // Step 5 draw the interference line   
        graphics.setColor (getRandColor (160 , 200));   
        int x1;   
        int x2;   
        int y1;   
        int y2;   
        for (int i = 0; i <30; i ++) {   
            x1 = random.nextInt (width);   
            x2 = random.nextInt (12);   
            y1 = random.nextInt (height);   
            y2 = random.nextInt (12) ;  
            graphics.drawLine (x1, y1, x1 + x2, x2 + y2);   
        }     
        
     // Output the above image to the browser ImageIO   
        graphics.dispose (); // Release resources   
        try { 
			ImageIO.write (bufferedImage, "jpg", ServletActionContext.getResponse (). GetOutputStream ()); 
		} catch (IOException e) { 
			// TODO Auto-generated catch block 
			e.printStackTrace (); 
		} 
        
        return NONE; 
	} 
	/ ** 
     * Function: Set the first verification code Properties 
     * /   
    public void initNumVerificationCode (BufferedImage image) {   
  
        Random random = new Random (); // Generate random class    
        Graphics g = initImage (image, random);  
        String sRand = "";   
        for (int i = 0; i <4; i ++) {  
            String rand = String.valueOf (random.nextInt (10));   
            sRand + = rand;   
            // Display the authentication code in the image   
            g.setColor (new Color (20 + random.nextInt (110), 20 + random   
                    . nextInt (110), 20 + random.nextInt (110)));   
            // The color of the calling function is the same, probably because the seeds are too close, so only 
            g.drawString (rand, 13 * i + 6, 16 can be generated directly   );   
        }   
        this.setStr (sRand); / * Assignment verification code * /   
        // Image takes effect   
        g.dispose (); this.setImage   
        (drawImage (image));   
    }   
    / ** 
     * Function: Set the second verification Code property  
     * /  
    public void initLetterAndNumVerificationCode (BufferedImage image) {  
  
        Random random = new Random(); // 生成随机类  
        Graphics g = initImage(image, random);  
        String[] letter = { "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" };  
        String sRand = "";  
        for (int i = 0; i < 4; i++) {  
            String tempRand = "";  
            if (random.nextBoolean ()) {  
                }  
                    tempRand = tempRand.toLowerCase ();  
                if (random.nextBoolean ()) {// randomly change the letter to lowercase  
            } else {  
                tempRand = String.valueOf (random.nextInt (10));  
                tempRand = letter[random.nextInt(25)];  
            }   
            sRand + = tempRand; g.setColor   
            (new Color (20 + random.nextInt (10), 20 + random   
                    .nextInt (110), 20 + random.nextInt (110)));   
            g.drawString (tempRand, 13 * i + 6, 16);   
        }   
        this.setStr (sRand); / * assignment verification code * /   
        g.dispose (); // image takes effect   
        this.setImage (drawImage (image));   
    }   
  
    / ** 
     * Function: Set the third type of verification code attribute, which is: +? = 24 
     * /   
    public void initChinesePlusNumVerificationCode (BufferedImage image) {    
        String [] cn = {"One", "Two", "Three", "Wu", "Wu", "Lu", "  
        Random random = new Random (); // Generate a random class   
        Graphics g = initImage (image, random);   
        int x = random.nextInt (10) + 1;   
        int y = random.nextInt (30); this.setStr   
        (String.valueOf (y));   
        g.setFont (new Font ("italic", Font.PLAIN, 14)); // Set font   
        g.setColor (new Color (20 + random.nextInt (10), 20 + random.nextInt (110),   
                20 + random. nextInt (110)));   
        g.drawString (cn [x-1], 1 * 1 + 6, 16);   
        g.drawString ("+", 22, 16);   
        g.drawString ("?", 35, 16);   
        g.drawString ("=", 48, 16);   
        g.drawString (String.valueOf (x + y), 61, 16);   
        g.dispose (); // The image takes effect   
        this.setImage (drawImage (image));  
  
    }  
  
    public Graphics initImage (BufferedImage image, Random random) {   
        Graphics g = image.getGraphics (); // Get graphics context   
        g.setColor (getRandColor (200, 250)); // Set background color   
        g.fillRect (0, 0 , WIDTH, HEIGHT);   
        g.setFont (new Font ("Times New Roman", Font.PLAIN, 14)); // set font   
        g.setColor (getRandColor (160, 200)); // randomly generate 165 Interference lines make the authentication code in the image difficult to be detected by other programs   
        for (int i = 0; i <165; i ++) {   
            int x = random.nextInt (WIDTH);   
            int y = random.nextInt (HEIGHT);   
            int xl = random.nextInt (12);   
            int yl = random.nextInt (12);   
            g.drawLine (x, y, x + xl, y + yl);   
        }   
        return g;  
    }  
  
    public ByteArrayInputStream drawImage(BufferedImage image) {  
        ByteArrayInputStream input = null;  
        ByteArrayOutputStream output = new ByteArrayOutputStream();  
        try {  
            ImageOutputStream imageOut = ImageIO  
                    .createImageOutputStream(output);  
            ImageIO.write(image, "JPEG", imageOut);  
            imageOut.close();  
            input = new ByteArrayInputStream(output.toByteArray());  
        } catch (Exception e) {  
            System.out.println("验证码图片产生出现错误:" + e.toString());  
        }  
        return input;  
    }  
  
    /* 
     * Function: get random color in given range 
     * /   
    private Color getRandColor (int fc, int bc) {   
        Random random = new Random ();   
        if (fc> 255)   
            fc = 255;   
        if (bc> 255)   
            bc = 255;   
        int r = fc + random.nextInt (bc-fc);   
        int g = fc + random.nextInt (bc-fc);   
        int b = fc + random.nextInt (bc-fc);   
        return new Color (r, g, b);   
    }   
  
    / ** 
     * Function: Get the string value of the verification code 
     *   
     * @return 
     * /   
    public String getVerificationCodeValue () {   
        return this.getStr ();   
    }   
  
    / ** 
     * function: image acquisition codes 
     *  
     * @return 
     */  
    public ByteArrayInputStream getImage() {  
        return this.image;  
    }  
  
    public String getStr() {  
        return str;  
    }  
  
    public void setStr(String str) {  
        this.str = str;  
    }  
  
    public void setImage(ByteArrayInputStream image) {  
        this.image = image;  
    } 
    
    private void mian() {
		// TODO Auto-generated method stub
    	System.out.println(getRandomPictrue());
	}
}

 

Guess you like

Origin www.cnblogs.com/hmhhz/p/12689479.html