图片数字验证码生成
生成验证码
- controller层
@GetMapping("/image")
public String image(HttpServletRequest request, HttpServletResponse response){
RandomCodeTemp randomCodeTemp = new RandomCodeTemp();
randomCodeTemp.getRandomCode(request,response);
return String.valueOf(request.getSession());
}
- 实现层
public class RandomCodeTemp {
private Logger logger;
/**
* 图片流参数
*/
// 放到session中的key
public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";
// 随机产生数字和字母组合的字符串
private String randString = "1234568789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 图片宽 越小图片文字越大(当stringNum超过5时, width就需要放大,否则可能出现显示不全问题,75对应 4和5位验证码,90对应6位验证码)
private int width = 75;
// 图片高 越小图片文字越大
private int height = 20;
// 干扰线的数量
private int lineSize = 2;
// 随机产生字符串的数量
private int stringNum = 4;
// 验证码背景RGB参数
private Color backgroundColor = Color.white;
// 验证码字体
private Font font = new Font("Times New Roman", Font.ROMAN_BASELINE, 18);
// 干扰线字体
private Font drawFont = new Font("Fixedsys", Font.CENTER_BASELINE, 18);
// 安全的随机数对象
private SecureRandom random = new SecureRandom();
/**
* 获得干扰线字体
* @return 干扰线字体
*/
private Font getFont(){
return drawFont;
}
/**
* 获得颜色
* @param fc
* @param bc
* @return
*/
private Color getRandColor(int fc, int bc){
if (fc > 255){
fc = 255;
}
if (bc > 255){
bc = 255;
}
int r = fc + random.nextInt(bc - fc - 16);
int g = fc + random.nextInt(bc - fc - 14);
int b = fc + random.nextInt(bc - fc - 18);
return new Color(r,g,b);
}
/**
* 绘制干扰线
* @param g
*/
private void drawLine(Graphics g){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(13);
int y1 = random.nextInt(13);
g.drawLine(x, y, x + x1,y + y1);
}
/**
* 获取随机字符
* @param num
* @return
*/
public String getRandomString(int num){
return String.valueOf((randString.charAt(num)));
}
/**
* 绘制字符串
* @param g
* @param randomString
* @param i
* @return
*/
private String drawString(Graphics g, String randomString,int i){
g.setFont(getFont());
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
randomString += rand;
// 设置字符水平移动
// g.translate(random.nextInt(3), random.nextInt(3));
g.drawString(rand , 13 * i, 16);
return randomString;
}
/**
* 获得随机的字符
* @param request
* @param response
*/
public void getRandomCode(HttpServletRequest request, HttpServletResponse response){
HttpSession session = request.getSession();
// BUfferedImage类是具有缓存区的Image类,image类是用于描述图像信息的类
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
// 产生Image对象的GRaphics对象,该对象可以在图像上进行各种绘制操作
Graphics g = image.getGraphics();
// 设置背景透明
g.setColor(backgroundColor);
// 图片大小
g.fillRect(0, 0, width, height = height);
// 字体大小
g.setFont(font);
// 字体颜色
g.setColor(getRandColor(110, 133));
// 绘制干扰线
// 判断是否需要干扰线
if (lineSize > 0){
for (int i=0; i<=lineSize; i++){
drawLine(g);
}
}
// 绘制随机的字符
String randomString = "";
for (int i = 0; i < stringNum; i++) {
randomString = drawString(g, randomString, i);
}
// 将生成的随机字符串保存到session中
session.removeAttribute(RANDOMCODEKEY);
session.setAttribute(RANDOMCODEKEY, randomString);
// 验证码图片布置生成
g.dispose();
try {
ImageIO.write(image, "JPEG", response.getOutputStream());
}catch (Exception e){
logger.error("生成图片失效 ");
}
}
}
本文来自博客园,作者:ElloeStudy,转载请注明原文链接:https://www.cnblogs.com/ElloeStudy/p/17612414.html