相关文章推荐
豪气的沙滩裤  ·  Connect to a ...·  11 月前    · 
低调的炒面  ·  Centos7 ...·  1 年前    · 
爱旅游的佛珠  ·  必备 .NET - 使用 .NET ...·  1 年前    · 

Java生成随机图片验证码



前台html代码


  1. <div style="margin-top: 50px;">

  2. <span>验证码:</span><input type="text" name="verifyCode" id="verifyCode" style="width: 75px;height: 25px;"/>

  3. <img id="verifyCodeImg" alt="点击更换" src="/qos/dog/getVerifyCodeImg"

  4. title="点击更换" onclick="change()">

  5. </div>


前台js代码


  1. function change() {

  2. var verifyCode = document.getElementById("verifyCodeImg");

  3. verifyCode.src = "/qos/dog/getVerifyCodeImg?time=" + Math.random(1000);

  4. }




  5. /*-*/


  6. /qos/dog/ 这里的路径是需要换成自己的哦


验证代码,在controller里面新建一个util文件夹,然后放入VerifyCodeUtil.java
代码如下


  1. package com.paladin.qos.util;


  2. import javax.imageio.ImageIO;

  3. import java.awt.*;

  4. import java.awt.image.BufferedImage;

  5. import java.io.ByteArrayOutputStream;

  6. import java.io.IOException;

  7. import java.util.Random;


  8. public class VerifyCodeUtil {


  9. private static final Random random = new Random();

  10. private static final String[] fontNames = {"宋体", "华文楷体", "黑体", "Georgia", "微软雅黑", "楷体_GB2312"};


  11. public static String drawImage(ByteArrayOutputStream output) {

  12. String code = "";

  13. int width = 50;

  14. int height = 25;


  15. //创建图片缓冲区

  16. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);


  17. Graphics2D g = bi.createGraphics();


  18. //设置背景颜色

  19. g.setBackground(new Color(255, 255, 255));

  20. g.clearRect(0, 0, width, height);


  21. StringBuilder stringBuilder = new StringBuilder();

  22. //这里只画入四个字符

  23. for (int i = 0; i < 4; i++) {

  24. String s = randomChar() + ""; //随机生成字符,因为只有画字符串的方法,没有画字符的方法,所以需要将字符变成字符串再画

  25. stringBuilder.append(s); //添加到StringBuilder里面

  26. float x = i * 1.0F * width / 4; //定义字符的x坐标

  27. g.setFont(randomFont()); //设置字体,随机

  28. g.setColor(randomColor()); //设置颜色,随机

  29. g.drawString(s, x, height - 5);

  30. }

  31. code = stringBuilder.toString(); //获取验证码字符串


  32. //定义干扰线

  33. //定义干扰线的数量(3-5条)int num = random.nextInt(max)%(max-min+1) + min;

  34. int num = random.nextInt(5) % 3 + 3;

  35. Graphics2D graphics = (Graphics2D) bi.getGraphics();

  36. for (int i = 0; i < num; i++) {

  37. int x1 = random.nextInt(width);

  38. int y1 = random.nextInt(height);

  39. int x2 = random.nextInt(width);

  40. int y2 = random.nextInt(height);

  41. graphics.setColor(randomColor());

  42. graphics.drawLine(x1, y1, x2, y2);

  43. }

  44. // 释放图形上下文

  45. g.dispose();

  46. try {

  47. ImageIO.write(bi, "jpg", output);

  48. } catch (IOException e) {

  49. e.printStackTrace();

  50. }

  51. return code; //为了方便取值,直接返回code,




  52. }


  53. //随机字体

  54. private static Font randomFont() {

  55. int index = random.nextInt(fontNames.length);

  56. String fontName = fontNames[index];

  57. int style = random.nextInt(4); //随机获取4种字体的样式

  58. int size = random.nextInt(20) % 6 + 15; //随机获取字体的大小(10-20之间的值)

  59. return new Font(fontName, style, size);

  60. }


  61. //随机颜色

  62. private static Color randomColor() {

  63. int r = random.nextInt(225);

  64. int g = random.nextInt(225);

  65. int b = random.nextInt(225);

  66. return new Color(r, g, b);

  67. }



  68. //随机字符

  69. private static char randomChar() {

  70. //A-Z,a-z,0-9,可剔除一些难辨认的字母与数字

  71. String str = "0123456789ABCdefghiDEFGHIJopPQRVWXYZabcjklSTUmnqrstKLMNOvuwxyz";


  72. return str.charAt(random.nextInt(str.length()));

  73. }


  74. }


最后,在controller里面引用


  1. @RequestMapping("/getVerifyCodeImg")

  2. @ResponseBody

  3. public void getVerifyCodeImg(HttpServletResponse response, HttpSession session) {

  4. ByteArrayOutputStream output = new ByteArrayOutputStream();

  5. String code = VerifyCodeUtil.drawImage(output);

  6. //将验证码文本直接存放到session中

  7. session.setAttribute("verifyCode", code);

  8. try {

  9. ServletOutputStream out = response.getOutputStream();

  10. output.writeTo(out);

  11. } catch (IOException e) {

  12. e.printStackTrace();

  13. }

  14. }


发布于 2019-12-10 20:02

文章被以下专栏收录