athrunwang

          紀(jì)元
          數(shù)據(jù)加載中……
          Java驗(yàn)證碼

          Java生成驗(yàn)證碼圖片 
          1.Servlet生成驗(yàn)證碼圖片

            1 package com.logcd.servlet;
          2
          3 import java.awt.Color;
          4 import java.awt.Font;
          5 import java.awt.Graphics2D;
          6 import java.awt.image.BufferedImage;
          7 import java.util.Random;
          8 import javax.imageio.ImageIO;
          9 import javax.servlet.*;
          10 import java.io.*;
          11 import javax.servlet.http.*;
          12 import javax.servlet.ServletException;
          13 import javax.servlet.http.HttpServlet;
          14 import javax.servlet.http.HttpServletRequest;
          15 import javax.servlet.http.HttpServletResponse;
          16
          17 @SuppressWarnings("serial")
          18 public class RandomCode extends HttpServlet {
          19
          20 public void doGet(HttpServletRequest request, HttpServletResponse response)
          21 throws ServletException, IOException {
          22
          23 this.doPost(request, response);
          24 }
          25
          26 public void doPost(HttpServletRequest request, HttpServletResponse response)
          27 throws ServletException, IOException {
          28
          29 // 驗(yàn)證碼圖片的寬度。
          30 int width = 70;
          31 // 驗(yàn)證碼圖片的高度。
          32 int height = 30;
          33 BufferedImage buffImg = new BufferedImage(width, height,
          34 BufferedImage.TYPE_INT_RGB);
          35 Graphics2D g = buffImg.createGraphics();
          36
          37 // 創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類。
          38 Random random = new Random();
          39
          40 // 設(shè)定圖像背景色(因?yàn)槭亲霰尘埃云?
          41 g.setColor(getRandColor(200, 250));
          42 g.fillRect(0, 0, width, height);
          43 // 創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來定。
          44 Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);
          45 // 設(shè)置字體。
          46 g.setFont(font);
          47
          48 // 畫邊框。
          49 g.setColor(Color.BLACK);
          50 g.drawRect(0, 0, width - 1, height - 1);
          51 // 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到。
          52 //g.setColor(Color.GRAY);
          53 g.setColor(getRandColor(160,200));
          54 for (int i = 0; i < 155; i++) {
          55 int x = random.nextInt(width);
          56 int y = random.nextInt(height);
          57 int xl = random.nextInt(12);
          58 int yl = random.nextInt(12);
          59 g.drawLine(x, y, x + xl, y + yl);
          60 }
          61
          62 // randomCode用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。
          63 StringBuffer randomCode = new StringBuffer();
          64
          65 // 設(shè)置默認(rèn)生成4個(gè)驗(yàn)證碼
          66 int length = 4;
          67 // 設(shè)置備選驗(yàn)證碼:包括"a-z"和數(shù)字"0-9"
          68 String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
          69
          70 int size = base.length();
          71
          72 // 隨機(jī)產(chǎn)生4位數(shù)字的驗(yàn)證碼。
          73 for (int i = 0; i < length; i++) {
          74 // 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字。
          75 int start = random.nextInt(size);
          76 String strRand = base.substring(start, start + 1);
          77
          78 // 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。
          79 // 生成隨機(jī)顏色(因?yàn)槭亲銮熬埃云?
          80 //g.setColor(getRandColor(1, 100));
          81
          82 //調(diào)用函數(shù)出來的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
          83 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
          84
          85 g.drawString(strRand, 15 * i + 6, 24);
          86
          87 // 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。
          88 randomCode.append(strRand);
          89 }
          90 // 將四位數(shù)字的驗(yàn)證碼保存到Session中。
          91 HttpSession session = request.getSession();
          92 session.setAttribute("rand", randomCode.toString());
          93
          94 //圖象生效
          95 g.dispose();
          96
          97 // 禁止圖像緩存。
          98 response.setHeader("Pragma", "no-cache");
          99 response.setHeader("Cache-Control", "no-cache");
          100 response.setDateHeader("Expires", 0);
          101
          102 response.setContentType("image/jpeg");
          103
          104 // 將圖像輸出到Servlet輸出流中。
          105 ServletOutputStream sos = response.getOutputStream();
          106 ImageIO.write(buffImg, "jpeg", sos);
          107 sos.flush();
          108 sos.close();
          109
          110 }
          111
          112 Color getRandColor(int fc, int bc) {// 給定范圍獲得隨機(jī)顏色
          113 Random random = new Random();
          114 if (fc > 255)
          115 fc = 255;
          116 if (bc > 255)
          117 bc = 255;
          118 int r = fc + random.nextInt(bc - fc);
          119 int g = fc + random.nextInt(bc - fc);
          120 int b = fc + random.nextInt(bc - fc);
          121 return new Color(r, g, b);
          122 }
          123
          124 }

          2.配置

          1     <servlet>
          2 <servlet-name>RandomCode</servlet-name>
          3 <servlet-class>com.logcd.servlet.RandomCode</servlet-class>
          4 </servlet>
          5 <servlet-mapping>
          6 <servlet-name>RandomCode</servlet-name>
          7 <url-pattern>/randomCode</url-pattern>
          8 </servlet-mapping>

          3.調(diào)用

           1 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
          2 <meta http-equiv="pragma" content="no-cache"/>
          3 <meta http-equiv="cache-control" content="no-cache"/>
          4 <meta http-equiv="expires" content="0"/>
          5
          6 <iframe src="http://127.0.0.1/js_test/randomCode" id="codeFrame" name="codeFrame" frameborder="no" border="0" marginwidth="0"
          7 marginheight="0" scrolling="no" allowtransparency="yes" height="35" width="102"></iframe>
          8 <a href="javascript:void(0);" onclick="refreshCode();">看不清,換一張</a>
          9 <br>
          10 <span id="codeImg"><img border=0 src="randomCode"></span>
          11 <a href="javascript:void(0);" onclick="reloadCode()">看不清,再換一張</a>

           

           1 function $(id){
          2 return document.getElementById(id);
          3 }
          4
          5 /**刷新iframe**/
          6 function refreshCode(){
          7 window.frames["codeFrame"].location.reload();
          8 }
          9
          10 /**替換圖片**/
          11 function reloadCode(){
          12 $("codeImg").innerHTML = "<img border=0 src='randomCode'>";
          13 }

          posted on 2011-12-06 10:36 AthrunWang 閱讀(2772) 評(píng)論(3)  編輯  收藏

          評(píng)論

          # re: Java驗(yàn)證碼 2012-04-06 11:25 DFS

          SDF

          # re: Java驗(yàn)證碼 2013-08-08 14:43 e

          erw

          # re: Java驗(yàn)證碼 2014-03-18 21:46 最代碼

          我根據(jù)你的代碼整理分享了一份代碼:Servlet生成驗(yàn)證碼圖片
          下載地址:http://www.zuidaima.com/share/1724427578084352.htm

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 东城区| 成都市| 清新县| 神农架林区| 桦川县| 肃宁县| 齐河县| 镇沅| 吉林市| 兴国县| 万山特区| 泗洪县| 长泰县| 禹城市| 柳江县| 汶上县| 金塔县| 平乐县| 岳池县| 旬邑县| 资阳市| 龙川县| 金塔县| 鱼台县| 通城县| 武宁县| 安平县| 贞丰县| 会泽县| 湖口县| 思茅市| 芜湖县| 于都县| 泊头市| 肇庆市| 镇康县| 鄂伦春自治旗| 航空| 古丈县| 云浮市| 青川县|