---
title: "彩色验证码实现 "
author: "srj2903"
date: "2009-03-19T17:25:55Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-03-19T17:25:55Z"
id: 351667
url: "https://srj2903.iteye.com/blog/351667"
---

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