easy-captcha 算法图片验证码去除结果小于0的计算

本次调整是基于自定义方式调整 来的,由于随机算法计算后由挺多的小于0,但是输入值时需要添加负号,比较麻烦,有多个业主反馈,所以,我进行优化调整,我这边解决方案比较简单,只要判断两个值大小并且是减号时,把减号调整为加号即可

代码如下



import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.awt.*;

/**
 * @description: 自定义图验证码
 * @program: datacenter
 * @author: zhaoshouyun
 * @create: 2022-03-25 14:27
 */
@Slf4j
public class CustomArithmeticCaptcha extends ArithmeticCaptcha {
    public CustomArithmeticCaptcha() {
        super();
    }

    public CustomArithmeticCaptcha(int width, int height) {
        super(width, height);
    }

    public CustomArithmeticCaptcha(int width, int height, int len) {
        super(width, height, len);
    }

    public CustomArithmeticCaptcha(int width, int height, int len, Font font) {
        super(width, height, len, font);
    }
    @Override
    protected char[] alphas() {
        StringBuilder sb = new StringBuilder();
        //定义获取减速和被减速变量
        int num1 = 0;
        int num2 = 0;
        //是否随机到减数标志位,默认false
        boolean flag = Boolean.FALSE;
        for (int i = 0; i < len; i++) {
            int num = num(10);
            sb.append(num);
            //进行赋值
            if (i == 0){
                num1 = num;
            }else {
                num2 = num;
            }

            if (i < len - 1) {
                int type = num(1, 4);
                if (type == 1) {
                    sb.append("+");
                } else if (type == 2) {
                    sb.append("-");
                    //当是减数时打上标记,进行后续处理
                    flag = true;
                } else if (type == 3) {
                    sb.append("x");
                }
            }

        }

        if (flag) {
            //不能出现差是小于0的结果,如果出现就直接变成+
            if (num1 < num2) {
               sb.replace(1,2,"+");
            }
        }
        ScriptEngineManager manager = new ScriptEngineManager();
        //添加这一行
        manager.registerEngineName("customScriptEngineFactory",new NashornScriptEngineFactory());
        ScriptEngine engine = manager.getEngineByName("javascript");
        try {
            chars = String.valueOf(engine.eval(sb.toString().replaceAll("x", "*")));
        } catch (ScriptException e) {
            e.printStackTrace();
        }
        sb.append("=?");
         setArithmeticString(sb.toString());
        return chars.toCharArray();
    }
}

以上代码就能解决出现结果为负数的情况,同时也不会去除减法

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Django中使用django-simple-captcha生成验证码可以通过以下步骤现: 1. 安装django-simple-captcha库: 在终端中运行以下命令安装django-simple-captcha库: ``` pip install django-simple-captcha ``` 2. 在Django项目的`settings.py`文件中添加`captcha`应用: 打开`settings.py`文件,找到`INSTALLED_APPS`列表,将`'captcha'`添加到其中。 3. 运行数据库迁移: 在终端中运行以下命令,将`captcha`应用的数据库迁移到你的项目中: ``` python manage.py migrate captcha ``` 4. 在需要生成验证码的表单中添加验证码字段: 在你的表单类中导入`CaptchaField`,并将其作为一个字段添加到表单中。例如: ```python from captcha.fields import CaptchaField class MyForm(forms.Form): # 其他字段... captcha = CaptchaField() ``` 5. 在视图函数中验证验证码: 在你的视图函数中,可以通过调用`form.is_valid()`来验证验证码是否正确。例如: ```python def my_view(request): if request.method == 'POST': form = MyForm(request.POST) if form.is_valid(): # 验证码正确,执行相应的逻辑 # ... else: # 验证码错误,处理错误信息 # ... else: form = MyForm() return render(request, 'my_template.html', {'form': form}) ``` 6. 在模板中显示验证码输入框: 在你的模板文件中,可以通过`form.captcha`来渲染验证码输入框。例如: ```html <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">提交</button> </form> ``` 这样,你就可以在Django中使用django-simple-captcha生成验证码了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值