日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

生成二维码接口,前端调用接口将二维码显示在页面上

發布時間:2024/4/17 HTML 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 生成二维码接口,前端调用接口将二维码显示在页面上 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要是生成數字和字母4位的二維碼

我們會把生成的二維碼放入到緩存中,有過期時間。當用二維碼進行驗證時需要根據key值拿到二維碼的值,如果頁面傳過來的驗證碼和緩存中的一樣,則說明驗證碼輸入正確。

@RequestMapping(value = "/getImgCode")
public void getImgCode(HttpServletRequest request, HttpServletResponse response, @RequestParam(defaultValue = "") String telephone) throws Exception {

// 設置響應的類型格式為圖片格式
response.setContentType("image/jpeg");
//禁止圖像緩存。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
HttpSession session = request.getSession();

ValidateCode vCode = new ValidateCode(140, 50, 4, 0);
String key = String.format(KConstants.Key.IMGCODE, telephone.trim());
SKBeanUtils.getRedisCRUD().set(key, vCode.getCode());
SKBeanUtils.getRedisCRUD().expire(key, 300);

session.setAttribute("code", vCode.getCode());
// session.setMaxInactiveInterval(10*60);
System.out.println("getImgCode telephone ===>" + telephone + " code " + vCode.getCode());
vCode.write(response.getOutputStream());
}

/**
*
* @param width 圖片寬
* @param height 圖片高
* @param codeCount 字符個數
* @param lineCount 干擾線條數
*/
public ValidateCode(int width,int height,int codeCount,int lineCount) {
this.width=width;
this.height=height;
this.codeCount=codeCount;
this.lineCount=lineCount;
this.createCode();
}

public void createCode() {
int x = 0,fontHeight=0,codeY=0;
int red = 0, green = 0, blue = 0;

x = width / (codeCount);//每個字符的寬度
fontHeight = height -4;//字體的高度
codeY = height - 8;

// 圖像buffer
buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 生成隨機數
Random random = new Random();
// 將圖像填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 創建字體
ImgFontByte imgFont = new ImgFontByte();
Font font =imgFont.getFont(fontHeight);
g.setFont(font);

for (int i = 0; i < lineCount; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs+random.nextInt(width/8);
int ye = ys+random.nextInt(height/8);
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawLine(xs, ys, xe, ye);
}

// randomCode記錄隨機產生的驗證碼
StringBuffer randomCode = new StringBuffer();
// 隨機產生codeCount個字符的驗證碼。
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
// 產生隨機的顏色值,讓輸出的每個字符的顏色值都將不同。
red =0;
green = 0;
blue =0;
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i) * x, codeY);
// 將產生的四個隨機數組合在一起。
randomCode.append(strRand);
}
// 將四位數字的驗證碼保存到Session中。
code=randomCode.toString();
}

頁面:這樣的話就得到生成的二維碼。 var url = "/getImgCode?telephone=" + account+"&timestamp=" + new Date().getTime();

$("#getImgCode").show(); $("#getImgCode").attr("src", url);

轉載于:https://www.cnblogs.com/echo777/p/11549382.html

總結

以上是生活随笔為你收集整理的生成二维码接口,前端调用接口将二维码显示在页面上的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。