Pingchas 发表于 2025-9-18 09:25:27

[Java]《实现算术验证码》

本帖最后由 Pingchas 于 2025-9-18 09:29 编辑

配置参数说明对于一张验证码图片来说,我们如何控制验证码图片的样式呢?这就是kaptcha提供的配置参数的意义。首先,它本质是一张图片,所以将会涉及图片的边框、宽高、背景颜色验证码是字符,这将会涉及到字体类型、字体大小、字体颜色、字体间距、字体数量验证码的另一个重要功能是干扰,这将会涉及干扰类型、干扰样式。


import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class KaptchaConfig {
    @Bean    
public DefaultKaptcha getDefaultKaptcha(){       
        DefaultKaptcha defaultKaptcha=new DefaultKaptcha();       
        Properties properties=new Properties();       
        properties.setProperty("kaptcha.border", "no");       
        properties.setProperty("kaptcha.border.color", "34,114,200");       
        properties.setProperty("kaptcha.image.width", "200");       
        properties.setProperty("kaptcha.image.height", "50");       
         //properties.setProperty("kaptcha.textproducer.char.string", "0123456789");       
        properties.setProperty("kaptcha.textproducer.char.length", "6");       
        properties.setProperty("kaptcha.textproducer.font.names", "Arial,Arial Narrow,Serif,Helvetica,Tahoma,Times New Roman,Verdana");
                properties.setProperty("kaptcha.textproducer.font.size", "38");
               properties.setProperty("kaptcha.background.clear.from", "white");       
        properties.setProperty("kaptcha.background.clear.to", "white");
                Config config=new Config(properties);       
        defaultKaptcha.setConfig(config);        
        return defaultKaptcha;   
        }
}



参数 | 说明 | 可选值
kaptcha.border |是否显示隐藏边框 | yes,no
   kaptcha.border.color |边框颜色| 颜色字符串,如:34,114,200
kaptcha.image.width | 验证码图片宽度 |宽度值
kaptcha.image.height |验证码图片高度 |高度值
kaptcha.textproducer.char.length| 字符数量 | 字符的数量
kaptcha.textproducer.font.names | 字符字体 | 字符字体名称列表
kaptcha.textproducer.font.size |字体大小 | 字体的大小

            

maven依赖坐标
<dependency>                
        <groupId>pro.fessional</groupId>                
        <artifactId>kaptcha</artifactId>                
        <version>2.3.3</version>            
</dependency>

import java.util.Random;import com.google.code.kaptcha.text.impl.DefaultTextCreator;
/** * 验证码文本生成器 * */public class KaptchaTextCreator extends DefaultTextCreator{    private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
    @Override    public String getText()    {        Integer result = 0;        Random random = new Random();        int x = random.nextInt(10);        int y = random.nextInt(10);        StringBuilder suChinese = new StringBuilder();        int randomoperands = random.nextInt(3);        if (randomoperands == 0)        {            result = x * y;            suChinese.append(CNUMBERS);            suChinese.append("*");            suChinese.append(CNUMBERS);        }        else if (randomoperands == 1)        {            if ((x != 0) && y % x == 0)            {                result = y / x;                suChinese.append(CNUMBERS);                suChinese.append("/");                suChinese.append(CNUMBERS);            }            else            {                result = x + y;                suChinese.append(CNUMBERS);                suChinese.append("+");                suChinese.append(CNUMBERS);            }        }        else        {            if (x >= y)            {                result = x - y;                suChinese.append(CNUMBERS);                suChinese.append("-");                suChinese.append(CNUMBERS);            }            else            {                result = y - x;                suChinese.append(CNUMBERS);                suChinese.append("-");                suChinese.append(CNUMBERS);            }        }        suChinese.append("=?@" + result);        return suChinese.toString();    }}

微尘 发表于 2025-9-19 19:13:56

挺有用的,支持一下!

H.U.C清风 发表于 2025-9-20 13:26:42

确实挺有用
页: [1]
查看完整版本: [Java]《实现算术验证码》