javajohn

          金色年華

          彩色驗證碼實現

          ??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 }

          posted on 2006-07-17 11:17 javajohn 閱讀(758) 評論(0)  編輯  收藏 所屬分類: 我的記憶

          My Links

          Blog Stats

          常用鏈接

          留言簿(7)

          隨筆分類(36)

          隨筆檔案(39)

          classmate

          good blog

          企業管理網站

          好友

          站點收藏

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 富锦市| 二连浩特市| 大邑县| 万州区| 南开区| 柳河县| 义乌市| 洪泽县| 延长县| 肃南| 大关县| 宁阳县| 疏勒县| 凯里市| 绍兴县| 临潭县| 上饶市| 深水埗区| 娄烦县| 罗江县| 江城| 长宁区| 博爱县| 五家渠市| 扶余县| 连平县| 江城| 乌兰浩特市| 留坝县| 玉山县| 茌平县| 双牌县| 衡南县| 南华县| 兴仁县| 洪湖市| 霍山县| 多伦县| 阿瓦提县| 德钦县| 叙永县|