hgc-ghc
          Good Good Study!!! Day Day Up!!!
          posts - 40,comments - 3,trackbacks - 0
          <%@ page language="java" contentType="image/jpegRoomAction.javaRoomAction.javaRoomAction.java; charset=utf-8" pageEncoding="utf-8"%>
          <%@page import="java.io.*,java.util.*,com.sun.image.codec.jpeg.*,java.awt.*,java.awt.image.*"%>
          <%
              String s = "";
              int intCount = 0;
              /**
               * 驗證碼寬度
               */
              int width = 100;
              /**
               * 驗證碼高度
               */
              int height = 25;
              /**
               * 驗證碼字符集
               */
              char[] code = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                      'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
                      'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
                      'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't',
                      'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7',
                      '8', '9' };
              /**
               *  創建一個隨機數生成器類
               */
              Random random = new Random();
              // 隨機產生codeCount數字的驗證碼。   
              for (int i = 0; i < 4; i++) {
                  // 得到隨機產生的驗證碼數字。   
                  String strRand = String.valueOf(code[random
                          .nextInt(code.length)]);
                  // 將產生的四個隨機數組合在一起。   
                  s = s + strRand;
              }
              // 保存入session,用于與用戶的輸入進行比較.
              // 注意比較完之后清除session.
              session.setAttribute("rand", s);
              response.setContentType("image/gif");
              // 定義圖像buffer   
              BufferedImage image = new BufferedImage(width, height,
                      BufferedImage.TYPE_INT_RGB);
              Graphics2D gra = image.createGraphics();
              // 將圖像填充為白色   
              gra.setColor(Color.WHITE);
              gra.fillRect(0, 0, width, height);
              // 創建字體,字體的大小應該根據圖片的高度來定。  
              //字體對象構造方法public Font(String familyName,int style,int size)
              // familyName字體名;字體名可以分成兩大類:中文字體:宋體、楷體、黑體等;英文字體:Arial、Times New Roman等等;
              // style風格。PLAIN普通字體,BOLD(加粗),ITALIC(斜體),Font.BOLD+ Font.ITALIC(粗斜體)
              //size 大小
              Font font = new Font("宋體", Font.BOLD + Font.ITALIC, height - 1);//
              // 設置字體。   
              gra.setFont(font);
              // 畫邊框。   
              gra.setColor(getColor());
              gra.drawRect(0, 0, width - 1, height - 1);
              // 隨機產生干擾線,使圖象中的認證碼不易被其它程序探測到。   
              gra.setColor(Color.BLACK);
              for (int i = 0; i < 50; i++) {
                  int x = random.nextInt(width);
                  int y = random.nextInt(height);
                  int xl = random.nextInt(5);
                  int yl = random.nextInt(5);
                  gra.setColor(getColor());
                  gra.drawLine(x, y, x + xl, y + yl);
              }
              // 輸出數字
              char c;
              for (int i = 0; i < 4; i++) {
                  c = s.charAt(i);
                  gra.drawString(c + "", i * 25 + 4, 20); // 25為寬度,11為上下高度位置
              }
              OutputStream toClient = response.getOutputStream();
              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(toClient);
              encoder.encode(image);
              toClient.close();
              out.clear();
              out = pageContext.pushBody();
          %>
          <%!private Color getColor() {
                  Random random = new Random();
                  int red = 0, green = 0, blue = 0;
                  // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。   
                  red = random.nextInt(255);
                  green = random.nextInt(255);
                  blue = random.nextInt(255);
                  return new Color(red, green, blue);
              }%>
          posted on 2012-08-29 11:58 宏才 閱讀(136) 評論(0)  編輯  收藏 所屬分類: jsp
          主站蜘蛛池模板: 东丰县| 漯河市| 宁波市| 新兴县| 永平县| 海城市| 上饶县| 丹凤县| 辉南县| 雷波县| 图木舒克市| 定州市| 莒南县| 浠水县| 新乡市| 中牟县| 宜都市| 松溪县| 利辛县| 襄汾县| 陵川县| 祁连县| 尼勒克县| 屯门区| 兰州市| 双桥区| 突泉县| 昌吉市| 内黄县| 汉沽区| 芦溪县| 西安市| 阳城县| 潮州市| 阜新| 斗六市| 太原市| 双鸭山市| 三台县| 巴塘县| 田林县|