欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java動態(tài)驗證碼單線設(shè)計的兩種方法

 更新時間:2018年07月23日 09:46:37   作者:垂眸愛笑約  
這篇文章主要介紹了Java動態(tài)驗證碼單線設(shè)計的兩種方法,需要的朋友可以參考下

1.java的動態(tài)驗證碼我這里將介紹兩種方法:

一:根據(jù)java本身提供的一種驗證碼的寫法,這種呢只限于大家了解就可以了,因為java自帶的模式編寫的在實際開發(fā)中是沒有意義的,所以只供學(xué)習(xí)一下就可以了,待會講解的第二種呢就是我們需要掌握的一種模式了:

第一種的代碼如下:

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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Servlet implementation class inde
 */
@WebServlet("/inde")
public class inde extends HttpServlet {
  private static final long serialVersionUID = 1L;
  /**
   * @see HttpServlet#HttpServlet()
   */
  public inde() {
    super();
    // TODO Auto-generated constructor stub
  }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      this.doPost(request, response);      
  }
  /**動態(tài)生成圖片驗證碼
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //創(chuàng)建圖像
    int width=100;
    int height=40;
    //圖片的大小設(shè)置
    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    //創(chuàng)建畫板    
    Graphics g=image.getGraphics();  
     setSquareBackGround(g,width,height,5);
    //確定畫筆顏色
    g.setColor(Color.black);
    //填充矩形
    g.fillRect(0, 0, width, height);
      //在大矩形中放小矩形
      g.setColor(Color.WHITE);
      g.fillRect(1, 1, width-2, height-2);        
    //填充字符
    String str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789";
    StringBuffer sb=new StringBuffer();
     //畫隨機(jī)干擾框
    setSquareBackGround(g,width,height,3); 
    //畫干擾點
    CreateRandomPoint(width, height,100,g,100);    
    //隨機(jī)畫幾條線
    CreateRandomLine(width, height,8,g,100);  
    //隨機(jī)獲取4個字符
    Random random=new Random();
    for (int i = 0; i < 4; i++) {
      //62個填充字符里面隨機(jī)的隨機(jī)的收取字符
      int index=random.nextInt(62);
      //截取一個字符
      String st=str.substring(index, index+1);
      //把字符放到圖片中去
      g.setColor(Color.red);
      //設(shè)置字體
      g.setFont(new Font("宋體",Font.BOLD,30));
      g.drawString(st, 20*i, 30);//防止4個字符在一起
      sb.append(st);
    }
    //把StringBuffer中的驗證碼放到session里面,目的是讓Login調(diào)用
    HttpSession se=request.getSession();
    se.setAttribute("number", sb.toString());
    //發(fā)送圖片到瀏覽器 指定發(fā)送的圖片 和格式
    response.setContentType("image/jpeg");
    //圖片,圖片的格式,輸出的方式
    ImageIO.write(image, "jpg", response.getOutputStream());
  }
  Random rand = new Random();
  private void CreateRandomPoint(int width,int height,int many,Graphics g,int alpha)
  { // 隨機(jī)產(chǎn)生干擾點
    for (int i=0;i<many;i++) {
      int x = rand.nextInt(width); 
      int y = rand.nextInt(height); 
      g.setColor(getColor(alpha));
      g.drawOval(x,y,rand.nextInt(3),rand.nextInt(3)); 
    } 
  }
/**
 * 隨機(jī)產(chǎn)生干擾線條
 * @param width
 * @param height
 * @param minMany 最少產(chǎn)生的數(shù)量
 * @param g
 * @param alpha 透明度0~255 0表示全透
 */
  private void CreateRandomLine(int width,int height,int minMany,Graphics g,int alpha)
  { // 隨機(jī)產(chǎn)生干擾線條
    for (int i=0;i<getIntRandom(minMany, minMany+6);i++) { 
      int x1 =getIntRandom(0,(int)(width*0.6)); 
      int y1 =getIntRandom(0,(int)(height*0.6)); 
      int x2 =getIntRandom((int)(width*0.4),width); 
      int y2 =getIntRandom((int)(height*0.2),height); 
      g.setColor(getColor(alpha));
      g.drawLine(x1, y1, x2, y2);
    } 
  }
  /**
   * 由隨機(jī)產(chǎn)生的方塊來作為干擾背景
   */
  private void setSquareBackGround(Graphics g,int width,int height,int count){
    // 隨機(jī)產(chǎn)生干擾線條
    for (int i=0;i<getIntRandom(count, count+2);i++) { 
      int x1 =getIntRandom(0,(int)(width*0.3)); 
      int y1 =getIntRandom(0,(int)(height*0.3)); 
      int x2 =getIntRandom((int)(width*0.5),width); 
      int y2 =getIntRandom((int)(height*0.55),height); 
      g.setColor(getColor(100));
      int w=x2-x1;
      int h=y2-y1;
      if(w<0) w=-w;
      if(h<0) h=-h;
      g.drawRect(x1, y1, w, h);
      g.setColor(getColor(25));
      g.fillRect(x1, y1, w, h);
    } 
  }
  private int getIntRandom(double start,double end)
  {  if(end<start)
    {
     double t=end;
     end=start;
     start=t;
    }
    double i=start+(int) (Math.random()*(end-start));
    return (int)i;
  }

上面的代碼呢寫的很詳細(xì)了,這里我就不重復(fù)了,這里的干擾線條是有很多寫法的,我這里就沒有全寫出來,有需要的話可以私聊我哦!

下面介紹第二種:

這種呢是我們開發(fā)中是可以用得到的,使用在的是網(wǎng)頁端的交互,我們在登錄網(wǎng)站的時候有很多的驗證碼就可以用這個來寫了

