Java生成验证码

桃扇骨 2023-10-06 20:26 87阅读 0赞
  1. package com.test;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.RenderedImage;
  7. import java.io.FileOutputStream;
  8. import java.io.OutputStream;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11. import java.util.Random;
  12. import javax.imageio.ImageIO;
  13. public class CodeUtil {
  14. private static int width = 90;// 定义图片的width
  15. private static int height = 20;// 定义图片的height
  16. private static int codeCount = 4;// 定义图片上显示验证码的个数
  17. private static int xx = 15;
  18. private static int fontHeight = 18;
  19. private static int codeY = 16;
  20. private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  21. /**
  22. * 生成一个map集合
  23. * code为生成的验证码
  24. * codePic为生成的验证码BufferedImage对象
  25. * @return
  26. */
  27. public static Map<String, Object> generateCodeAndPic() {
  28. // 定义图像buffer
  29. BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  30. // Graphics2D gd = buffImg.createGraphics();
  31. // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
  32. Graphics gd = buffImg.getGraphics();
  33. // 创建一个随机数生成器类
  34. Random random = new Random();
  35. // 将图像填充为白色
  36. gd.setColor(Color.WHITE);
  37. gd.fillRect(0, 0, width, height);
  38. // 创建字体,字体的大小应该根据图片的高度来定。
  39. Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
  40. // 设置字体。
  41. gd.setFont(font);
  42. // 画边框。
  43. gd.setColor(Color.BLACK);
  44. gd.drawRect(0, 0, width - 1, height - 1);
  45. // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
  46. gd.setColor(Color.BLACK);
  47. for (int i = 0; i < 30; i++) {
  48. int x = random.nextInt(width);
  49. int y = random.nextInt(height);
  50. int xl = random.nextInt(12);
  51. int yl = random.nextInt(12);
  52. gd.drawLine(x, y, x + xl, y + yl);
  53. }
  54. // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
  55. StringBuffer randomCode = new StringBuffer();
  56. int red = 0, green = 0, blue = 0;
  57. // 随机产生codeCount数字的验证码。
  58. for (int i = 0; i < codeCount; i++) {
  59. // 得到随机产生的验证码数字。
  60. String code = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
  61. // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
  62. red = random.nextInt(255);
  63. green = random.nextInt(255);
  64. blue = random.nextInt(255);
  65. // 用随机产生的颜色将验证码绘制到图像中。
  66. gd.setColor(new Color(red, green, blue));
  67. gd.drawString(code, (i + 1) * xx, codeY);// 最左侧字符的基线坐标,第一个字符的左下角坐标
  68. // 将产生的四个随机数组合在一起。
  69. randomCode.append(code);
  70. }
  71. Map<String, Object> map = new HashMap<String, Object>();
  72. // 存放验证码
  73. map.put("code", randomCode);
  74. // 存放生成的验证码BufferedImage对象
  75. map.put("codePic", buffImg);
  76. return map;
  77. }
  78. public static void main(String[] args) throws Exception {
  79. // 创建文件输出流对象
  80. OutputStream out = new FileOutputStream("G:/img/" + System.currentTimeMillis() + ".jpg");
  81. Map<String, Object> map = CodeUtil.generateCodeAndPic();
  82. ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
  83. System.out.println("验证码的值为:" + map.get("code"));
  84. }
  85. }

转载:Java实现验证码的产生和验证 - 南阳客 - 博客园

发表评论

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

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

相关阅读

    相关 Java生成图片验证

    功能介绍:自定义图片尺寸和字符长度,随机背景颜色和字符颜色,随机字符偏移角度,字符平滑边缘,干扰线,噪点,背景扭曲。 VerifyCodeUtils类,生成图片流,然后不同框