Java实现图形验证码

Java实现图形验证码实现类
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class ImageVerificationCode {
/**
* 验证码图⽚的长
*/
private final int weight = 100;
/**
* 验证码图⽚的宽
*/
private final int height = 40;
/**
* 获取随机数对象
*/
private final Random r = new Random();
/**
* 字体数组 {"宋体", "华⽂楷体", "⿊体", "微软雅⿊", "楷体_GB2312"};
*/
private final String[] fontNames = {"Georgia"};
/**
92gan* ⽤来保存验证码的⽂本内容
*/
private String text;
/**
* 将验证码图⽚写出的⽅法
* @param image
* @param out
* @throws IOException
*/
public static void output(BufferedImage image, OutputStream out) throws IOException {
ImageIO.write(image, "JPEG", out);
}
/**
* 获取随机的颜⾊
*
* @return
*/
private Color randomColor() {
//这⾥为什么是225,因为当r,g,b都为255时,即为⽩⾊,为了好辨认,需要颜⾊深⼀点。
int r = Int(225);
int g = Int(225);
int b = Int(225);
//返回⼀个随机颜⾊
return new Color(r, g, b);
}
/**
* 获取随机字体
*
* @return
*/
private Font randomFont() {
//获取随机的字体
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
//随机获取字体的样式,0是⽆样式,1是加粗,2是斜体,3是加粗加斜体
int style = r.nextInt(4);
//随机获取字体的⼤⼩
int size = r.nextInt(10) + 24;
//返回⼀个随机的字体
return new Font(fontName, style, size);
}
/
**
* 获取随机字符
*
* @return
*/
private char randomChar() {
鸳鸯电火锅
//验证码数组
String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
/
**
* 画⼲扰线,验证码⼲扰线⽤来防⽌计算机解析图⽚
*
* @param image
*/
private void drawLine(BufferedImage image) {
//定义⼲扰线的数量
int num = r.nextInt(10);
Graphics2D g = (Graphics2D) Graphics();
for (int i = 0; i < num; i++) {电子表单系统
int x1 = r.nextInt(weight);
int y1 = r.nextInt(height);
int x2 = r.nextInt(weight);
int y2 = r.nextInt(height);
g.setColor(randomColor());
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 创建图⽚的⽅法
*
* @return
*/
private BufferedImage createImage() {
//创建图⽚缓冲区
BufferedImage image = new BufferedImage(weight, height, BufferedImage.TYPE_INT_RGB);
//获取画笔
Graphics2D g = (Graphics2D) Graphics();
//设置背景⾊随机
g.setColor(new Color(255, 255, r.nextInt(245) + 10));
g.fillRect(0, 0, weight, height);
//返回⼀个图⽚
return image;
}
/**
* 获取验证码图⽚的⽅法
*
* @return
*/
public BufferedImage getImage() {
BufferedImage image = createImage();
//获取画笔
Graphics2D g = (Graphics2D) Graphics();
StringBuilder sb = new StringBuilder();
/
/画四个字符即可
for (int i = 0; i < 4; i++) {
//随机⽣成字符,因为只有画字符串的⽅法,没有画字符的⽅法,所以需要将字符变成字符串再画            String s = randomChar() + "";
//添加到StringBuilder⾥⾯
sb.append(s);
//定义字符的x坐标
float x = i * 1.0F * weight / 4;
//设置字体,随机
g.setFont(randomFont());
//设置颜⾊,随机
g.setColor(randomColor());
g.drawString(s, x, height - 5);
}
< = sb.toString();
drawLine(image);
return image;
}
/**
* 获取验证码⽂本的⽅法
*
* @return
食品烤箱*/
public String getText() {
return text;
}
}
调⽤⽅法
@ApiOperation("获取图⽚验证码")
@GetMapping("/getPictureValidationCode")
@ResponseBody
@Override
@ApiHeaderCheck
public void getPictureValidationCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
//获取图形验证码
ImageVerificationCode imageVerificationCode = new ImageVerificationCode();
BufferedImage image = Image();
//将验证码的⽂本存在session中
//将验证码图⽚响应给客户端
ImageVerificationCode.output(image, OutputStream());
}
@ApiOperation("校验图⽚验证码")
@GetMapping("/checkPictureValidationCode")
@ResponseBody
@Override
@ApiHeaderCheck
数模转换
public CommonResult<Boolean> checkPictureValidationCode(HttpServletRequest request, HttpServletResponse response, @RequestParam String validationCode) {        String rightValidationCode = (String) Session().getAttribute("validationCode");
if (StringUtils.isEmpty(rightValidationCode) || !LowerCase().LowerCase())) {
return CommonResult.<Boolean>builder().result(false).build();
}
//重置今⽇该ip请求验证码次数
String ipAddress = IPAddress(request);
Long zeroTimeWithTimeZone = ZeroTimeWithTimeZone(null);
String key = ipAddress + "_VALIDATE_CODE_TIMES_" + zeroTimeWithTimeZone;
redisUtil2.set(key, 0, DateUtils.SECOND_OF_DAY);
return CommonResult.<Boolean>builder().result(true).build();
}
前端获取
1.⾸先在http请求⾥,加上responseType: 'blob',如:
然后:将返回的blob类型的图⽚转成base64
let blob = res.data;
let reader = new FileReader();
电子加速器辐照
_this.url= sult

本文发布于:2024-09-23 05:28:34,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/219071.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   获取   字符   字体   电子   扰线
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议