大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學(xué)習(xí)攻略 Java學(xué)習(xí) Java生成驗(yàn)證碼圖片的代碼

Java生成驗(yàn)證碼圖片的代碼

更新時(shí)間:2022-06-16 11:07:57 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1730次

動(dòng)力節(jié)點(diǎn)小編來告訴大家最簡單的生成java的圖片驗(yàn)證碼的方法,供大家參考。

現(xiàn)在所有行業(yè)都在考慮機(jī)器注冊來定制他們的系統(tǒng),最有效的方法是輸入驗(yàn)證。有很多方法可以驗(yàn)證:

1.問題驗(yàn)證,也就是圖片驗(yàn)證,在圖片上生成問題,然后在輸入框中輸入答案。

2.圖片驗(yàn)證,輸入圖片上顯示的文字信息。

3.短信驗(yàn)證,比較復(fù)雜,用戶不喜歡。

4.還有百度最新的驗(yàn)證方式。在圖像上生成文本,然后出現(xiàn)一個(gè)文本單擊框,選擇您在驗(yàn)證圖像上看到的文本。

現(xiàn)在分享一段java生成驗(yàn)證碼的代碼,這是一段基本的代碼。可以直接在學(xué)習(xí)中使用,如果需要更復(fù)雜的驗(yàn)證,可以添加自己的邏輯驗(yàn)證。

@Controller
public class ImgVerifyCode extends HttpServlet { 
  /**
   *
   */
  private static final long serialVersionUID = 1L; 
  /**
   *  The width of the captcha image. 
   */
  private int width = 70;
  /**
   *  Captcha image height. 
   */
  private int height =30; 
  /**
   *  Number of verification code characters 
   */
  private int codeCount = 5; 
  /**
   * xx
   */
  private int xx = 0;
  /**
   *  The font height 
   */
  private int fontHeight; 
  /**
   * codeY
   */
  private int codeY; 
  /**
   * codeSequence
   */
   String[] codeSequence = { "1" , "2" , "3" , "4" , "5" ,"6","7","8","9","A","a","B","b","c","C"
       ,"D","d","E","e","F","f","G","g","z","X","Q","v"}; 
  /**
   *  Initializes the validation image properties 
   */
  public void init() throws ServletException {
    //  from web.xml Get the initial information in 
    //  The width of the 
    String strWidth =width+"";
    //  highly 
    String strHeight = height+"";
    //  The number of characters 
    String strCodeCount = codeCount+"";
    //  Converts configuration information to numeric values 
    try {
      if (strWidth != null && strWidth.length() != 0) {
        width = Integer.parseInt(strWidth);
      }
      if (strHeight != null && strHeight.length() != 0) {
        height = Integer.parseInt(strHeight);
      }
      if (strCodeCount != null && strCodeCount.length() != 0) {
        codeCount = Integer.parseInt(strCodeCount);
      }
    } catch (NumberFormatException e) {
      e.printStackTrace();
    } 
    xx = width / (codeCount + 2); // Generates the horizontal distance of the random number 
    fontHeight = height - 12;   // The height of a number to generate a random number 
    codeY = height - 8;      // Generate the vertical distance of the random number  
  } 
  protected String images(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException,IOException {
    init();
    //  Define the image buffer
    BufferedImage buffImg = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_RGB);
    Graphics2D gd = buffImg.createGraphics(); 
    //  create 1 Random number generator class 
    Random random = new Random(); 
    //  Fill the image with white 
    gd.setColor(Color.WHITE);
    gd.fillRect(0, 0, width, height); 
    //  Create a font. The size of the font should depend on the height of the image. 
    Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
    //  Set the font. 
    gd.setFont(font); 
    //  Picture frame. 
    gd.setColor(Color.BLACK);
    gd.drawRect(0, 0, width - 1, height - 1); 
    //  Randomly generated 4 Interference line, so that the authentication code in the image is not easily detected by other programs. 
    gd.setColor(Color.BLACK);
    for (int i = 0; i < 4; i++) {
      int x = random.nextInt(width);
      int y = random.nextInt(height);
      int xl = random.nextInt(12);
      int yl = random.nextInt(12);
      gd.drawLine(x, y, x + xl, y + yl);
    } 
    // randomCode Used to save the randomly generated captcha for user authentication after login. 
    StringBuffer randomCode = new StringBuffer();
    int red = 0, green = 0, blue = 0; 
    //  Randomly generated codeCount Number verification code. 
    for (int i = 0; i < codeCount; i++) {
      //  Get the randomly generated captcha number. 
      String strRand = String.valueOf(codeSequence[random.nextInt(27)]);
      //  Generate a random color component to construct a color value, so that the color value of each number output will be different. 
      red = random.nextInt(125);
      green = random.nextInt(255);
      blue = random.nextInt(200); 
      //  Draws the captcha into the image with randomly generated colors. 
      gd.setColor(new Color(red, green, blue));
      gd.drawString(strRand, (i + 1) * xx, codeY); 
      //  Will produce the 4 I'm going to have a bunch of random Numbers 1 Up. 
      randomCode.append(strRand);
    }
    //  will 4 The numeric captcha is saved to Session In the. 
    HttpSession session = req.getSession();
    session.setAttribute("validateCode", randomCode.toString()); 
    //  Disable image caching. 
    resp.setHeader("Pragma", "no-cache");
    resp.setHeader("Cache-Control", "no-cache");
    resp.setDateHeader("Expires", 0); 
    resp.setContentType("image/jpeg"); 
    //  Output the image to Servlet In the output stream. 
    ServletOutputStream sos = resp.getOutputStream();
    ImageIO.write(buffImg, "jpeg", sos);
    sos.close();
    return null;
  }
   } 

此代碼是生成驗(yàn)證圖像的基本方法。

提交申請后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 成人合集大片bd高清在线观看 | 麻豆国产精品免费视频 | 人人狠狠综合久久亚洲婷婷 | 国产欧美精品国产国产专区 | 国产精品一级片 | 五月天婷婷缴情五月免费观看 | 欧美一区二区三区在线 | 九九综合九九综合 | 久久一日本道色综合久久m 久久一色本道亚洲 | 九九九精品在线观看 | 国产高清免费午夜在线视频 | 性色生活免费看性大片 | 久草精品在线播放 | 好吊妞视频一区二区 | 夜福利视频 | 亚洲精品影视 | 国产精品久久久久久久久ktv | 日本三级带日本三级带黄首页 | 国产精品欧美在线观看 | 麻豆精品国产免费观看 | 日韩中文字幕高清在线专区 | 在线你懂得 | 毛片网站在线 | 综合亚洲精品一区二区三区 | 日韩免费高清 | 国产成人在线播放视频 | 日韩精品一区二区三区免费观看 | 99热精品成人免费观看 | 国产精品综合一区二区三区 | 国内外成人在线视频 | 最新四虎4hu影库地址在线 | 免费看欧美日韩一区二区三区 | 在线观看国产亚洲 | 99视频在线看观免费 | 奇米视频在线观看 | 四虎影院在线免费播放 | 亚洲综合涩 | 性视频一区二区三区免费 | 天天色天天草 | 亚洲国产网| 国产社区 |