溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java如何生成隨機圖片驗證碼

發(fā)布時間:2021-09-26 16:57:07 來源:億速云 閱讀:196 作者:小新 欄目:編程語言

這篇文章主要介紹了java如何生成隨機圖片驗證碼,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前臺html代碼

<p >  <span>驗證碼:</span><input type="text" name="verifyCode" id="verifyCode" />  <img id="verifyCodeImg" alt="點擊更換" src="/qos/dog/getVerifyCodeImg"   title="點擊更換" onclick="change()"></p>

注釋:此處的src="/qos/dog/getVerifyCodeImg" SpringBoot頁面展示Thymeleaf的語法

前臺js代碼

function change() {  var verifyCode = document.getElementById("verifyCodeImg");  verifyCode.src = "/qos/dog/getVerifyCodeImg?time=" + Math.random(1000); } /*-*/ /qos/dog/ 這里的路徑是需要換成自己的哦

驗證代碼,在controller里面新建一個util文件夾,然后放入VerifyCodeUtil.java

package com.paladin.qos.util; import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Random; public class VerifyCodeUtil {  private static final Random random = new Random(); private static final String[] fontNames = {"宋體", "華文楷體", "黑體", "Georgia", "微軟雅黑", "楷體_GB2312"};  public static String drawImage(ByteArrayOutputStream output) {  String code = "";  int width = 50;  int height = 25;   //創(chuàng)建圖片緩沖區(qū)  BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);   Graphics2D g = bi.createGraphics();   //設(shè)置背景顏色  g.setBackground(new Color(255, 255, 255));  g.clearRect(0, 0, width, height);   StringBuilder stringBuilder = new StringBuilder();  //這里只畫入四個字符  for (int i = 0; i < 4; i++) {   String s = randomChar() + "";  //隨機生成字符,因為只有畫字符串的方法,沒有畫字符的方法,所以需要將字符變成字符串再畫   stringBuilder.append(s);   //添加到StringBuilder里面   float x = i * 1.0F * width / 4; //定義字符的x坐標   g.setFont(randomFont());   //設(shè)置字體,隨機   g.setColor(randomColor());   //設(shè)置顏色,隨機   g.drawString(s, x, height - 5);  }  code = stringBuilder.toString();//獲取驗證碼字符串   //定義干擾線  //定義干擾線的數(shù)量(3-5條)int num = random.nextInt(max)%(max-min+1) + min;  int num = random.nextInt(5) % 3 + 3;  Graphics2D graphics = (Graphics2D) bi.getGraphics();  for (int i = 0; i < num; i++) {   int x1 = random.nextInt(width);   int y1 = random.nextInt(height);   int x2 = random.nextInt(width);   int y2 = random.nextInt(height);   graphics.setColor(randomColor());   graphics.drawLine(x1, y1, x2, y2);  }  // 釋放圖形上下文  g.dispose();  try {   ImageIO.write(bi, "jpg", output);  } catch (IOException e) {   e.printStackTrace();  }  return code;//為了方便取值,直接返回code,    }  //隨機字體 private static Font randomFont() {  int index = random.nextInt(fontNames.length);  String fontName = fontNames[index];  int style = random.nextInt(4);   //隨機獲取4種字體的樣式  int size = random.nextInt(20) % 6 + 15; //隨機獲取字體的大小(10-20之間的值)  return new Font(fontName, style, size); }  //隨機顏色 private static Color randomColor() {  int r = random.nextInt(225);  int g = random.nextInt(225);  int b = random.nextInt(225);  return new Color(r, g, b); }   //隨機字符 private static char randomChar() {  //A-Z,a-z,0-9,可剔除一些難辨認的字母與數(shù)字  String str = "0123456789ABCdefghiDEFGHIJopPQRVWXYZabcjklSTUmnqrstKLMNOvuwxyz";   return str.charAt(random.nextInt(str.length())); } }

最后,在controller里面引用

@RequestMapping("/getVerifyCodeImg")@ResponseBodypublic void getVerifyCodeImg(HttpServletResponse response, HttpSession session) {  ByteArrayOutputStream output = new ByteArrayOutputStream();  String code = VerifyCodeUtil.drawImage(output);  //將驗證碼文本直接存放到session中  session.setAttribute("verifyCode", code);  try {   ServletOutputStream out = response.getOutputStream();   output.writeTo(out);  } catch (IOException e) {   e.printStackTrace();  } }

感謝你能夠認真閱讀完這篇文章,希望小編分享的“java如何生成隨機圖片驗證碼”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI