Servlet編程實現實現圖形驗證碼

 

//----------------------------------servlet文件----------------------------

package com.msit.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Code_MarkerServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doPost(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  // 首先設置頁面不緩存--最後加上
  response.setHeader("Pragma", "No-cache");  // 設置HTTP響應的頭信息
  response.setHeader("Cache-Control", "no-cache");
  response.setDateHeader("Expires", 0);

  // 定義圖片的寬度和高度
  int width = 90, height = 40;
  // 創建一個圖像對象
  BufferedImage image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
  /**
   * component類及其子類在新生成對象的時候會自動調用paint(Graphics g)方法
   */
  Graphics g = image.createGraphics();// 得到圖像的繪製環境
  
  // 用隨機顏色填充圖像背景
  g.setColor(getRandColor(180, 250));
  /**
   * fillRect(x,y,w,h)函數的作用是:填充一個矩形區域,x、y爲起始座標(即左上角座標),
   * 後面兩個參數分別爲:w、h,是矩形區域的寬和高,這裏的20表示填充寬度20像素,15表示填充高度15像素。
如有其它不懂的地方可以在線問我。
   */
  g.fillRect(0, 0, width, height);
  Random random = new Random();
  for (int i = 0; i < 6; i++) {
   g.setColor(getRandColor(50, 100));
   int x = random.nextInt(width);
   int y = random.nextInt(height);
   /**
    * fillRect(x,y,w,h)函數的作用是:填充一個矩形區域,x、y爲起始座標(即左上角座標),
    * 後面兩個參數分別爲:w、h,是矩形區域的寬和高,這裏的20表示填充寬度20像素,
    * 15表示填充高度15像素。
    */
   g.drawOval(x, y, 5, 5);
  }
  g.setFont(new Font("", Font.PLAIN, 40)); // 設置字體,下面準備畫隨機數
  String sRand = ""; // 隨機數字符串
  for (int i = 0; i < 4; i++) {
   // 生成四個數字字符
   String rand = String.valueOf(random.nextInt(10));
   sRand += rand;
   // 生成隨機顏色
   g.setColor(new Color(20 + random.nextInt(80), 20 + random
     .nextInt(100), 20 + random.nextInt(90)));
   // 將隨機數字畫在圖像上
   
   /**
    * drawString
public abstract void drawString(AttributedCharacterIterator iterator,
int x,
int y)
使用此圖形上下文的當前顏色繪製由指定迭代器給定的文本。迭代器必須爲每個字符指定字體。最左側字符的基線位於此圖形上下文座標系統的 (x, y) 位置處。

參數:
iterator - 要繪製其文本的迭代器
x - x 座標。
y - y 座標。
另請參見:
drawBytes(byte[], int, int, int, int), drawChars(char[], int, int, int, int)
    *
    */
   g.drawString(rand, (17 + random.nextInt(3)) * i + 8, 34);

   // 生成干擾線
   for (int k = 0; k < 12; k++) {
    int x = random.nextInt(width);
    int y = random.nextInt(height);
    int xl = random.nextInt(9);
    int yl = random.nextInt(9);
    /**
     * 這個方法是畫一條直線,那麼我們都知道兩點確定一條直線,而在座標中由橫(X)、縱座標(y)確定一個點,這四參數實際就是確定兩個點,你要畫的直線的起始點橫縱座標和終點的橫縱座標。
X1,Y1是確定直線的起始點,即橫座標爲x1,縱座標爲y1的點。同理x2,y2確定直線的終點。
例A(x1,y1)  B(x2,y2) 就可以畫出直線AB了
     *
     *
     *
     * public abstract void drawLine(int x1,
                              int y1,
                              int x2,
                              int y2)在此圖形上下文的座標系統中,使用當前顏色在點 (x1, y1) 和 (x2, y2) 之間畫一條線。

參數:
x1 - 第一個點的 x 座標。
y1 - 第一個點的 y 座標。
x2 - 第二個點的 x 座標。
y2 - 第二個點的 y 座標。

譬如,你從(0,0, 100, 200),就是從畫一條從(0,0)點開始向目標點(100,200)的直線
     *
     *
     */
    g.drawLine(x, y, x + xl, y + yl);
   }
  }

  // 將生成的隨機數字字符串寫入session
  request.getSession().setAttribute("randomNumber", sRand);
  g.dispose(); // 使圖像生效
  ImageIO.write(image, "JPEG", response.getOutputStream()); // 輸出圖像到頁面
 }

 // 產生一個隨機的顏色 其中,min:顏色分量最小值 ;max:顏色分量最大值
 public Color getRandColor(int min, int max) {
  Random random = new Random();
  if (min > 255) {
   min = 255;
  }
  if (max > 255) {
   max = 255;
  }
  int R = min + random.nextInt(max - min);
  int G = min + random.nextInt(max - min);
  int B = min + random.nextInt(max - min);
  /**
   * 0~255   蘭色:0~7 位       綠色:8~15位        紅色:16~23位
   */
  return new Color(R, G, B);
 }

}

 

----------------------------------------jsp頁面代碼--------------------------------------------

<body>
  <form name="form1" method="post" action=""> 
     <IMG id="img1" src="<%=contextPath%>/Code_MarkerServlet"height=40 width=90>&nbsp;
     <input type="button" name="Submit" value="換一個" οnclick="aa()">  
  </form>
  <script type="text/javascript">
  function aa(){
       document.getElementById("img1").src="<%=contextPath%>/Code_MarkerServlet";
       form1.submit();
  }
  </script>
 </body>

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章