前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java数字和中文算数验证码

java数字和中文算数验证码

作者头像
崔笑颜
发布2020-07-15 09:46:06
1.5K0
发布2020-07-15 09:46:06
举报
image.png
image.png

代码如下

中文算法

代码语言:javascript
复制
package com.example.captcha;

import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author cuixy
 * @datetime 2020/7/9 20:10
 * @description
 */
@Slf4j
public class ChineseCaptcha {
    // 宽度
    private static final int IMAGE_WIDTH = 160;
    // 高度
    private static final int IMAGE_HEIGHT = 40;
    // 字体大小
    private static final int FONT_SIZE = 28;
    // 干扰线数量
    private static final int IMAGE_DISTURB_LINE_NUMBER = 15;
    // 后缀
    private static final String SUFFIX = "等于?";

    // 汉字数字
    private static final String SOURCE = "零一二三四五六七八九十乘除加减";

    // 计算类型
    private static final Map<String, Integer> calcType = new HashMap<>();
    private static final Random RANDOM = new Random();

    static {
        // 对应SOURCE下标
        calcType.put("*", 11);
        calcType.put("/", 12);
        calcType.put("+", 13);
        calcType.put("-", 14);
    }

    // 计算公式
    private String content;
    // 计算结果
    private int result;

    /**
     * 生成图像验证码
     */
    public BufferedImage create() {
        createMathChar();
        BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // 字体颜色
        g.setColor(Color.WHITE);
        // 背景颜色
        g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
        g.setColor(color());
        // 图片边框
        g.drawRect(0, 0, IMAGE_WIDTH - 1, IMAGE_HEIGHT - 1);
        g.setColor(color());
        for (int i = 0; i < IMAGE_DISTURB_LINE_NUMBER; i++) {
            // 绘制干扰线
            int x1 = RANDOM.nextInt(IMAGE_WIDTH);
            int y1 = RANDOM.nextInt(IMAGE_HEIGHT);
            int x2 = RANDOM.nextInt(13);
            int y2 = RANDOM.nextInt(15);
            g.drawLine(x1, y1, x1 + x2, y1 + y2);
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0, j = content.length(); i < j; i++) {
            int index;
            if (i == 1) {
                index = calcType.get(String.valueOf(content.charAt(i)));
            } else {
                index = Integer.parseInt(String.valueOf(content.charAt(i)));
            }
            String ch = String.valueOf(SOURCE.charAt(index));
            builder.append(ch);
            drawString((Graphics2D) g, ch, i);
        }
        drawString((Graphics2D) g, SUFFIX, 3);
        content = builder.append(SUFFIX).toString();
        g.dispose();
        return image;
    }

    private void createMathChar() {
        StringBuilder number = new StringBuilder();
        // 10以内数字
        int xx = RANDOM.nextInt(10);
        int yy = RANDOM.nextInt(10);
        // 0~3 对应加减乘除
        int round = (int) Math.round(Math.random() * 3);
        if (round == 0) {
            this.result = yy + xx;
            number.append(yy);
            number.append("+");
            number.append(xx);
        } else if (round == 1) {
            this.result = yy - xx;
            number.append(yy);
            number.append("-");
            number.append(xx);
        } else if (round == 2) {
            this.result = yy * xx;
            number.append(yy);
            number.append("*");
            number.append(xx);
        } else {
            // 0不可为被除数 yy对xx取余无余数
            if (!(xx == 0) && yy % xx == 0) {
                this.result = yy / xx;
                number.append(yy);
                number.append("/");
                number.append(xx);
            } else {
                this.result = yy + xx;
                number.append(yy);
                number.append("+");
                number.append(xx);
            }
        }
        this.content = number.toString();
        log.info("数字计算公式 {}", this.content);
    }

    private void drawString(Graphics2D g2d, String s, int i) {
        g2d.setFont(new Font("黑体", Font.BOLD, FONT_SIZE));
        int r = RANDOM.nextInt(255);
        int g = RANDOM.nextInt(255);
        int b = RANDOM.nextInt(255);
        g2d.setColor(new Color(r, g, b));
        int x = RANDOM.nextInt(3);
        int y = RANDOM.nextInt(2);
        g2d.translate(x, y);
        int angle = new Random().nextInt() % 15;
        g2d.rotate(angle * Math.PI / 180, 5 + i * 25, 20);
        g2d.drawString(s, 5 + i * 25, 30);
        g2d.rotate(-angle * Math.PI / 180, 5 + i * 25, 20);
    }

    // 获取随机颜色
    private Color color() {
        int r = RANDOM.nextInt(256);
        int g = RANDOM.nextInt(256);
        int b = RANDOM.nextInt(256);
        return new Color(r, g, b);
    }

    public String content() {
        return content;
    }

    public int result() {
        return result;
    }
}

数字算法 此代码使用了ttf字体 字体在此处 https://gitee.com/tothis/java-record/tree/master/src/main/resources/font

代码语言:javascript
复制
package com.example.captcha;

import lombok.SneakyThrows;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author cuixy
 * @datetime 2020/7/9 20:16
 * @description
 */
