VerifyUtil.java 3.8 KB

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