驗證碼的實驗_第1頁
驗證碼的實驗_第2頁
驗證碼的實驗_第3頁
驗證碼的實驗_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、public class VerifyCode private String sRand = ; protected String sNode =AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz; protected String sFonts = new StringTimes New Roman,Comic Sans MS,華文行楷, MS Mincho,Gungsuh,Sylfaen,Arial, Franklin Gothic Medium,Century; public Color getRandColor(int fc, in

2、t bc) / 給定范圍獲得隨機顏色 Random random = new Random(); if (fc 255) fc = 255; if (bc 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); public BufferedImage creatImage() / 在內存中創(chuàng)建圖象 int width = 80, heigh

3、t = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); / 獲取圖形上下文 /Graphics g = image.getGraphics(); Graphics2D g = image.createGraphics(); AffineTransform tx =new AffineTransform(); /調整字體變形,可以產生效果,但不理想 /tx.scale(1.2, 0.9); g.transform(tx); / 生成隨機類 Random random =

4、 new Random(); / 設定背景色 g.setColor(getRandColor(200, 250); g.fillRect(0, 0, width, height); / 設定字體 g.setFont(new Font(this.getCurrentFonts(), Font.PLAIN, 18); / 畫邊框 / g.setColor(new Color(); / g.drawRect(0,0,width-1,height-1); / 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到 g.setColor(getRandColor(160, 200); for (i

5、nt i = 0; i 155; i+) int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); / 取隨機產生的認證碼(4位數字) for (int i = 0; i =50) ii=50; else rand = String.valueOf(random.nextInt(10); sRand += rand; / 將認證碼顯示到圖象中 g

6、.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110); /調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成 g.drawString(rand, 13 * i + 6, 16); / 圖象生效 g.dispose(); return image; public String getSRand() return sRand; public void setSRand(String rand) sRand = rand; /* * 隨機產生是數

7、字還是字符 */ public boolean getRandomBoolResult() int i =(int) (Math.round(Math.random() * 4) ); if(i%2=0) return true; return false; 二使用方法public class VerifyCodeAction extends Action Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletRes

8、ponse response) throws Excep tion VerifyCode verify = new VerifyCode(); /獲得一個實例 response.setHeader(Pragma, No-cache); /設置頭屬性 response.setHeader(Cache-Control, no-cache); response.setDateHeader(Expires, 0); BufferedImage buffImage = verify.creatImage(); /得到驗證碼圖片 HttpSession session = request.getSession(true); session.setAttribute

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論