服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - JAVA教程 - java验证码生成具体代码

java验证码生成具体代码

2020-04-20 13:57卖蜡笔的小新 JAVA教程

这篇文章主要为大家分享了java验证码生成具体代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java验证码生成的示例代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.gonvan.component.captcha;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
/**
 * Created by yuerzm on 2016/3/14.
 */
public class CaptchaFactory {
 
  private static final char[]   CODE_SEQUENCE    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    .toCharArray();
  private static final int    DEFAULT_WIDTH    = 60;
  private static final int    DEFAULT_HEIGHT   = 20;
  private static final int    DEFAULT_CODE_LEN  = 4;
  private static final int    DEFAULT_CODE_X   = 13;
  private static final int    DEFAULT_CODE_Y   = 16;
  private static final int    DEFAULT_FONT_SIZE  = 18;
  private static final String   DEFAULT_FONT_FAMILY = "Times New Roman";
  private static CaptchaFactory  instance      = new CaptchaFactory();
  private int           width        = DEFAULT_WIDTH;            // 定义图片的width
  private int           height       = DEFAULT_HEIGHT;            // 定义图片的height
  private int           length       = DEFAULT_CODE_LEN;           // 定义图片上显示验证码的个数
  private int           xx         = DEFAULT_CODE_X;            // 定义图片上显示验证码x坐标
  private int           yy         = DEFAULT_CODE_Y;            // 定义图片上显示验证码y坐标
  private int           fontSize      = DEFAULT_FONT_SIZE;          // 定义图片上显示验证码的字体大小
  private String         fontFamily     = DEFAULT_FONT_FAMILY;         // 定义图片上显示验证码的个数
 
  private CaptchaFactory() {
  }
 
  public static CaptchaFactory getInstance() {
    return instance;
  }
 
  /**
   * 配置宽高
   *
   * @param w
   * @param h
   * @return
   */
  public CaptchaFactory configWidthAndHeight(int w, int h) {
    instance.width = w;
    instance.height = h;
    return instance;
  }
 
  /**
   * 配置坐标
   *
   * @param x
   * @param y
   * @return
   */
  public CaptchaFactory configXY(int x, int y) {
    instance.xx = x;
    instance.yy = y;
    return instance;
  }
 
  /**
   * 配置字体大小
   *
   * @param fontSize
   * @return
   */
  public CaptchaFactory configFontSize(int fontSize) {
    instance.fontSize = fontSize;
    return instance;
  }
 
  /**
   * 配置字体
   *
   * @param fontFamily
   * @return
   */
  public CaptchaFactory configFontSize(String fontFamily) {
    instance.fontFamily = fontFamily;
    return instance;
  }
 
  public void write(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // 将四位数字的验证码保存到Session中。
    Map captcha = generate();
    String randomCode = (String) captcha.get("captchaCode");
    BufferedImage buffImg = (BufferedImage) captcha.get("captchaImg");
 
    HttpSession session = request.getSession();
    session.setAttribute("code", randomCode);
 
    // 禁止图像缓存。
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");
 
    // 将图像输出到Servlet输出流中。
    ServletOutputStream outputStream = response.getOutputStream();
    ImageIO.write(buffImg, "jpeg", outputStream);
    outputStream.close();
  }
 
  public Map<String, Object> generate() throws IOException {
    // 定义图像buffer
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics gd = buffImg.getGraphics();
    // 设定背景色
    gd.setColor(getRandColor(200, 250));
    gd.fillRect(0, 0, width, height);
    // 设定字体,字体的大小应该根据图片的高度来定。
    gd.setFont(new Font(fontFamily, Font.PLAIN, fontSize));
 
    // 创建一个随机数生成器类
    Random random = new Random();
 
    // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
    gd.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
      int x = random.nextInt(width);
      int y = random.nextInt(height);
      int xl = random.nextInt(12);
      int yl = random.nextInt(12);
      gd.drawLine(x, y, x + xl, y + yl);
    }
 
    // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
    StringBuffer randomCode = new StringBuffer();
    int red = 0, green = 0, blue = 0;
 
    // 随机产生 length 个验证码。
    for (int i = 0; i < length; i++) {
      // 得到随机产生的验证码数字。
      String code = String.valueOf(CODE_SEQUENCE[random.nextInt(36)]);
      // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
      red = random.nextInt(110);
      green = random.nextInt(110);
      blue = random.nextInt(110);
 
      // 用随机产生的颜色将验证码绘制到图像中。
      gd.setColor(new Color(red + 20, green + 20, blue + 20));
      gd.drawString(code, i * xx + 6, yy);
 
      // 将产生的随机数组合在一起。
      randomCode.append(code);
    }
    Map<String, Object> retval = new HashMap<>();
    retval.put("captchaCode", randomCode.toString());
    retval.put("captchaImg", buffImg);
    return retval;
  }
 
  /**
   * 给定范围获得随机颜色
   *
   * @param fc
   *      最小值
   * @param bc
   *      最大值
   * @return Color
   */
  private Color getRandColor(int fc, int bc) {
    Random random = new Random();
    if (fc > 255)
      fc = 255;
    if (bc > 255)
      bc = 255;
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);
    return new Color(r, g, b);
  }
}

 

以上就是本文的全部内容,希望对大家的学习有所帮助。

延伸 · 阅读

精彩推荐