Welcome 微信登录

首页 / 操作系统 / Linux / Java生成验证码

Java生成验证码
  1. package cn.net.seek.util;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.io.IOException;  
  8. import java.io.OutputStream;  
  9. import java.util.Random;  
  10.   
  11. import javax.imageio.ImageIO;  
  12.   
  13. public class ImgZoom {  
  14.   
  15.     // 验证码图片中可以出现的字符集,可根据需要修改   
  16.     private char mapTable[] = { "A""B""C""D""E""F""G""H""I",  
  17.             "J""K""L""M""N""O""P""Q""R""S""T""U""V",  
  18.             "W""X""Y""Z""a""b""c""d""e""f""g""h""i",  
  19.             "j""k""l""m""n""o""p""q""r""s""t""u""v",  
  20.             "w""x""y""z""0""1""2""3""4""5""6""7""8",  
  21.             "9" };  
  22.       
  23.   
  24.     /** 
  25.      * 生成验证码 
  26.      * @param width 宽度  60 
  27.      * @param height 高度  24 
  28.      * @param os 输出流 
  29.      * @return 生成的随机字符串 
  30.      */  
  31.     public String getCertPic(int width, int height, OutputStream os) {  
  32.         BufferedImage image = new BufferedImage(width, height,  
  33.                 BufferedImage.TYPE_INT_RGB);  
  34.         // 获取图形上下文   
  35.         Graphics g = image.getGraphics();  
  36.   
  37.         // 生成随机类   
  38.         Random random = new Random();  
  39.   
  40.         // 设定背景色   
  41.         // g.setColor(getRandColor(200, 250)); //随机生成背景色   
  42.         g.setColor(new Color(255255255));  
  43.         g.fillRect(00, width, height);  
  44.   
  45.         // 设定字体   
  46.         g.setFont(new Font("Times New Roman", Font.PLAIN, 18));  
  47.   
  48.         // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到   
  49.         g.setColor(getRandColor(160200));  
  50.         for (int i = 0; i < 155; i++) {  
  51.             int x = random.nextInt(width);  
  52.             int y = random.nextInt(height);  
  53.             int xl = random.nextInt(12);  
  54.             int yl = random.nextInt(12);  
  55.             g.drawLine(x, y, x + xl, y + yl);  
  56.         }  
  57.   
  58.         // 取随机产生的认证码(5位)   
  59.         String sRand = "";  
  60.         for (int i = 0; i < 5; i++) {  
  61.             String rand = mapTable[(int) (mapTable.length * Math.random())]  
  62.                     + "";  
  63.             sRand += rand;  
  64.             // 将认证码显示到图象中   
  65.             g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  66.                     .nextInt(110), 20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成   
  67.             g.drawString(rand, 10 * i + 3, (int) (Math.random() * 9 + 14));// 14-23 Math.round(Math.random()*(Max-Min)+Min)   
  68.         }  
  69.         // 释放图形上下文   
  70.         g.dispose();  
  71.         try {  
  72.             // 输出图象到页面   
  73.             ImageIO.write(image, "JPEG", os);  
  74.         } catch (IOException e) {  
  75.             return "";  
  76.         }  
  77.         return sRand;  
  78.     }  
  79.   
  80.     Color getRandColor(int fc, int bc) {  
  81.         // 给定范围获得随机颜色   
  82.         Random random = new Random();  
  83.         if (fc > 255)  
  84.             fc = 255;  
  85.         if (bc > 255)  
  86.             bc = 255;  
  87.         int r = fc + random.nextInt(bc - fc);  
  88.         int g = fc + random.nextInt(bc - fc);  
  89.         int b = fc + random.nextInt(bc - fc);  
  90.         return new Color(r, g, b);  
  91.     }  
  92.   
  93. }