java中怎么實現(xiàn)生成圖片驗證碼功能

本篇文章為大家展示了java中怎么實現(xiàn)生成圖片驗證碼功能,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司技術(shù)團隊十多年來致力于為客戶提供做網(wǎng)站、網(wǎng)站建設(shè)、高端網(wǎng)站設(shè)計、全網(wǎng)營銷推廣、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過多年發(fā)展,公司擁有經(jīng)驗豐富的技術(shù)團隊,先后服務(wù)、推廣了上千多家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機構(gòu)單位。

Java類

public class RandomValidateCode {  public static final String RANDOMCODEKEY = "randomcode_key";//放到session中的key  private Random random = new Random();  private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//隨機產(chǎn)生的字符串  private int width = 80;//圖片寬  private int height = 26;//圖片高  private int lineSize = 40;//干擾線數(shù)量  private int stringNum = 4;//隨機產(chǎn)生字符數(shù)量  /**   * 生成隨機圖片   */  public void getRandcode(HttpServletRequest request,              HttpServletResponse response) {    HttpSession session = request.getSession();    //BufferedImage類是具有緩沖區(qū)的Image類,Image類是用于描述圖像信息的類    BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);    //產(chǎn)生Image對象的Graphics對象,改對象可以在圖像上進行各種繪制操作    Graphics g = image.getGraphics();    g.fillRect(0, 0, width, height);    g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));    g.setColor(getRandColor(160, 200));    //繪制干擾線    for(int i=0;i<=lineSize;i++){      drowLine(g);    }    //繪制隨機字符    String randomString = "";    for(int i=1;i<=stringNum;i++){      randomString=drowString(g,randomString,i);    }    session.removeAttribute(RANDOMCODEKEY);    session.setAttribute(RANDOMCODEKEY, randomString);    g.dispose();    try {      //將內(nèi)存中的圖片通過流動形式輸出到客戶端      ImageIO.write(image, "JPEG", response.getOutputStream());    } catch (Exception e) {      e.printStackTrace();    }  }  /*   * 獲得字體   */  private Font getFont(){    return new Font("Fixedsys",Font.CENTER_BASELINE,18);  }  /*   * 獲得顏色   */  private Color getRandColor(int fc,int bc){    if(fc > 255)      fc = 255;    if(bc > 255)      bc = 255;    int r = fc + random.nextInt(bc-fc-16);    int g = fc + random.nextInt(bc-fc-14);    int b = fc + random.nextInt(bc-fc-18);    return new Color(r,g,b);  }  /*   * 繪制字符串   */  private String drowString(Graphics g,String randomString,int i){    g.setFont(getFont());    g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));    String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));    randomString +=rand;    g.translate(random.nextInt(3), random.nextInt(3));    g.drawString(rand, 13*i, 16);    return randomString;  }  /*   * 繪制干擾線   */  private void drowLine(Graphics g){    int x = random.nextInt(width);    int y = random.nextInt(height);    int xl = random.nextInt(13);    int yl = random.nextInt(15);    g.drawLine(x, y, x+xl, y+yl);  }  /*   * 獲取隨機的字符   */  public String getRandomString(int num){    return String.valueOf(randString.charAt(num));  }}

Controller

@RequestMapping(value="/checkCode")  public void checkCode(HttpServletRequest request, HttpServletResponse response)      throws ServletException, IOException {    //設(shè)置相應(yīng)類型,告訴瀏覽器輸出的內(nèi)容為圖片    response.setContentType("image/jpeg");    //設(shè)置響應(yīng)頭信息,告訴瀏覽器不要緩存此內(nèi)容    response.setHeader("pragma", "no-cache");    response.setHeader("Cache-Control", "no-cache");    response.setDateHeader("Expire", 0);    RandomValidateCode randomValidateCode = new RandomValidateCode();    try {      randomValidateCode.getRandcode(request, response);//輸出圖片方法    } catch (Exception e) {      e.printStackTrace();    }  }

前端jsp頁面表單,顯示驗證碼

<p class="layui-form-item">          <p class="layui-row">            <p class="layui-col-xs7">              <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label>              <input type="text" name="code" id="LAY-user-login-vercode" lay-verify="required" placeholder="圖形驗證碼" class="layui-input">            </p>            <p class="layui-col-xs5">              <p >                <img src="${pageContext.request.contextPath}checkCode" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode" onclick="this.src=this.src+'?'" alt="">              </p>            </p>          </p>        </p>

上述內(nèi)容就是java中怎么實現(xiàn)生成圖片驗證碼功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:java中怎么實現(xiàn)生成圖片驗證碼功能
當前網(wǎng)址:http://bm7419.com/article8/goshop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機服務(wù)器托管、網(wǎng)站排名、用戶體驗、網(wǎng)站建設(shè)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)