前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >绘制验证码

绘制验证码

作者头像
晚上没宵夜
发布2020-04-24 15:17:41
5420
发布2020-04-24 15:17:41
举报

验证码功能只需复制粘贴即可,做个记录方便回看

1. 绘制验证码

代码语言:javascript
复制
public class VerifyCode {

    private int width = 100;
    private int height = 50;
    private int CODE_COUNT = 4;
    private int LINE_COUNT = 5;
    private String[] FONTNAMES = {"宋体", "楷体", "隶书", "微软雅黑"};
    private String CODES = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private Random random = new Random();
    private String code;

    // 随机RGB,RGB不易过高
    private Color randomColor() {
        int red = random.nextInt(150);
        int green = random.nextInt(150);
        int blue = random.nextInt(150);
        return new Color(red, green, blue);
    }

    // 随机字体
    private Font randomFont() {
        String name = FONTNAMES[random.nextInt(FONTNAMES.length)];
        int style = random.nextInt(4);
        int size = random.nextInt(5) + 25;
        return new Font(name, style, size);
    }

    // 随机字符
    private char randomChar() {
        return CODES.charAt(random.nextInt(CODES.length()));
    }

    // 返回绘制的验证码图片
    public BufferedImage getImage() {
        // 绘制背景
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(new Color(250, 250, 250));
        g2.fillRect(0, 0, width, height);

        // 绘制验证码
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < CODE_COUNT; i++) {
            String s = randomChar() + "";
            sb.append(s);
            g2.setColor(randomColor());
            g2.setFont(randomFont());
            float x = i * width / CODE_COUNT;
            float y = height / 2 + random.nextInt(height / 4);
            g2.drawString(s, x, y);
        }
        
        // 绘制干扰线
        for (int i = 0; i < LINE_COUNT; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g2.setColor(randomColor());
            g2.setStroke(new BasicStroke(1));
            g2.drawLine(x1, y1, x2, y2);
        }
        this.code = sb.toString();
        return image;
    }

    // 获取验证码
    public String getCode() {
        return code;
    }

    // 输出图片
    public static void output(BufferedImage image, OutputStream out) throws IOException {
        ImageIO.write(image, "JPEG", out);
    }
}

2. Controller层

代码语言:javascript
复制
@RestController
public class VerifyCodeController {
    @GetMapping("/verifyCode")
    public void verifyCode(HttpServletRequest req, HttpServletResponse res) throws IOException {
        VerifyCode vc = new VerifyCode();
        BufferedImage image = vc.getImage();
        String code = vc.getCode();
        HttpSession session = req.getSession();
        session.setAttribute("verifyCode", code);
        VerifyCode.output(image, res.getOutputStream());
    }
}

3. 页面

代码语言:javascript
复制
<img src="/verifyCode" alt="看不清楚,点击换一张">

4. 测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 绘制验证码
  • 2. Controller层
  • 3. 页面
  • 4. 测试
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档