Struts2(十五)---驗證碼

第一步:生成驗證碼圖片以及隨機產生四位字母

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CheckCode {
    // 獲取隨機顏色
        public static Color getRandColor(int s, int e) {
            Random random = new Random();
            if (s > 255) s = 255;
            if (e > 255) e = 255;
            int r = s + random.nextInt(e - s);      //隨機生成RGB顏色中的r值
            int g = s + random.nextInt(e - s);      //隨機生成RGB顏色中的g值
            int b = s + random.nextInt(e - s);      //隨機生成RGB顏色中的b值
            return new Color(r, g, b);
        }
        //生成隨機的四位字母
        public static String createSecurityCode() {
            String sRand = "";
            Random random = new Random();
            //輸入隨機的驗證文字
            for(int i = 0; i < 4; i++) {
                 char ctmp = (char)(random.nextInt(26) + 65);   //生成A~Z的字母
                 sRand+=ctmp;
            }
            return sRand;
        }

        public static BufferedImage createImage(String sRand) {
            int width = 116;            //指定驗證碼的寬度
            int height = 33;            //指定驗證碼的高度

            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();       //獲取Graphics類的對象(Java的繪圖類)
            Random random = new Random();               //實例化一個Random對象
            Font mFont = new Font("宋體", Font.BOLD, 22);   //通過Font構造字體
            g.fillRect(0, 0, width, height);                //繪製驗證碼背景
            g.setFont(mFont);           //設置字體
            g.setColor(getRandColor(180, 200));     //設置顏色
            // 畫隨機的線條
            for (int i = 0; i < 100; i++) {
                int x = random.nextInt(width - 1);
                int y = random.nextInt(height - 1);
                int x1 = random.nextInt(3) + 1;
                int y1 = random.nextInt(6) + 1;
                g.drawLine(x, y, x + x1, y + y1);       //繪製直線
            }
            /**************************畫一條折線********************************/       
            BasicStroke bs=new BasicStroke(2f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL); //創建一個供畫筆選擇線條粗細的對象
            Graphics2D g2d = (Graphics2D) g;    //通過Graphics類的對象創建一個Graphics2D類的對象
            g2d.setStroke(bs);                  //改變線條的粗細
            g.setColor(Color.GRAY);     //設置當前顏色爲預定義顏色中的灰色
            int lineNumber=4;       //指定端點的個數
            int[] xPoints=new int[lineNumber];      //定義保存x軸座標的數組
            int[] yPoints=new int[lineNumber];      //定義保存x軸座標的數組
            //通過循環爲x軸座標和y軸座標的數組賦值
            for(int j=0;j<lineNumber;j++){
                xPoints[j]=random.nextInt(width - 1);
                yPoints[j]=random.nextInt(height - 1);
            }
            g.drawPolyline(xPoints, yPoints,lineNumber);    //繪製折線
            /*******************************************************************/
             sRand =  createSecurityCode();
            // 輸出隨機的驗證文字
             for (int i = 0; i < sRand.length(); i++) {
               char ctmp = sRand.charAt(i);
                Color color = new Color(20 + random.nextInt(110), 20 + random
                        .nextInt(110), 20 + random.nextInt(110));
                g.setColor(color);              //設置顏色
                /** **隨機縮放文字並將文字旋轉指定角度* */
                // 將文字旋轉指定角度
                Graphics2D g2d_word = (Graphics2D) g;
                AffineTransform trans = new AffineTransform();
                trans.rotate(random.nextInt(45) * 3.14 / 180, 22 * i + 8, 7);
                // 縮放文字
                float scaleSize = random.nextFloat() +0.8f;
                if (scaleSize > 1f) scaleSize = 1f;
                trans.scale(scaleSize, scaleSize);          //進行縮放
                g2d_word.setTransform(trans);
                /** ********************* */
                g.drawString(String.valueOf(ctmp), width/6 * i+23, height/2);

            }
             g.dispose(); //關閉資源
            return image;
        }


}

第二步:驗證碼圖片與Struts2結合

private InputStream inputStream;
 public InputStream getInputStream() {
        return inputStream;
    }
public String checkCodeImage() throws IOException {
        //獲取驗證碼圖片的內容
        String sRand = CheckCode.createSecurityCode();
        //獲取驗證碼的圖片
        BufferedImage image = CheckCode.createImage(sRand);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpeg", outputStream);
        inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        session.put("checkCode", sRand);
        return "checkCodeImage";
    }
Struts.xml
<result name="checkCodeImage" type="stream">
          <param name="contentType">image/jpeg</param>
          <param name="inputStream">inputStream</param>
          <param name="bufferSize">2048</param>
       </result>
jsp頁面

                                    <!-- 顯示驗證碼 -->
<img src="../mem-checkCodeImage" name="checkCode" onClick="myReload()" width="116" height="43" class="img_checkcode"  id="img_checkCode" />

JS 驗證刷新驗證碼
  $(function(){

              //點擊圖片更換驗證碼
              $("#img_checkCode").click(function(){
                   $(this).attr("src","front/mem-checkCodeImage?time="+new Date().getTime());
               });

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