另一种思路,后台生成随机数,前端生成画布,用ajax拿随机数

//后台只生成随机数

 

@RequestMapping(value="random.action")
public void findRandom (HttpServletResponse response) throws IOException{
// 验证码字符个数
int codeCount = 4;
char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

// 创建一个随机数生成器类
Random random = new Random();
// randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBuffer randomCode = new StringBuffer();
for (int i = 0; i < codeCount; i++) {
// 得到随机产生的验证码数字。
String strRand = String.valueOf(codeSequence[random.nextInt(36)]);
// 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
PrintWriter out = response.getWriter();
out.print(randomCode);
}

 

 

前端页面

<body>
<canvas );
ctx.fillStyle = randomColor(50,160); //随机生成字体颜色
ctx.font = randomNum(15,20)+'px SimHei'; //随机生成字体大小
var x = 20;
var y = randomNum(20,30);
var deg = randomNum(-45, 45);
//修改坐标原点和旋转角度
ctx.translate(x,y);
ctx.rotate(deg*Math.PI/180);
ctx.fillText(txt, 0,0);
//恢复坐标原点和旋转角度
ctx.rotate(-deg*Math.PI/180);
ctx.translate(-x,-y);
/* } */
/* /**绘制干扰线**/
for(var i=0; i<8; i++){
ctx.strokeStyle = randomColor(40,180);
ctx.beginPath();
ctx.moveTo( randomNum(0,width), randomNum(0,height) );
ctx.lineTo( randomNum(0,width), randomNum(0,height) );
ctx.stroke();
}
/**绘制干扰点**/
/* for(var i=0; i<100; i++){
ctx.fillStyle = randomColor(0,255);
ctx.beginPath();
ctx.arc(randomNum(0,width),randomNum(0,height), 1, 0, 2*Math.PI);
ctx.fill();
} */
}

相关文章:

  • 2022-12-23
  • 2021-11-29
  • 2021-07-14
  • 2021-12-29
  • 2022-12-23
  • 2021-12-04
  • 2020-04-12
猜你喜欢
  • 2021-05-15
  • 2022-12-23
  • 2021-11-22
  • 2021-06-13
  • 2022-12-23
  • 2021-10-08
  • 2021-12-21
相关资源
相似解决方案