Java验证码与验证图片

编程入门 行业动态 更新时间:2024-10-10 08:27:46

Java<a href=https://www.elefans.com/category/jswz/34/1771040.html style=验证码与验证图片"/>

Java验证码与验证图片

      1.图片工具类

public class CreateVerificationCode {private String code;private Graphics g;public String getCode() {return code;}public void setCode(String code) {this.code = code;}public Graphics getG() {return g;}public void setG(Graphics g) {this.g = g;}/*** 获取随机生成的颜色** @param s* @param e* @return*/public Color getRandColor(int s, int e) {Random random = new Random();if (s > 255) {s = 91;}if (e > 255) {e = 97;}int r, g, b;r = s + random.nextInt(e - s);g = s + random.nextInt(e - s);b = s + random.nextInt(e - s);return new Color(r, g, b);}/*** 获取验证码图片*/public BufferedImage getIdentifyImg() {int width = 100;int height = 28;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//创建Graphics对象,相当于画笔Graphics g = image.getGraphics();//创建Graphics2D对象Graphics2D g2d = (Graphics2D) g;Random random = new Random();//定义字体样式Font font = new Font("华文宋体", Font.BOLD, 19);g.setColor(this.getRandColor(200, 250));g.fillRect(0, 0, width, height);g.setFont(font);g.setColor(this.getRandColor(180, 200));//绘制100条颜色和位置全部随机的线条 该线条为2ffor (int i = 0; i < 100; i++) {int x = random.nextInt(width - 1);int y = random.nextInt(height - 1);int x1 = random.nextInt(6) + 1;int y1 = random.nextInt(12) + 1;//绘制线条样式BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);Line2D line = new Line2D.Double(x, y, x1 + x, y1 + y);g2d.setStroke(bs);g2d.draw(line);}//输出由英文中文数字随机组成的验证码StringBuilder sRand = new StringBuilder();String ctmp = "";int itmp = 0;for (int i = 0; i < 4; i++) {switch (random.nextInt(3)) {case 1:case 2:itmp = random.nextInt(26) + 65;ctmp = String.valueOf((char) itmp);break;default:itmp = random.nextInt(10) + 48;ctmp = String.valueOf((char) itmp);break;}sRand.append(ctmp);Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), random.nextInt(110));g.setColor(color);g.drawString(ctmp, 19 * i + 19, 19);}this.setCode(sRand.toString());this.setG(g);return image;}
}

2.controller层对应操作 

@RestController
@RequestMapping("/util")
public class UtilController {static String CODE;/*** 生成随机验证码图片** @param response* @throws IOException*/@GetMapping("/getCodeImg")public void getIdentifyImage(HttpServletResponse response) throws IOException {//设置不缓存图片response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "No-cache");response.setDateHeader("Expires", 0);//指定生成的响应图片response.setContentType("image/jpeg");CreateVerificationCode code = new CreateVerificationCode();BufferedImage image = code.getIdentifyImg();code.getG().dispose();//将图形验证码IO流传输至前端ImageIO.write(image, "JPEG", response.getOutputStream());CODE = code.getCode();}@GetMapping("/getCode")public CommonResult<String> getCode() {return new CommonResult<>(200, CODE);}
}

3 .访问结果 

更多推荐

Java验证码与验证图片

本文发布于:2024-02-06 08:32:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1747651.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:验证码   图片   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!