RandomValidateCodeUtil.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package cn.keking.utils;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Random;
  7. public class RandomValidateCodeUtil {
  8. private static final int width = 100;// 定义图片的width
  9. private static final int height = 30;// 定义图片的height
  10. private static final int codeCount = 4;// 定义图片上显示验证码的个数
  11. private static final int xx = 18;
  12. private static final int fontHeight = 28;
  13. private static final int codeY = 27;
  14. private static final char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R','T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  15. 'a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y', '2', '3', '4','5', '6', '7', '8', '9' };
  16. /**
  17. * 生成一个map集合
  18. * code为生成的验证码
  19. * codePic为生成的验证码BufferedImage对象
  20. */
  21. public static Map<String,Object> generateCodeAndPic(String ip, String sessionCode, int lx) {
  22. // 定义图像buffer
  23. BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  24. // Graphics2D gd = buffImg.createGraphics();
  25. // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
  26. Graphics gd = buffImg.getGraphics();
  27. // 创建一个随机数生成器类
  28. Random random = new Random();
  29. // 将图像填充为白色
  30. gd.setColor(Color.WHITE);
  31. gd.fillRect(0, 0, width, height);
  32. // 创建字体,字体的大小应该根据图片的高度来定。
  33. Font font = new Font("Times New Roman", Font.BOLD, fontHeight);
  34. // 设置字体。
  35. gd.setFont(font);
  36. // 画边框。
  37. gd.setColor(Color.BLACK);
  38. gd.drawRect(0, 0, width - 1, height - 1);
  39. // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
  40. gd.setColor(Color.BLACK);
  41. for (int i = 0; i < 30; i++) {
  42. int x = random.nextInt(width);
  43. int y = random.nextInt(height);
  44. int xl = random.nextInt(12);
  45. int yl = random.nextInt(12);
  46. gd.drawLine(x, y, x + xl, y + yl);
  47. }
  48. StringBuffer randomCode = new StringBuffer();
  49. Map<String,Object> map = new HashMap<>();
  50. // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
  51. int red, green, blue;
  52. if (lx ==1){
  53. // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
  54. red = random.nextInt(255);
  55. green = random.nextInt(255);
  56. blue = random.nextInt(255);
  57. // 用随机产生的颜色将验证码绘制到图像中。
  58. gd.setColor(new Color(red, green, blue));
  59. gd.drawString(sessionCode, 18, codeY);
  60. randomCode.append(sessionCode);
  61. }else {
  62. // 随机产生codeCount数字的验证码。
  63. for (int i = 0; i < codeCount; i++) {
  64. // 得到随机产生的验证码数字。
  65. String code = String.valueOf(codeSequence[random.nextInt(52)]);
  66. // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
  67. red = random.nextInt(255);
  68. green = random.nextInt(255);
  69. blue = random.nextInt(255);
  70. // 用随机产生的颜色将验证码绘制到图像中。
  71. gd.setColor(new Color(red, green, blue));
  72. gd.drawString(code, (i + 1) * xx, codeY);
  73. // 将产生的四个随机数组合在一起。
  74. randomCode.append(code);
  75. }
  76. }
  77. //存放验证码
  78. map.put("code", randomCode);
  79. //存放生成的验证码BufferedImage对象
  80. map.put("codePic", buffImg);
  81. return map;
  82. }
  83. }