java生成动态gif格式与png格式的验证码(代码4)

来源:互联网 发布:一组数据的加权平均值 编辑:程序博客网 时间:2024/05/21 18:43
Java代码  收藏代码
  1. import java.util.Random;  
  2.   
  3. /** 
  4.  * 随机工具类 
  5.  * @author wzztestin 
  6.  * 
  7.  */  
  8. public class Randoms {  
  9.     private static final Random RANDOM = new Random();  
  10.     // 定义验证码字符.去除了O和I等容易混淆的字母  
  11.     public static final char shuzi[] = { '1''2''3''4''5''6''7','8''9'};  
  12.     public static final char chaozuofu[] = { '+''-''×''÷'};  
  13.   
  14.     /** 
  15.      * 产生两个数之间的随机数 
  16.      *  
  17.      * @param min 
  18.      *            小数 
  19.      * @param max 
  20.      *            比min大的数 
  21.      * @return int 随机数字 
  22.      */  
  23.     public static int num(int min, int max) {  
  24.         return min + RANDOM.nextInt(max - min);  
  25.     }  
  26.   
  27.     /** 
  28.      * 产生0--num的随机数,不包括num 
  29.      *  
  30.      * @param num 
  31.      *            数字 
  32.      * @return int 随机数字 
  33.      */  
  34.     public static int num(int num) {  
  35.         return RANDOM.nextInt(num);  
  36.     }  
  37.   
  38.     public static char getNum() {  
  39.         return shuzi[num(0, shuzi.length)];  
  40.     }  
  41.       
  42.     public static char getChaoZuoFu() {  
  43.         return chaozuofu[num(02)];  
  44.     }  
  45. }  

 

Java代码  收藏代码
  1. import java.awt.AlphaComposite;  
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.IOException;  
  7. import java.io.OutputStream;  
  8.   
  9. import javax.imageio.ImageIO;  
  10.   
  11. /** 
  12.  * png格式验证码 
  13.  * @author wzztestin 
  14.  * 
  15.  */  
  16. public class SpecCaptcha extends Captcha {  
  17.     public SpecCaptcha() {  
  18.     }  
  19.       
  20.     /** 
  21.      * 初始化宽高 
  22.      * @param width 
  23.      * @param height 
  24.      */  
  25.     public SpecCaptcha(int width, int height) {  
  26.         this.width = width;  
  27.         this.height = height;  
  28.     }  
  29.       
  30.     public SpecCaptcha(int width, int height, int alpha) {  
  31.         this(width, height);  
  32.     }  
  33.   
  34.     public SpecCaptcha(int width, int height, int len, Font font) {  
  35.         this(width, height, len);  
  36.         this.font = font;  
  37.     }  
  38.   
  39.     /** 
  40.      * 生成验证码 
  41.      *  
  42.      * @throws java.io.IOException 
  43.      *             IO异常 
  44.      */  
  45.     @Override  
  46.     public void out(OutputStream out) {  
  47.         graphicsImage(getVilidCode().toCharArray(), out);  
  48.     }  
  49.   
  50.     /** 
  51.      * 画随机码图 
  52.      *  
  53.      * @param strs 
  54.      *            文本 
  55.      * @param out 
  56.      *            输出流 
  57.      */  
  58.     private boolean graphicsImage(char[] strs, OutputStream out) {  
  59.         boolean ok = false;  
  60.         try {  
  61.             BufferedImage bi = new BufferedImage(width, height,  
  62.                     BufferedImage.TYPE_INT_RGB);  
  63.             Graphics2D g = (Graphics2D) bi.getGraphics();  
  64.             AlphaComposite ac3;  
  65.             Color color;  
  66.             int len = strs.length;  
  67.             g.setColor(Color.WHITE);  
  68.             g.fillRect(00, width, height);  
  69.             // 随机画干扰的蛋蛋  
  70.             for (int i = 0; i < 15; i++) {  
  71.                 color = color(150250);  
  72.                 g.setColor(color);  
  73.                 g.drawOval(Randoms.num(width), Randoms.num(height),  
  74.                         5 + Randoms.num(10), 5 + Randoms.num(10));// 画蛋蛋,有蛋的生活才精彩  
  75.                 color = null;  
  76.             }  
  77.             g.setFont(font);  
  78.             int h = height - ((height - font.getSize()) >> 1), w = width / len, size = w  
  79.                     - font.getSize() + 1;  
  80.             /* 画字符串 */  
  81.             for (int i = 0; i < len; i++) {  
  82.                 ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);// 指定透明度  
  83.                 g.setComposite(ac3);  
  84.                 color = new Color(20 + Randoms.num(110), 20 + Randoms.num(110),  
  85.                         20 + Randoms.num(110));// 对每个字符都用随机颜色  
  86.                 g.setColor(color);  
  87.                 g.drawString(strs[i] + "", (width - (len - i) * w) + size,  
  88.                         h - 4);  
  89.                 color = null;  
  90.                 ac3 = null;  
  91.             }  
  92.             ImageIO.write(bi, "png", out);  
  93.             out.flush();  
  94.             ok = true;  
  95.         } catch (IOException e) {  
  96.             ok = false;  
  97.         } finally {  
  98.             Streams.close(out);  
  99.         }  
  100.         return ok;  
  101.     }  
  102. }  
0 0
原创粉丝点击