public class NumberCaptcha {
    // 宽度
    private static final int IMAGE_WIDTH = 160;
    // 高度
    private static final int IMAGE_HEIGHT = 40;
    // 字体大小
    private static final int FONT_SIZE = 28;
    // 字体目录
    private static final String FONT_PATH = "/font/";
    // 字体列表
    private static final String[] FONT_NAMES = {
            "actionj.ttf", "epilog.ttf", "headache.ttf"
            , "lexo.ttf", "prefix.ttf", "robot.ttf"
    };

    private static final Random RANDOM = new Random();
    // 计算公式
    private String content;
    // 计算结果
    private int result;

    /**
     * 生成随机验证码
     */
    private void createMathChar() {
        StringBuilder number = new StringBuilder();
        int xx = RANDOM.nextInt(10);
        int yy = RANDOM.nextInt(10);
        // 0~3 对应加减乘除
        int round = (int) Math.round(Math.random() * 3);
        if (round == 0) {
            this.result = yy + xx;
            number.append(yy);
            number.append("+");
            number.append(xx);
        } else if (round == 1) {
            this.result = yy - xx;
            number.append(yy);
            number.append("-");
            number.append(xx);
        } else if (round == 2) {
            this.result = yy * xx;
            number.append(yy);
            number.append("x");
            number.append(xx);
        } else {
            // 0不可为被除数 yy对xx取余无余数
            if (!(xx == 0) && yy % xx == 0) {
                this.result = yy / xx;
                number.append(yy);
                number.append("/");
                number.append(xx);
            } else {
                this.result = yy + xx;
                number.append(yy);
                number.append("+");
                number.append(xx);
            }
        }

        content = number.append("=?").toString();
    }

    // 获取随机颜色
    private Color color() {
        int r = RANDOM.nextInt(256);
        int g = RANDOM.nextInt(256);
        int b = RANDOM.nextInt(256);
        return new Color(r, g, b);
    }

    /**
     * 随机画干扰圆
     *
     * @param num 数量
     * @param g   Graphics2D
     */
    private void drawOval(int num, Graphics2D g) {
        for (int i = 0; i < num; i++) {
            g.setColor(color());
            int j = 5 + RANDOM.nextInt(10);
            g.drawOval(RANDOM.nextInt(IMAGE_WIDTH - 25), RANDOM.nextInt(IMAGE_HEIGHT - 15), j, j);
        }
    }

    @SneakyThrows
    private Font font() {
        int index = RANDOM.nextInt(FONT_NAMES.length);
        Font font = Font.createFont(Font.TRUETYPE_FONT
                , getClass().getResourceAsStream(FONT_PATH + FONT_NAMES[index]))
                .deriveFont(Font.BOLD, FONT_SIZE);
        return font;
    }

    /**
     * 生成验证码图形
     */
    public BufferedImage create() {
        createMathChar();
        char[] chars = content.toCharArray();
        BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) image.getGraphics();
        // 填充背景
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 画干扰圆
        drawOval(2, g2d);
        // 画字符串
        g2d.setFont(font());
        FontMetrics fontMetrics = g2d.getFontMetrics();
        // 每一个字符所占的宽度
        int fx = IMAGE_WIDTH / chars.length;
        // 字符左填充
        int flp = 10;
        for (int i = 0; i < chars.length; i++) {
            String item = String.valueOf(chars[i]);
            g2d.setColor(color());
            // 文字的纵坐标
            int fy = IMAGE_HEIGHT - ((IMAGE_HEIGHT - (int) fontMetrics
                    .getStringBounds(item, g2d).getHeight()) >> 1);
            g2d.drawString(item, flp + i * fx + 3, fy - 3);
        }
        g2d.dispose();
        return image;
    }

    public String content() {
        return content;
    }

    public int result() {
        return result;
    }
}

测试类

代码语言:javascript
复制
package com.example.captcha;

import com.greenpineyu.fel.FelEngine;
import com.greenpineyu.fel.FelEngineImpl;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @author cuixy
 * @datetime 2020/7/9 20:10
 * @description
 */
public class Main {
    @SneakyThrows
    public static void main(String[] args) {
        // 图片类型
        String IMAGE_TYPE = "png";
        ChineseCaptcha chinese = new ChineseCaptcha();
        NumberCaptcha number = new NumberCaptcha();

        for (int i = 0; i < 10; i++) {
            BufferedImage image1 = chinese.create();
            // 输出文件
            ImageIO.write(image1, IMAGE_TYPE, new File("D:/data/a" + i + ".png"));
            System.out.println(chinese.content() + ' ' + chinese.result());

            BufferedImage image2 = number.create();
            // 输出文件
            ImageIO.write(image2, IMAGE_TYPE, new File("D:/data/b" + i + ".png"));
            System.out.println(number.content() + ' ' + number.result());
        }
    }
}

如需要多级运算如1+2-3*4/5 则需要使用表达式引擎计算 如下为使用fel计算表达式

代码语言:javascript
复制
// fel计算表达式
FelEngine fel = new FelEngineImpl();
System.out.println(3 * 2 + 1);
System.out.println(fel.eval("3*2+1"));
System.out.println(3 * (2 + 1));
System.out.println(fel.eval("3*(2+1)"));
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-13,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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