VerifyCodeUtils.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package com.ruoyi.common.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.geom.AffineTransform;
  8. import java.awt.image.BufferedImage;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. import java.security.SecureRandom;
  12. import java.util.Arrays;
  13. import java.util.Random;
  14. import javax.imageio.ImageIO;
  15. /**
  16. * 验证码工具类
  17. *
  18. * @author ruoyi
  19. */
  20. public class VerifyCodeUtils
  21. {
  22. // 使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
  23. public static final String VERIFY_CODES = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  24. private static Random random = new SecureRandom();
  25. /**
  26. * 使用系统默认字符源生成验证码
  27. *
  28. * @param verifySize 验证码长度
  29. * @return
  30. */
  31. public static String generateVerifyCode(int verifySize)
  32. {
  33. return generateVerifyCode(verifySize, VERIFY_CODES);
  34. }
  35. /**
  36. * 使用指定源生成验证码
  37. *
  38. * @param verifySize 验证码长度
  39. * @param sources 验证码字符源
  40. * @return
  41. */
  42. public static String generateVerifyCode(int verifySize, String sources)
  43. {
  44. if (sources == null || sources.length() == 0)
  45. {
  46. sources = VERIFY_CODES;
  47. }
  48. int codesLen = sources.length();
  49. Random rand = new Random(System.currentTimeMillis());
  50. StringBuilder verifyCode = new StringBuilder(verifySize);
  51. for (int i = 0; i < verifySize; i++)
  52. {
  53. verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
  54. }
  55. return verifyCode.toString();
  56. }
  57. /**
  58. * 输出指定验证码图片流
  59. *
  60. * @param w
  61. * @param h
  62. * @param os
  63. * @param code
  64. * @throws IOException
  65. */
  66. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException
  67. {
  68. int verifySize = code.length();
  69. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  70. Random rand = new Random();
  71. Graphics2D g2 = image.createGraphics();
  72. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  73. Color[] colors = new Color[5];
  74. Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA,
  75. Color.ORANGE, Color.PINK, Color.YELLOW };
  76. float[] fractions = new float[colors.length];
  77. for (int i = 0; i < colors.length; i++)
  78. {
  79. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
  80. fractions[i] = rand.nextFloat();
  81. }
  82. Arrays.sort(fractions);
  83. g2.setColor(Color.GRAY);// 设置边框色
  84. g2.fillRect(0, 0, w, h);
  85. Color c = getRandColor(200, 250);
  86. g2.setColor(c);// 设置背景色
  87. g2.fillRect(0, 2, w, h - 4);
  88. // 绘制干扰线
  89. Random random = new Random();
  90. g2.setColor(getRandColor(160, 200));// 设置线条的颜色
  91. for (int i = 0; i < 20; i++)
  92. {
  93. int x = random.nextInt(w - 1);
  94. int y = random.nextInt(h - 1);
  95. int xl = random.nextInt(6) + 1;
  96. int yl = random.nextInt(12) + 1;
  97. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
  98. }
  99. // 添加噪点
  100. float yawpRate = 0.05f;// 噪声率
  101. int area = (int) (yawpRate * w * h);
  102. for (int i = 0; i < area; i++)
  103. {
  104. int x = random.nextInt(w);
  105. int y = random.nextInt(h);
  106. int rgb = getRandomIntColor();
  107. image.setRGB(x, y, rgb);
  108. }
  109. shear(g2, w, h, c);// 使图片扭曲
  110. g2.setColor(getRandColor(100, 160));
  111. int fontSize = h - 4;
  112. Font font = new Font("Algerian", Font.ITALIC, fontSize);
  113. g2.setFont(font);
  114. char[] chars = code.toCharArray();
  115. for (int i = 0; i < verifySize; i++)
  116. {
  117. AffineTransform affine = new AffineTransform();
  118. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1),
  119. (w / verifySize) * i + fontSize / 2, h / 2);
  120. g2.setTransform(affine);
  121. g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10);
  122. }
  123. g2.dispose();
  124. ImageIO.write(image, "jpg", os);
  125. }
  126. private static Color getRandColor(int fc, int bc)
  127. {
  128. if (fc > 255) {
  129. fc = 255;
  130. }
  131. if (bc > 255) {
  132. bc = 255;
  133. }
  134. int r = fc + random.nextInt(bc - fc);
  135. int g = fc + random.nextInt(bc - fc);
  136. int b = fc + random.nextInt(bc - fc);
  137. return new Color(r, g, b);
  138. }
  139. private static int getRandomIntColor()
  140. {
  141. int[] rgb = getRandomRgb();
  142. int color = 0;
  143. for (int c : rgb)
  144. {
  145. color = color << 8;
  146. color = color | c;
  147. }
  148. return color;
  149. }
  150. private static int[] getRandomRgb()
  151. {
  152. int[] rgb = new int[3];
  153. for (int i = 0; i < 3; i++)
  154. {
  155. rgb[i] = random.nextInt(255);
  156. }
  157. return rgb;
  158. }
  159. private static void shear(Graphics g, int w1, int h1, Color color)
  160. {
  161. shearX(g, w1, h1, color);
  162. shearY(g, w1, h1, color);
  163. }
  164. private static void shearX(Graphics g, int w1, int h1, Color color)
  165. {
  166. int period = random.nextInt(2);
  167. boolean borderGap = true;
  168. int frames = 1;
  169. int phase = random.nextInt(2);
  170. for (int i = 0; i < h1; i++)
  171. {
  172. double d = (double) (period >> 1)
  173. * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
  174. g.copyArea(0, i, w1, 1, (int) d, 0);
  175. if (borderGap)
  176. {
  177. g.setColor(color);
  178. g.drawLine((int) d, i, 0, i);
  179. g.drawLine((int) d + w1, i, w1, i);
  180. }
  181. }
  182. }
  183. private static void shearY(Graphics g, int w1, int h1, Color color)
  184. {
  185. int period = random.nextInt(40) + 10; // 50;
  186. int frames = 20;
  187. int phase = 7;
  188. for (int i = 0; i < w1; i++)
  189. {
  190. double d = (double) (period >> 1)
  191. * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
  192. g.copyArea(i, 0, 1, h1, 0, (int) d);
  193. g.setColor(color);
  194. g.drawLine(i, (int) d, i, 0);
  195. g.drawLine(i, (int) d + h1, i, h1);
  196. }
  197. }
  198. }