javajohn

          金色年華

          彩色驗(yàn)證碼實(shí)現(xiàn)

          ??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 ???? // 產(chǎn)生隨機(jī)字符串
          ??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 ???? // 調(diào)用該方法,產(chǎn)生隨機(jī)字符串,
          ?21 ???? // 參數(shù)i:?為字符串的長(zhǎng)度
          ?22 ???? public ?String?runVerifyCode( int ?i) {
          ?23 ????????String?VerifyCode? = ?randString(i);
          ?24 ???????? return ?VerifyCode;
          ?25 ????}

          ?26 ????
          ?27 ????
          ?28 ???? // 給定范圍獲得隨機(jī)顏色
          ?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 ?????? // 調(diào)用該方法將得到的驗(yàn)證碼生成圖象
          ?41 ?????? // sCode:傳遞驗(yàn)證碼?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();???????????????????? // 得到驗(yàn)證碼長(zhǎng)度
          ?48 ???????????? int ?width = 22 * iLength,?height = 20 ;???????????????? // 圖象寬度與高度
          ?49 ???????????? int ?CharWidth? = ?( int )(width - 24 ) / iLength;???????? // 字符距左邊寬度
          ?50 ???????????? int ?CharHeight? = ? 16 ;???????????????????????????? // 字符距上邊高度
          ?51 ????????????
          ?52 ???????????? // ?在內(nèi)存中創(chuàng)建圖象
          ?53 ????????????BufferedImage?image? = ? new ?BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
          ?54 ????????????
          ?55 ???????????? // ?獲取圖形上下文
          ?56 ????????????Graphics?g? = ?image.getGraphics();
          ?57 ????????????
          ?58 ???????????? // 生成隨機(jī)類
          ?59 ????????????Random?random? = ? new ?Random();
          ?60 ????????????
          ?61 ???????????? // ?設(shè)定背景色
          ?62 ????????????g.setColor(getRandColor( 200 , 240 ));
          ?63 ????????????g.fillRect( 0 ,? 0 ,?width,?height);
          ?64 ????????????
          ?65 ???????????? // 設(shè)定字體
          ?66 ????????????g.setFont(CodeFont);
          ?67 ????????????
          ?68 ???????????? // 畫隨機(jī)顏色的邊框
          ?69 ????????????g.setColor(getRandColor( 10 , 50 ));
          ?70 ????????????g.drawRect( 0 , 0 ,width - 1 ,height - 1 );
          ?71 ????????????
          ?72 ???????????? // ?隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
          ?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 ???????????????? // ?將認(rèn)證碼顯示到圖象中
          ?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 ???? // 測(cè)試
          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 閱讀(760) 評(píng)論(0)  編輯  收藏 所屬分類: 我的記憶

          My Links

          Blog Stats

          常用鏈接

          留言簿(7)

          隨筆分類(36)

          隨筆檔案(39)

          classmate

          good blog

          企業(yè)管理網(wǎng)站

          好友

          站點(diǎn)收藏

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 科技| 宝清县| 衡水市| 泽州县| 缙云县| 房产| 江陵县| 托克逊县| 合江县| 左权县| 资溪县| 柳江县| 亚东县| 云龙县| 陆丰市| 大新县| 墨玉县| 黔江区| 延川县| 龙门县| 万年县| 云和县| 甘德县| 邵阳市| 江源县| 临潭县| 揭阳市| 康保县| 固始县| 玛曲县| 茶陵县| 玉屏| 海南省| 文安县| 铜陵市| 定边县| 汉沽区| 怀宁县| 龙口市| 东平县| 遂川县|