熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

支持中文的驗證碼生成程序.

2013-11-23 19:21:38  來源: Java核心技術 

  一個驗證圖形生成程序 支持中文的驗證碼生成程序 寫的時候沒有仔細看 可能有小問題但是運行沒有任何問題

  使用方法 :
    <tr><td>&nbsp;驗 證 碼:&nbsp;</td>
    <td><input tabindex= maxlength= size= name=yzmimg usage=int tip=驗證碼請輸入阿拉伯數字! title=輸入阿拉伯數字/>&nbsp;</td>
    <td><a javascript:refreshImg(validateCodeImg);><img src=yzmImgjsp alt=請在左側輸入對應的阿拉伯數字 name=validateCodeImg border= align=absmiddle id=validateCodeImg /></a>
    輸入對應阿拉伯數字.看不清點請點擊更換</td></tr>

  <script language=javascript type=text/javascript>
    function refreshImg(imgID){var img = documentgetElementById(imgID);  imgsrc = imgsrc;}
    </script>
    生成圖形的jsp文件:

  <%@page session=true

  contentType=image/jpeg; charset=gb

  import=javaawtimage*javaawt*

  import=javautil*

  import=javaxservlet*

  import=javaxservlethttp*

  import=javaximageio*

  import=javaioOutputStream

  import=javaioOutputStream

  %><%!

  private int iCharHeight = ;

  private int iCharCnt = ;

  private int iMaxOffset = ; //字符位置左右最大便宜像素

  private int iImgWidth = *iCharCnt + iMaxOffset*; //圖片寬度

  private int iImgHeight = iCharHeight + ; //圖片高度

  private int iDisturbLineCnt = ; //干擾線數量

  //////////////////////////////////////////////////////

  //private String Src = 零壹貳三肆伍陸柒捌玖;

  private String Src = 零一二三四五六七八九;

  //private String Src = 的了是我不在人們有來他這上著個地到大裡說就去子得也和那要下看天時過出小麼起你都把好還多沒為又可家學只以主會樣年想生同老中十從自面前頭道它後然走很像見兩用她國動進成回什邊作對開而己些現山民候經發工向事命給長水幾義三聲於高手知理眼志點心戰二問但身方實吃做叫當住聽革打呢真全才四已所敵之最光產情路分總條白話東席次親如被花口放兒常氣五第使寫軍吧文運再果怎定許快明行因別飛外樹物活部門無往船望新帶隊先力完卻站代員機更九您每風級跟笑啊孩萬少直意夜比階連車重便斗馬哪化太指變社似士者干石滿日決百原拿群究各六本思解立河村八難早論嗎根共讓相研今其書坐接應關信覺步反處記將千找爭領或師結塊跑誰草越字加腳緊愛等習陣怕月青半火法題建趕位唱海七女任件感准張團屋離色臉片科倒睛利世剛且由送切星導晚表夠整認響雪流未場該並底深刻平偉忙提確近亮輕講農古黑告界拉名呀土清陽照辦史改歷轉畫造嘴此治北必服雨穿內識驗傳業菜爬睡興形量咱觀苦體眾通沖合破友度術飯公旁房極南槍讀沙歲線野堅空收算至政城勞落錢特圍弟勝教熱展包歌類漸強數鄉呼性音答哥際舊神座章幫啦受系令跳非何牛取入岸敢掉忽種裝頂急林停息句區衣般報葉壓慢叔背細;

  String[] strFontNames = {

  隸書 華文新魏 黑體 幼圓 楷體_GB 新宋體 華文彩雲};

  private int FONTSTYLE = FontBOLD | FontITALIC;

      //////////////////////////////////////////////////////

  private String getRandomStr(Random random int cnt StringBuffer sbNum) {

  String str = ;

  int max = Srclength();

  for (int i = ; i < cnt; i++) {

  int t = randomnextInt(max);

  str += SrccharAt(t);

  sbNumappend(t);

  }

  return str;

  }


      //生成隨機顏色

  Color getRandColor(Random random int fc int bc) {

  if (fc > )

  fc = ;

  if (bc > )

  bc = ;

  int r = fc + randomnextInt(bc fc);

  int g = fc + randomnextInt(bc fc);

  int b = fc + randomnextInt(bc fc);

  return new Color(r g b);

  }

  %><%

  String strText = ;

  try {

  Date now = new Date();

  Random random = new Random(nowgetTime());

  now = null;

  StringBuffer bufNum = new StringBuffer();

  strText = getRandomStr(random iCharCnt bufNum);

  responsesetHeader(CacheControl nostore); //HTTP

  responsesetHeader(Pragma nocache); //HTTP

  responsesetDateHeader(Expires ); //prevents caching at the proxy server

  BufferedImage image = new BufferedImage(iImgWidth iImgHeight BufferedImageTYPE_INT_RGB);

  Graphics g = imagegetGraphics();

  // 設定圖像背景色(因為是做背景所以偏淡)

  gsetColor(getRandColor(random ));

  gfillRect( iImgWidth iImgHeight);

  //畫邊框

  gsetColor(Colorblack);

  gdrawRect( iImgWidth iImgHeight );

  //干擾線

  

  for (int k = ; k < iDisturbLineCnt; k++)

  {

  gsetColor(getRandColor(random ));

  gdrawLine(randomnextInt(iImgWidth) randomnextInt(iImgHeight) randomnextInt(iImgWidth) randomnextInt(iImgHeight));

  }

  int startx = ;

  int strWidth = ;

  for (int i = ; i < iCharCnt; i++) {

  gsetColor(getRandColor(random ));

  char ch = strTextcharAt(i);

  String fname = strFontNames[randomnextInt(strFontNameslength)];

  Font f = new Font(fname FONTSTYLE iCharHeight);

  Systemoutprintln(fname +   + ch);

  gsetFont(f);

  gdrawString( + ch startx + strWidth + randomnextInt(iMaxOffset) iCharHeight);

  javaawtFontMetrics fm = ggetFontMetrics();

  strWidth += fmcharWidth(ch);

  fm = null;

  }

  // 圖象生效

  gdispose();

  ServletOutputStream responseOutputStream = responsegetOutputStream();

  // 輸出圖象到頁面

  ImageIOwrite(image JPEG responseOutputStream);

  //以下關閉輸入流!~~~

  responseOutputStreamflush();

  responseOutputStreamclose();

  /**//*

  ServletOutputStream outstream = responsegetOutputStream();

  JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(outstream);

  encoderencode(image);

  outstreamclose();

  */

  // 將認證碼存入SESSION

  sessionsetAttribute(YZMIMAGE bufNumtoString());

  random = null;

  bufNum = null;

  image = null;

  }

  catch (Exception ex) {exprintStackTrace();  }%>


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26717.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.