验证码工具类

待我称王封你为后i 2022-04-25 04:28 341阅读 0赞

验证码工具类

在这里插入图片描述

  1. package com.sky.ospringboot.common.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.util.Random;
  7. import javax.imageio.ImageIO;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11. import lombok.extern.log4j.Log4j2;
  12. @Log4j2
  13. public class ValidateCodeUtil {
  14. public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";// 放到session中的key
  15. private int width = 95;// 图片宽
  16. private int height = 25;// 图片高
  17. private int lineSize = 40;// 干扰线数量
  18. private int stringNum = 4;// 随机产生字符数量
  19. private Random random = new Random();
  20. /** * 获得字体 */
  21. private Font getFont() {
  22. return new Font("Fixedsys", Font.CENTER_BASELINE, 18);
  23. }
  24. /** * 获得颜色 */
  25. private Color getRandColor(int fc, int bc) {
  26. if (fc > 255) {
  27. fc = 255;
  28. }
  29. if (bc > 255) {
  30. bc = 255;
  31. }
  32. int r = fc + random.nextInt(bc - fc - 16);
  33. int g = fc + random.nextInt(bc - fc - 14);
  34. int b = fc + random.nextInt(bc - fc - 18);
  35. return new Color(r, g, b);
  36. }
  37. /** * 生成随机图片 */
  38. public void getRandcode(HttpServletRequest request, HttpServletResponse response) {
  39. HttpSession session = request.getSession();
  40. // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
  41. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
  42. Graphics g = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
  43. g.fillRect(0, 0, width, height);// 图片大小
  44. g.setFont(new Font("Default", Font.ROMAN_BASELINE, 18));// 字体大小
  45. g.setColor(getRandColor(110, 133));// 字体颜色
  46. // 绘制干扰线
  47. for (int i = 0; i <= lineSize; i++) {
  48. drowLine(g);
  49. }
  50. // 绘制随机字符
  51. String randomString = "";
  52. for (int i = 1; i <= stringNum; i++) {
  53. randomString += drowString(g, getRandomString(), i);
  54. }
  55. log.info(randomString);
  56. // 将生成的随机字符串保存到session中
  57. session.removeAttribute(RANDOMCODEKEY);
  58. session.setAttribute(RANDOMCODEKEY, randomString);
  59. g.dispose();
  60. try {
  61. // 将内存中的图片通过流动形式输出到客户端
  62. ImageIO.write(image, "JPEG", response.getOutputStream());
  63. } catch (Exception e) {
  64. log.error("将内存中的图片通过流动形式输出到客户端失败>>>> ", e);
  65. }
  66. }
  67. /** * 绘制字符串 */
  68. private String drowString(Graphics g, String rand, int i) {
  69. g.setFont(getFont());
  70. g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
  71. g.translate(random.nextInt(3), random.nextInt(3));
  72. g.drawString(rand, 13 * i, 16);
  73. return rand;
  74. }
  75. /** * 绘制干扰线 */
  76. private void drowLine(Graphics g) {
  77. int x = random.nextInt(width);
  78. int y = random.nextInt(height);
  79. int xl = random.nextInt(13);
  80. int yl = random.nextInt(15);
  81. g.drawLine(x, y, x + xl, y + yl);
  82. }
  83. /** * 获取随机的字符 */
  84. public String getRandomString() {
  85. StringBuffer sb = new StringBuffer();
  86. sb.append("123456789");
  87. sb.append("ABCDEFGHJKLMNPQRSTUVWXYZ");
  88. sb.append("abcdefghjklmnpqrstuvwxyz");
  89. int nextInt = random.nextInt(sb.length());
  90. return sb.charAt(nextInt) + "";
  91. }
  92. }

发表评论

表情:
评论列表 (有 0 条评论,341人围观)

还没有评论,来说两句吧...

相关阅读