14f4f4e3db42bf724aa3dfc1d2a5b56f37cc95f2.svn-base 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package org.jeecg.modules.system.util;
  2. import javax.imageio.ImageIO;
  3. import javax.servlet.http.HttpServletResponse;
  4. import java.awt.*;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.IOException;
  8. import java.util.Base64;
  9. import java.util.Random;
  10. /**
  11. * 登录验证码工具类
  12. */
  13. public class RandImageUtil {
  14. public static final String key = "JEECG_LOGIN_KEY";
  15. /**
  16. * 定义图形大小
  17. */
  18. private static final int width = 105;
  19. /**
  20. * 定义图形大小
  21. */
  22. private static final int height = 35;
  23. /**
  24. * 定义干扰线数量
  25. */
  26. private static final int count = 200;
  27. /**
  28. * 干扰线的长度=1.414*lineWidth
  29. */
  30. private static final int lineWidth = 2;
  31. /**
  32. * 图片格式
  33. */
  34. private static final String IMG_FORMAT = "JPEG";
  35. /**
  36. * base64 图片前缀
  37. */
  38. private static final String BASE64_PRE = "data:image/jpg;base64,";
  39. /**
  40. * 直接通过response 返回图片
  41. * @param response
  42. * @param resultCode
  43. * @throws IOException
  44. */
  45. public static void generate(HttpServletResponse response, String resultCode) throws IOException {
  46. BufferedImage image = getImageBuffer(resultCode);
  47. // 输出图象到页面
  48. ImageIO.write(image, IMG_FORMAT, response.getOutputStream());
  49. }
  50. /**
  51. * 生成base64字符串
  52. * @param resultCode
  53. * @return
  54. * @throws IOException
  55. */
  56. public static String generate(String resultCode) throws IOException {
  57. BufferedImage image = getImageBuffer(resultCode);
  58. ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  59. //写入流中
  60. ImageIO.write(image, IMG_FORMAT, byteStream);
  61. //转换成字节
  62. byte[] bytes = byteStream.toByteArray();
  63. //转换成base64串
  64. String base64 = Base64.getEncoder().encodeToString(bytes).trim();
  65. base64 = base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
  66. //写到指定位置
  67. //ImageIO.write(bufferedImage, "png", new File(""));
  68. return BASE64_PRE+base64;
  69. }
  70. private static BufferedImage getImageBuffer(String resultCode){
  71. // 在内存中创建图象
  72. final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  73. // 获取图形上下文
  74. final Graphics2D graphics = (Graphics2D) image.getGraphics();
  75. // 设定背景颜色
  76. graphics.setColor(Color.WHITE); // ---1
  77. graphics.fillRect(0, 0, width, height);
  78. // 设定边框颜色
  79. // graphics.setColor(getRandColor(100, 200)); // ---2
  80. graphics.drawRect(0, 0, width - 1, height - 1);
  81. final Random random = new Random();
  82. // 随机产生干扰线,使图象中的认证码不易被其它程序探测到
  83. for (int i = 0; i < count; i++) {
  84. graphics.setColor(getRandColor(150, 200)); // ---3
  85. final int x = random.nextInt(width - lineWidth - 1) + 1; // 保证画在边框之内
  86. final int y = random.nextInt(height - lineWidth - 1) + 1;
  87. final int xl = random.nextInt(lineWidth);
  88. final int yl = random.nextInt(lineWidth);
  89. graphics.drawLine(x, y, x + xl, y + yl);
  90. }
  91. // 取随机产生的认证码
  92. for (int i = 0; i < resultCode.length(); i++) {
  93. // 将认证码显示到图象中,调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  94. // graphics.setColor(new Color(20 + random.nextInt(130), 20 + random
  95. // .nextInt(130), 20 + random.nextInt(130)));
  96. // 设置字体颜色
  97. graphics.setColor(Color.BLACK);
  98. // 设置字体样式
  99. // graphics.setFont(new Font("Arial Black", Font.ITALIC, 18));
  100. graphics.setFont(new Font("Times New Roman", Font.BOLD, 24));
  101. // 设置字符,字符间距,上边距
  102. graphics.drawString(String.valueOf(resultCode.charAt(i)), (23 * i) + 8, 26);
  103. }
  104. // 图象生效
  105. graphics.dispose();
  106. return image;
  107. }
  108. private static Color getRandColor(int fc, int bc) { // 取得给定范围随机颜色
  109. final Random random = new Random();
  110. if (fc > 255) {
  111. fc = 255;
  112. }
  113. if (bc > 255) {
  114. bc = 255;
  115. }
  116. final int r = fc + random.nextInt(bc - fc);
  117. final int g = fc + random.nextInt(bc - fc);
  118. final int b = fc + random.nextInt(bc - fc);
  119. return new Color(r, g, b);
  120. }
  121. }