设为首页收藏本站
查看: 123|回复: 7

[高级语言] [Java]《实现算术验证码》

[复制链接]
  • TA的每日心情
    奋斗
    前天 12:57
  • 签到天数: 1005 天

    [LV.10]以坛为家III

    发表于 4 天前 | 显示全部楼层 |阅读模式
    本帖最后由 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&#160roperties();       
            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>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#160;
            <groupId>pro.fessional</groupId>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#160;
            <artifactId>kaptcha</artifactId>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;&#160;
            <version>2.3.3</version>&#160; &#160; &#160; &#160; &#160; &#160;&#160;
    </dependency>

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

    点评

    谢谢分享  发表于 3 天前
    支持一下  发表于 3 天前
  • TA的每日心情
    开心
    昨天 14:01
  • 签到天数: 638 天

    [LV.9]以坛为家II

    发表于 3 天前 | 显示全部楼层
    谢谢分享,已回复。
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    3 小时前
  • 签到天数: 94 天

    [LV.6]常住居民II

    发表于 3 天前 | 显示全部楼层
    谢谢分享,支持一下!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    3 小时前
  • 签到天数: 94 天

    [LV.6]常住居民II

    发表于 3 天前 | 显示全部楼层
    挺有用的,支持一下!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    昨天 14:01
  • 签到天数: 638 天

    [LV.9]以坛为家II

    发表于 前天 13:25 | 显示全部楼层
    来支持一下
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    昨天 14:01
  • 签到天数: 638 天

    [LV.9]以坛为家II

    发表于 前天 13:26 | 显示全部楼层
    确实挺有用
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    红盟社区--红客联盟 

    Processed in 0.073199 second(s), 22 queries.

    站点统计| 举报| Archiver| 手机版| 黑屋 |   

    备案号:冀ICP备20006029号-1 Powered by HUC © 2001-2021 Comsenz Inc.

    手机扫我进入移动触屏客户端

    关注我们可获取更多热点资讯

    Honor accompaniments. theme macfee

    快速回复 返回顶部 返回列表