import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * Servlet implementation class inde
 */
@WebServlet("/inde")
public class inde extends HttpServlet {
  private static final long serialVersionUID = 1L;
  /**
   * @see HttpServlet#HttpServlet()
   */
  public inde() {
    super();
    // TODO Auto-generated constructor stub
  }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      this.doPost(request, response);      
  }
  /**動態(tài)生成圖片驗證碼
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //創(chuàng)建圖像
    int width=100;
    int height=40;
    //圖片的大小設(shè)置
    BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
    //創(chuàng)建畫板    
    Graphics g=image.getGraphics();  
     setSquareBackGround(g,width,height,5);
    //確定畫筆顏色
    g.setColor(Color.black);
    //填充矩形
    g.fillRect(0, 0, width, height);
      //在大矩形中放小矩形
      g.setColor(Color.WHITE);
      g.fillRect(1, 1, width-2, height-2);        
    //填充字符
    String str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789";
    StringBuffer sb=new StringBuffer();
//隨機(jī)獲取4個字符
    Random random=new Random();
    for (int i = 0; i < 4; i++) {
      //62個填充字符里面隨機(jī)的隨機(jī)的收取字符
      int index=random.nextInt(62);
      //截取一個字符
      String st=str.substring(index, index+1);
      //把字符放到圖片中去
      g.setColor(Color.red);
      //設(shè)置字體
      g.setFont(new Font("宋體",Font.BOLD,30));
      g.drawString(st, 20*i, 30);//防止4個字符在一起
      sb.append(st);
    }
//把StringBuffer中的驗證碼放到session里面,目的是讓Login調(diào)用
    HttpSession se=request.getSession();
    se.setAttribute("number", sb.toString());
    //發(fā)送圖片到瀏覽器 指定發(fā)送的圖片 和格式
    response.setContentType("image/jpeg");
    //圖片,圖片的格式,輸出的方式
    ImageIO.write(image, "jpg", response.getOutputStream());
  }

這樣就可以運(yùn)行了,當(dāng)然了我們可以在這里面加入第一種的干擾模塊給拿過來這樣就可以實現(xiàn)整體的一個效果了。

總結(jié)

以上所述是小編給大家介紹的Java動態(tài)驗證碼單線設(shè)計的兩種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 【Java IO流】字節(jié)流和字符流的實例講解

    【Java IO流】字節(jié)流和字符流的實例講解

    下面小編就為大家?guī)硪黄綣ava IO流】字節(jié)流和字符流的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java?Ribbon與openfeign區(qū)別和用法講解

    Java?Ribbon與openfeign區(qū)別和用法講解

    Ribbon是基于Netflix?Ribbon實現(xiàn)的一套客戶端負(fù)載均衡的工具,主要功能是提供客戶端的軟件負(fù)載均衡算法和服務(wù)調(diào)用。openfeign對Feign進(jìn)行了增強(qiáng),使其支持Spring MVC注解,另外還整合了Ribbon和Nacos,從而使得Feign的使用更加方便
    2022-08-08
  • Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)之線索化二叉樹的實現(xiàn)

    在二叉樹的結(jié)點上加上線索的二叉樹稱為線索二叉樹,對二叉樹以某種遍歷方式進(jìn)行遍歷,使其變?yōu)榫€索二叉樹的過程稱為對二叉樹進(jìn)行線索化。本文將詳解如何實現(xiàn)線索化二叉樹,需要的可以參考一下
    2022-05-05
  • java通過客戶端訪問服務(wù)器webservice的方法

    java通過客戶端訪問服務(wù)器webservice的方法

    這篇文章主要介紹了java通過客戶端訪問服務(wù)器webservice的方法,涉及java創(chuàng)建與調(diào)用webservice的相關(guān)技巧,需要的朋友可以參考下
    2016-08-08
  • mybatisplus報Invalid bound statement (not found)錯誤的解決方法

    mybatisplus報Invalid bound statement (not found)錯誤的解決方法

    搭建項目時使用了mybatisplus,項目能夠正常啟動,但在調(diào)用mapper方法查詢數(shù)據(jù)庫時報Invalid bound statement (not found)錯誤。本文給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧
    2020-08-08
  • 在Spring Boot中集成RabbitMQ詳細(xì)步驟(最新推薦)

    在Spring Boot中集成RabbitMQ詳細(xì)步驟(最新推薦)

    本文將介紹如何在Spring Boot項目中集成RabbitMQ,實現(xiàn)生產(chǎn)者和消費(fèi)者的基本配置,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • 詳解Java 網(wǎng)絡(luò)IO編程總結(jié)(BIO、NIO、AIO均含完整實例代碼)

    詳解Java 網(wǎng)絡(luò)IO編程總結(jié)(BIO、NIO、AIO均含完整實例代碼)

    本篇文章主要介紹了Java 網(wǎng)絡(luò)IO編程總結(jié)(BIO、NIO、AIO均含完整實例代碼),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • MyBatis傳入List集合查詢數(shù)據(jù)問題

    MyBatis傳入List集合查詢數(shù)據(jù)問題

    這篇文章主要介紹了MyBatis傳入List集合查詢數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • fastjson 使用方法詳細(xì)介紹

    fastjson 使用方法詳細(xì)介紹

    Fastjson是一個Java語言編寫的JSON處理器,由阿里巴巴公司開發(fā)。接下來通過本文給大家分享fastjson 使用方法詳細(xì)介紹,感興趣的朋友一起看看吧
    2017-11-11
  • Java Synchronized鎖升級原理及過程剖析

    Java Synchronized鎖升級原理及過程剖析

    這篇文章主要為大家詳細(xì)介紹一下Java中Synchronized鎖升級原理及過程,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)
    2022-08-08

最新評論