java验证码开发示例

验证码是一种区分用户是计算机还是人的公共全自动程序;可以防止恶意破解密码、刷票、论坛灌水、刷页、恶意注册、登录。

直接上代码

public static void main(String[] args) throws IOException {
        Random rand = new Random();
        //画板
        BufferedImage i = new BufferedImage(200, 100, 1);
        //画笔
        var m = i.createGraphics();
        //平滑度
        m.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        m.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        m.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        m.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(150, 200)));
        m.fillRect(0, 0, 200, 100);
        Font font = new Font("宋体", Font.BOLD, 50);
        m.setFont(font);
        var fm = m.getFontMetrics(font);
        //验证字符
        String setter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        //生成随机验证码
        for (int j = 0; j < 6; j++) {
            //随机颜色
            m.setColor(new Color(rand.nextInt(120, 255), rand.nextInt(120, 255), rand.nextInt(120, 255), 255));
            int x = j * 30 + 10;
            //验证字符位置
            int y1 = i.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2 - fm.getHeight() / 3;
            int y2 = i.getHeight() / 2 + (fm.getAscent() - fm.getDescent()) / 2 + fm.getHeight() / 3;
            int a = rand.nextInt(setter.length());
            font.deriveFont(rand.nextInt(45, 55));
            var s = setter.substring(a, a + 1);
            m.drawString(s, x, rand.nextInt(y1, y2));
            sb.append(s);
        }

        //验证码干扰
        for (int j = 0; j < 10; j++) {
            m.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(50, 120)));
            int a = rand.nextInt(setter.length());
            var s = setter.substring(a, a + 1);
            m.drawString(s, rand.nextInt(10, 190), rand.nextInt(10, 90));
        }

        for (int j = 0; j < 3; j++) {
            m.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(100, 200)));
            m.setStroke(new BasicStroke(3));
            m.drawLine(rand.nextInt(10, 20), rand.nextInt(20, 90), rand.nextInt(170, 190), rand.nextInt(20, 90));

        }
        //曲线干扰
        Point[] points = {new Point(0, 0), new Point(0, rand.nextInt(100)), new Point(200, rand.nextInt(100))

        };

        GeneralPath path = new GeneralPath();
        path.moveTo(points[0].x, points[0].y);
        for (int i2 = 0; i2 < points.length - 1; ++i2) {
            Point sp = points[i2];
            Point ep = points[i2 + 1];
            Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
            Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
            path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
        }

        // g.draw(path);
        for (int n = 0; n <= 355; n += 10) {
            // g.drawArc(100, 200, 600, 300, n, 5);
        }

        int yy = 100;// y
        int hh = 100;// 高度
        int aa = 200;//
        // x
        for (int x = 10; x < 200; x += 15) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            //g.drawLine(x, (int) y, x, (int) y);
            m.fillOval(x, y, 6, 6);
        }

        m.dispose();
        ImageIO.write(i, "jpg", new File("yanzheng.jpg"));
        Runtime.getRuntime().exec("cmd /k start yanzheng.jpg");

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值