java驗證碼產生的基本流程_java

來源:互聯網
上載者:User

1.驗證碼的產生,我們能夠看到是用Graphics對象畫出來的。對象我們必須要獲得Graphics對象

1-1、Graphics對象的擷取,要通過BufferedImage獲得

<span style="font-size:18px;">int width=100;//確定框框的大小  int height=40;  BufferedImage bfi =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  Graphics g=bfi.getGraphics();//獲得Graphics對象就可以畫圖</span> 

1-2、一般的驗證碼背景框都是白色的

<span style="font-size:18px;"> //1,設定背景(白框框)  g.setColor(Color.WHITE);//白色的畫筆  g.fillRect(0, 0, width, height);//畫矩形矩形框框</span> 

1-3、儲存資料(後台驗證使用)和設定字型樣式(美觀)

String str="";//儲存資料  Random rom=new Random();  //設定字型的大寫與粗  g.setFont(new Font("a", Font.BOLD,20)); 

1-4、產生具體的數值,以及隨機產生的顏色

for(int i=0;i<4;i++){   int num=rom.nextInt(10);//產生的隨機數   g.setColor(new Color(rom.nextInt(256),rom.nextInt(256), rom.nextInt(256)));//設定畫筆的顏色(隨機)   g.drawString(""+num, 20*i, 20+rom.nextInt(10));//畫出線,x的位置每一之間增加20,y的座標以20一條線,線上上或者是線下   //PS:位置需要明確些,  } 

1-5、一般的數字容易被其他軟體直接識別出來,為了防黑。稍微加一點幹擾線

//畫出一些幹擾線  for (int i = 0; i < 10; i++) {   g.setColor(new Color(rom.nextInt(256),rom.nextInt(256), rom.nextInt(256)));//設定畫筆的顏色(隨機)   g.drawLine(rom.nextInt(100),rom.nextInt(40), rom.nextInt(100), rom.nextInt(40));//位置也是隨機,x,y的值不要超過矩形框框  } 

1-6、銷毀Graphics對象和儲存圖片

<span style="white-space:pre"> </span>g.dispose();//銷毀對象  ImageIO.write(bfi, "JPEG", res.getOutputStream());//圖片用位元組流 直接得到 PS::: res是Servlet裡面的。 

這樣驗證碼就產生了,那我們如何匯入到前台去呢

2、具體實現
前台代碼呈現():

<body>   <h1>使用者登入</h1><br/>   使用者名稱:<input type="text" name="nametext"/><br/>   密 碼:<input type="text" name="psd"/><br/>   請輸入驗證碼:<input type="text"/>   <img <span style="color:#ff0000;">src="/IMG/immg" </span>id="aid"/><a href="javascript:flush()" >看不清</a>  </body> 

src的地址來源就是從後台發過來的。路徑是很有意思的。
2-1步驟
項目裡面
myeclipse --> src -->new  Servlet 出現如下:

點擊----》next 出現如下頁面:

這個配置會自動到項目裡面的web-INF檔案夾裡面web.xml。這個框框裡面的值就是前台 src裡面寫的需要訪問的路徑,----> 點擊完成就行了。
自動了。產生如下介面:

在這裡就可寫之前的代碼。但是需要注意,我們必須通過覆蓋這個方法才能有效:

protected void service(HttpServletRequest req, HttpServletResponse resp)//自動產生 輸入 <span style="font-family: Arial, Helvetica, sans-serif;">service 補全,自動產生</span>    throws ServletException, IOException {  // TODO Auto-generated method stub  super.service(req, resp); } 

具體的代碼如下:

package cn.hncu.com.servlet;   import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random;  import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;  public class Imgdemo extends HttpServlet { @Override public void service(ServletRequest req, ServletResponse res)   throws ServletException, IOException {  int width=100;//確定框框的大小  int height=40;  BufferedImage bfi =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  Graphics g=bfi.getGraphics();//獲得Graphics對象就可以畫圖  //1,設定背景(白框框)  g.setColor(Color.WHITE);//白色的畫筆  g.fillRect(0, 0, width, height);//畫矩形矩形框框  //2,具體產生隨機數  String str="";//儲存資料  Random rom=new Random();  //設定字型的大寫與粗  g.setFont(new Font("a", Font.BOLD,20));  //畫出具體的圖片    for(int i=0;i<4;i++){   int num=rom.nextInt(10);//產生的隨機數   g.setColor(new Color(rom.nextInt(256),rom.nextInt(256), rom.nextInt(256)));//設定畫筆的顏色(隨機)   g.drawString(""+num, 20*i, 20+rom.nextInt(10));//畫出線,x的位置每一之間增加20,y的座標以20一條線,線上上或者是線下   //PS:位置需要明確些,  }  //畫出一些幹擾線  for (int i = 0; i < 10; i++) {   g.setColor(new Color(rom.nextInt(256),rom.nextInt(256), rom.nextInt(256)));//設定畫筆的顏色(隨機)   g.drawLine(rom.nextInt(100),rom.nextInt(40), rom.nextInt(100), rom.nextInt(40));//位置也是隨機,x,y的值不要超過矩形框框  }  g.dispose();  ImageIO.write(bfi, "JPEG", res.getOutputStream());//圖片用位元組流 直接得到 } }<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span> 

前台代碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>  <head>  <base href="<%=basePath%>">    <title>My JSP 'img.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0">   <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="This is my page">  <!--  <link rel="stylesheet" type="text/css" href="styles.css">  -->  <script type="text/javascript">      function flush(){   var text=document.getElementById("aid");   var date =new Date();   var tt=date.getTime();   text.src="/IMG/immg?"+tt;         }  </script>   </head>   <body>   <h1>使用者登入</h1><br/>   使用者名稱:<input type="text" name="nametext"/><br/>   密 碼:<input type="text" name="psd"/><br/>   請輸入驗證碼:<input type="text"/>   <img src="/IMG/immg" id="aid"/><a href="javascript:flush()" >看不清</a>  </body> </html> 

對於前台代碼需要解釋一下:
當我們的的驗證碼傳過來看不清的時候需要重新整理,而瀏覽器有自動記憶的功能,當沒有新的參數傳進來的時候,瀏覽器是不會重新整理的,所以我們需要手動的去寫一個js控制參數傳,我們知道,只有時間是不會變化的,所有我們採用時間來作為參數傳遞。
PS:自己坑了一段時間的問題:驗證碼的路徑問題。前端的“/”表示 tomcat目錄,在項目內部,如web.xml中“/”表示該項目下。也就是說,他們兩個的目錄差了一層。

最後附上自己在測試的時候的代碼以及修改數字形狀的問題,如改成2D的效果更不錯。都有很明顯的記錄。

package cn.hncu.com;  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.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random;  import javax.imageio.ImageIO;  import org.junit.Test;  public class Demoimg {  @Test  public void Test() throws Exception{   String str="9988";   int width=60;   int height=30;   //通過bufferedImage對象獲得Graphics對象   BufferedImage bfi=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);   Graphics g=bfi.getGraphics();   g.drawString(str, 10,10);   g.dispose();//類似於IO中的關流   ImageIO.write(bfi , "JPEG", new FileOutputStream("F:\\ex\\a.jpg"));   //bfi為畫布,將畫布寫到檔案中JPEG為指定檔案格式  }    @Test  public void Test2() throws Exception{   int width=100;   int height=40;   BufferedImage bfi =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   Graphics g=bfi.getGraphics();//獲得Graphics對象就可以畫圖   //1,設定背景(白框框)   g.setColor(Color.WHITE);//白色的畫筆   g.fillRect(0, 0, width, height);   //2,具體產生隨機數   String str="";//儲存資料   Random rom=new Random();   //設定字型的大寫與粗   g.setFont(new Font("a", Font.BOLD,20));   //畫出具體的圖片      for(int i=0;i<4;i++){    int num=rom.nextInt(10);//產生的隨機數    g.setColor(new Color(rom.nextInt(256),rom.nextInt(256), rom.nextInt(256)));//設定畫筆的顏色(隨機)    g.drawString(""+num, 20*i, 20+rom.nextInt(10));//畫出線,x的位置每一之間增加20,y的座標以20一條線,線上上或者是線下    //PS:位置需要明確些,   }   //畫出一些幹擾線   for (int i = 0; i < 10; i++) {    g.setColor(new Color(rom.nextInt(256),rom.nextInt(256), rom.nextInt(256)));//設定畫筆的顏色(隨機)    g.drawLine(rom.nextInt(100),rom.nextInt(40), rom.nextInt(100), rom.nextInt(40));//位置也是隨機,x,y的值不要超過矩形框框   }   g.dispose();   ImageIO.write(bfi, "JPEG", new FileOutputStream("F:\\ex\\b.jpg"));  }     //畫出可以變化的情況  //字型能夠旋轉的驗證碼  @Test  public void Test3() throws IOException{   int width=100;   int height=40;   BufferedImage bfi =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);   Graphics g=bfi.getGraphics();   Graphics2D g2d=(Graphics2D) g;   Random rom =new Random();   g2d.setColor(Color.WHITE);//設定畫筆的顏色   g2d.fillRect(0, 0, width, height);//畫出一個白色的矩形   g2d.setFont(new Font("a", Font.BOLD, 20));   for(int i=0;i<4;i++){    int num=rom.nextInt(10);    //旋轉,放縮    AffineTransform aff=new AffineTransform();    //aff.rotate(Math.random(), i*18, height-20);//旋轉    aff.scale(0.6+Math.random(), 0.6+Math.random());//縮放    g2d.setColor(new Color(rom.nextInt(256),rom.nextInt(256),rom.nextInt(256)));    g2d.setTransform(aff);    g2d.drawString(""+num, i*18, height-25);   }   g2d.dispose();   ImageIO.write(bfi, "JPEG", new FileOutputStream("F:\\ex\\c.jpg"));     } } 

以上就是關於java產生驗證碼的基本流程,還附帶了自己親自的測試的代碼,希望這些都能幫到大家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.