--彩色验证码实现

来源:百度文库 编辑:神马文学网 时间:2024/04/29 04:24:45
1 public   class  VerifyCode  {
2     static  Random r  =   new  Random();
3     static  String ssource  =   " ABCDEFGHIJKLMNOPQRSTUVWXYZ "    +   " abcdefghijklmnopqrstuvwxyz "   +   " 0123456789 " ;
4     static   char [] src  =  ssource.toCharArray();
5
6
7     // 产生随机字符串
8
9     private   static  String randString ( int  length) {
10         char [] buf  =   new   char [length];
11         int  rnd;
12         for ( int  i = 0 ;i < length;i ++ ) {
13            rnd  =  Math.abs(r.nextInt())  %  src.length;
14
15            buf[i]  =  src[rnd];
16        }
17         return   new  String(buf);
18    }
19
20     // 调用该方法,产生随机字符串,
21     // 参数i: 为字符串的长度
22      public  String runVerifyCode( int  i) {
23        String VerifyCode  =  randString(i);
24         return  VerifyCode;
25    }
26
27
28     // 给定范围获得随机颜色
29      public  Color getRandColor( int  fc, int  bc)
30     {
31       Random random  =   new  Random();
32        if (fc > 255 ) fc = 255 ;
33        if (bc > 255 ) bc = 255 ;
34        int  r = fc + random.nextInt(bc - fc);
35        int  g = fc + random.nextInt(bc - fc);
36        int  b = fc + random.nextInt(bc - fc);
37        return   new  Color(r,g,b);
38       }
39
40       // 调用该方法将得到的验证码生成图象
41       // sCode:传递验证码 w:图象宽度 h:图象高度
42        public  BufferedImage CreateImage(String sCode)
43       {
44           try {
45               // 字符的字体
46             Font CodeFont  =   new  Font( " Arial Black " ,Font.PLAIN, 16 );
47             int  iLength  =  sCode.length();                     // 得到验证码长度
48              int  width = 22 * iLength, height = 20 ;                 // 图象宽度与高度
49              int  CharWidth  =  ( int )(width - 24 ) / iLength;         // 字符距左边宽度
50              int  CharHeight  =   16 ;                             // 字符距上边高度
51
52             //  在内存中创建图象
53             BufferedImage image  =   new  BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
54
55             //  获取图形上下文
56             Graphics g  =  image.getGraphics();
57
58             // 生成随机类
59             Random random  =   new  Random();
60
61             //  设定背景色
62             g.setColor(getRandColor( 200 , 240 ));
63            g.fillRect( 0 ,  0 , width, height);
64
65             // 设定字体
66             g.setFont(CodeFont);
67
68             // 画随机颜色的边框
69             g.setColor(getRandColor( 10 , 50 ));
70            g.drawRect( 0 , 0 ,width - 1 ,height - 1 );
71
72             //  随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
73             g.setColor(getRandColor( 160 , 200 ));
74             for  ( int  i = 0 ;i < 155 ;i ++ )
75             {
76                   int  x  =  random.nextInt(width);
77                   int  y  =  random.nextInt(height);
78                   int  xl  =  random.nextInt( 12 );
79                   int  yl  =  random.nextInt( 12 );
80                  g.drawLine(x,y,x + xl,y + yl);
81            }
82
83
84             for  ( int  i = 0 ;i < iLength;i ++ )
85             {
86                String rand  =  sCode.substring(i,i + 1 );
87                 //  将认证码显示到图象中
88                 g.setColor( new  Color( 20 + random.nextInt( 60 ), 20 + random.nextInt( 120 ), 20 + random.nextInt( 180 )));
89                g.drawString(rand,CharWidth * i + 14 ,CharHeight);
90            }
91             //  图象生效
92             g.dispose();
93             return  image;
94        } catch (Exception e) {
95             // e.printStackTrace();
96             // System.out.println(e.getMessage());
97             }
98         return   null ;
99    }
100
101     // 测试
102      public   static   void  main(String[] args) {
103             // VerifyCode vc = new VerifyCode();
104             // String s1 = vc.runVerifyCode(4);
105             // Fun.DreamNewsTitle;System.out.println(s1);
106             // Image im = vc.CreateImage(s1);
107             // Graphics g = im.getGraphics();
108             // g.drawImage(im,20,20,this);
109             // g.drawString(s1,20,20);
110
111    }
112}