工具类---生成验证码图片工具类

怼烎@ 2023-07-10 05:13 92阅读 0赞
  1. /**
  2. * 可生成数字、大写、小写字母及三者混合类型的验证码。
  3. * 支持自定义验证码字符数量;
  4. * 支持自定义验证码图片的大小;
  5. * 支持自定义需排除的特殊字符;
  6. * 支持自定义干扰线的数量;
  7. * 支持自定义验证码图文颜色
  8. */
  9. public class ValidateUtil {
  10. /** 验证码类型为仅数字 0~9 */
  11. public static final int TYPE_NUM_ONLY = 0;
  12. /** 验证码类型为仅字母,即大写、小写字母混合 */
  13. public static final int TYPE_LETTER_ONLY = 1;
  14. /** 验证码类型为数字、大写字母、小写字母混合 */
  15. public static final int TYPE_ALL_MIXED = 2;
  16. /** 验证码类型为数字、大写字母混合 */
  17. public static final int TYPE_NUM_UPPER = 3;
  18. /** 验证码类型为数字、小写字母混合 */
  19. public static final int TYPE_NUM_LOWER = 4;
  20. /** 验证码类型为仅大写字母 */
  21. public static final int TYPE_UPPER_ONLY = 5;
  22. /** 验证码类型为仅小写字母 */
  23. public static final int TYPE_LOWER_ONLY = 6;
  24. private ValidateUtil() {
  25. }
  26. /**
  27. * 生成验证码字符串
  28. *
  29. * @param type 验证码类型,参见本类的静态属性
  30. * @param length 验证码长度,大于0的整数
  31. * @param exChars 需排除的特殊字符(仅对数字、字母混合型验证码有效,无需排除则为null)
  32. * @return 验证码字符串
  33. */
  34. public static String generateTextCode(int type, int length, String exChars) {
  35. if (length <= 0)
  36. return "";
  37. StringBuffer code = new StringBuffer();
  38. int i = 0;
  39. Random r = new Random();
  40. switch (type) {
  41. // 仅数字
  42. case TYPE_NUM_ONLY:
  43. while (i < length) {
  44. int t = r.nextInt(10);
  45. if (exChars == null || exChars.indexOf(t + "") < 0) {// 排除特殊字符
  46. code.append(t);
  47. i++;
  48. }
  49. }
  50. break;
  51. // 仅字母(即大写字母、小写字母混合)
  52. case TYPE_LETTER_ONLY:
  53. while (i < length) {
  54. int t = r.nextInt(123);
  55. if ((t >= 97 || (t >= 65 && t <= 90)) && (exChars == null || exChars.indexOf((char) t) < 0)) {
  56. code.append((char) t);
  57. i++;
  58. }
  59. }
  60. break;
  61. // 数字、大写字母、小写字母混合
  62. case TYPE_ALL_MIXED:
  63. while (i < length) {
  64. int t = r.nextInt(123);
  65. if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t <= 57))
  66. && (exChars == null || exChars.indexOf((char) t) < 0)) {
  67. code.append((char) t);
  68. i++;
  69. }
  70. }
  71. break;
  72. // 数字、大写字母混合
  73. case TYPE_NUM_UPPER:
  74. while (i < length) {
  75. int t = r.nextInt(91);
  76. if ((t >= 65 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) {
  77. code.append((char) t);
  78. i++;
  79. }
  80. }
  81. break;
  82. // 数字、小写字母混合
  83. case TYPE_NUM_LOWER:
  84. while (i < length) {
  85. int t = r.nextInt(123);
  86. if ((t >= 97 || (t >= 48 && t <= 57)) && (exChars == null || exChars.indexOf((char) t) < 0)) {
  87. code.append((char) t);
  88. i++;
  89. }
  90. }
  91. break;
  92. // 仅大写字母
  93. case TYPE_UPPER_ONLY:
  94. while (i < length) {
  95. int t = r.nextInt(91);
  96. if ((t >= 65) && (exChars == null || exChars.indexOf((char) t) < 0)) {
  97. code.append((char) t);
  98. i++;
  99. }
  100. }
  101. break;
  102. // 仅小写字母
  103. case TYPE_LOWER_ONLY:
  104. while (i < length) {
  105. int t = r.nextInt(123);
  106. if ((t >= 97) && (exChars == null || exChars.indexOf((char) t) < 0)) {
  107. code.append((char) t);
  108. i++;
  109. }
  110. }
  111. break;
  112. }
  113. return code.toString();
  114. }
  115. /**
  116. * 已有验证码,生成验证码图片
  117. *
  118. * @param textCode 文本验证码
  119. * @param width 图片宽度
  120. * @param height 图片高度
  121. * @param interLine 图片中干扰线的条数
  122. * @param randomLocation 每个字符的高低位置是否随机
  123. * @param backColor 图片颜色,若为null,则采用随机颜色
  124. * @param foreColor 字体颜色,若为null,则采用随机颜色
  125. * @param lineColor 干扰线颜色,若为null,则采用随机颜色
  126. * @return 图片缓存对象
  127. */
  128. public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine,
  129. boolean randomLocation, Color backColor, Color foreColor, Color lineColor) {
  130. BufferedImage bim = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  131. Graphics g = bim.getGraphics();
  132. // 画背景图
  133. g.setColor(backColor == null ? getRandomColor() : backColor);
  134. g.fillRect(0, 0, width, height);
  135. // 画干扰线
  136. Random r = new Random();
  137. if (interLine > 0) {
  138. int x = 0, y = 0, x1 = width, y1 = 0;
  139. for (int i = 0; i < interLine; i++) {
  140. g.setColor(lineColor == null ? getRandomColor() : lineColor);
  141. y = r.nextInt(height);
  142. y1 = r.nextInt(height);
  143. g.drawLine(x, y, x1, y1);
  144. }
  145. }
  146. // 写验证码
  147. // 字体大小为图片高度的80%
  148. int fsize = (int) (height * 0.8);
  149. int fx = height - fsize;
  150. int fy = fsize;
  151. g.setFont(new Font("Default", Font.PLAIN, fsize));
  152. // 写验证码字符
  153. for (int i = 0; i < textCode.length(); i++) {
  154. fy = randomLocation ? (int) ((Math.random() * 0.3 + 0.6) * height) : fy;// 每个字符高低是否随机
  155. g.setColor(foreColor == null ? getRandomColor() : foreColor);
  156. g.drawString(textCode.charAt(i) + "", fx, fy);
  157. fx += fsize * 0.9;
  158. }
  159. g.dispose();
  160. return bim;
  161. }
  162. /**
  163. * 生成图片验证码
  164. *
  165. * @param type 验证码类型,参见本类的静态属性
  166. * @param length 验证码字符长度,大于0的整数
  167. * @param exChars 需排除的特殊字符
  168. * @param width 图片宽度
  169. * @param height 图片高度
  170. * @param interLine 图片中干扰线的条数
  171. * @param randomLocation 每个字符的高低位置是否随机
  172. * @param backColor 图片颜色,若为null,则采用随机颜色
  173. * @param foreColor 字体颜色,若为null,则采用随机颜色
  174. * @param lineColor 干扰线颜色,若为null,则采用随机颜色
  175. * @return 图片缓存对象
  176. */
  177. public static BufferedImage generateImageCode(int type, int length, String exChars, int width, int height,
  178. int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) {
  179. String textCode = generateTextCode(type, length, exChars);
  180. BufferedImage bim = generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor,
  181. lineColor);
  182. return bim;
  183. }
  184. /**
  185. * 产生随机颜色
  186. *
  187. * @return
  188. */
  189. private static Color getRandomColor() {
  190. Random r = new Random();
  191. Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
  192. return c;
  193. }
  194. }

编写生成验证码图片的接口:

  1. @RequestMapping(value = "/validateCode",method=RequestMethod.GET)
  2. public void validateCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
  3. response.setHeader("Cache-Control", "no-cache");
  4. String verifyCode = ValidateUtil.generateTextCode(ValidateUtil.TYPE_ALL_MIXED, 4, "0Ooil"); //获取验证码
  5. request.getSession().setAttribute("validateCode", verifyCode); //验证码放到session中
  6. response.setContentType("image/jpeg");
  7. BufferedImage bim = ValidateUtil.generateImageCode(verifyCode, 100, 25,7, true, Color.WHITE, Color.BLACK, null); // 生成验证码图片
  8. ImageIO.write(bim, "JPEG", response.getOutputStream()); // 返回到前台
  9. }

前端使用:

  1. <img src="/manager/validateCode">

发表评论

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

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

相关阅读