Generate two-dimensional code interfaces, front-end interface to call the two-dimensional code displayed on the page

The main numbers and letters are generated two-dimensional code 4

We will generate two-dimensional code put to the cache, there is an expiration time. When the two-dimensional code verification need to get the value of the two-dimensional code based on key values, as if the page code and pass over the cache, then the code is entered correctly.

@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 wide image width
* @param high image height
* @param codeCount number of characters
* @param interference lineCount number of lines
* /
public ValidateCode (int width, int height, int codeCount, int lineCount) {
the this. width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode ();
}

public void the createCode () {
int X = 0, FontHeight = 0, = 0, codeY;
int Red 0 =, = 0 Green, Blue = 0;

X = width / (codeCount); // for each character width
fontHeight = height -4; // height of the font
, codeY = height -. 8;

// image 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);
= random.nextInt Green (255);
Blue = random.nextInt (255);
g.setColor (new new Color (Red, Green, Blue));
g.drawLine (XS, YS, XE, YE);
}

// randomCode recording the generated random codes
the StringBuffer randomCode the StringBuffer new new = ();
// codeCount randomly generated character codes.
for (int I = 0; I <codeCount; I ++) {
String strRand = String.valueOf (codeSequence [random.nextInt (codeSequence.length)]);
// generate random color value, so that each character of the output color values are different.
0 = Red;
Green = 0;
Blue = 0;
g.setColor (new new Color (Red, Green, Blue));
g.drawString (strRand, (I) * X,, codeY);
// four randomly generated the number of combinations together.
randomCode.append (strRand);
}
// save the four-digit codes to the Session.
= randomCode.toString code ();
}

Page: so would give a two-dimensional code generation.
var url = "/getImgCode?telephone=" + account+"&timestamp=" + new Date().getTime();

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

Guess you like

Origin www.cnblogs.com/echo777/p/11549382.html
Recommended