CodeUtil.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package jnpf.util;
  2. import lombok.Cleanup;
  3. import javax.imageio.ImageIO;
  4. import jakarta.servlet.ServletOutputStream;
  5. import jakarta.servlet.http.HttpServletResponse;
  6. import jakarta.servlet.http.HttpSession;
  7. import java.awt.*;
  8. import java.awt.image.BufferedImage;
  9. import java.util.Objects;
  10. import java.util.Random;
  11. /**
  12. *
  13. * @author JNPF开发平台组
  14. * @version V3.1.0
  15. * @copyright 引迈信息技术有限公司
  16. * @date 2021/3/16 10:45
  17. */
  18. public class CodeUtil {
  19. /**
  20. *放到session中的key
  21. */
  22. public static final String RANDOMCODEKEY = "RANDOMCODEKEY";
  23. private Random random = new Random();
  24. /**
  25. *随机产生的字符串
  26. */
  27. private String randString = "abcdefghjklmnopqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  28. /**
  29. *图片宽
  30. */
  31. private int width = 120;
  32. /**
  33. *图片高
  34. */
  35. private int height = 40;
  36. /**
  37. *干扰线数量
  38. */
  39. private int lineSize = 10;
  40. /**
  41. *随机产生字符数量
  42. */
  43. private int stringNum = 4;
  44. /**
  45. * 获得字体
  46. */
  47. private Font getFont(){
  48. return new Font("Fixedsys", Font.CENTER_BASELINE,18);
  49. }
  50. /**
  51. * 获得颜色
  52. */
  53. private Color getRandColor(int fc, int bc){
  54. if(fc > 255) {
  55. fc = 255;
  56. }
  57. if(bc > 255) {
  58. bc = 255;
  59. }
  60. int r = fc + random.nextInt(bc-fc-16);
  61. int g = fc + random.nextInt(bc-fc-14);
  62. int b = fc + random.nextInt(bc-fc-18);
  63. return new Color(r,g,b);
  64. }
  65. /**
  66. * 生成随机图片
  67. */
  68. public void getRandcode(HttpServletResponse response, Integer stringLength) {
  69. try {
  70. HttpSession session = ServletUtil.getSession();
  71. //BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
  72. BufferedImage image = new BufferedImage(width,height, BufferedImage.TYPE_INT_BGR);
  73. //产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
  74. Graphics g = image.getGraphics();
  75. g.fillRect(0, 0, width, height);
  76. g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE,20));
  77. g.setColor(getRandColor(110, 133));
  78. //绘制干扰线
  79. for(int i=0;i<=lineSize;i++){
  80. drowLine(g);
  81. }
  82. //绘制随机字符
  83. String randomString = "";
  84. if (Objects.nonNull(stringLength)) {
  85. stringNum = stringLength;
  86. }
  87. for(int i = 1; i <= stringNum; i++){
  88. randomString=drowString(g,randomString,i);
  89. }
  90. session.removeAttribute(RANDOMCODEKEY);
  91. session.setAttribute(RANDOMCODEKEY, randomString);
  92. System.out.println("后台代码"+randomString);
  93. g.dispose();
  94. @Cleanup ServletOutputStream outputStream = response.getOutputStream();
  95. //将内存中的图片通过流动形式输出到客户端
  96. ImageIO.write(image, "JPEG", outputStream);
  97. }catch (Exception e){
  98. e.getStackTrace();
  99. }
  100. }
  101. /**
  102. * 绘制字符串
  103. */
  104. private String drowString(Graphics g, String randomString, int i){
  105. g.setFont(getFont());
  106. g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
  107. String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
  108. randomString +=rand;
  109. g.translate(random.nextInt(3), random.nextInt(3));
  110. g.drawString(rand, 15*i, 22);
  111. return randomString;
  112. }
  113. /**
  114. * 绘制干扰线
  115. */
  116. private void drowLine(Graphics g){
  117. int x = random.nextInt(width);
  118. int y = random.nextInt(height);
  119. int xl = random.nextInt(13);
  120. int yl = random.nextInt(15);
  121. g.drawLine(x, y, x+xl, y+yl);
  122. }
  123. /**
  124. * 获取随机的字符
  125. */
  126. public String getRandomString(int num){
  127. return String.valueOf(randString.charAt(num));
  128. }
  129. }