JAVA 验证码生成。SimpleCaptcha_java导入nl.captcha-程序员宅基地

技术标签: java  SimpleCaptcha  

去官方网站下载Jar包:

http://simplecaptcha.sourceforge.net/

Javadocs:

http://simplecaptcha.sourceforge.net/javadocs/index.html

自己书写工具类:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sino.gxh.util;


import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl.captcha.Captcha;
import nl.captcha.gimpy.FishEyeGimpyRenderer;
import nl.captcha.gimpy.RippleGimpyRenderer;
import nl.captcha.noise.CurvedLineNoiseProducer;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.producer.DefaultTextProducer;
import nl.captcha.text.renderer.ColoredEdgesWordRenderer;
import nl.captcha.text.renderer.WordRenderer;


/**
 *
 * @author Administrator
 */
public class CodeMaker {


    //验证码内容
    private char[] numberChar = new char[]{'a', 'b', 'c', 'd',
        'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    //验证码数量
    private int _CodeCount = 4;
    //验证码宽度
    private int _width = 110;
    //验证码高度
    private int _height = 50;
    //验证码颜色
    private Color _CodeColor = Color.BLACK;
    //使用字体名字
    private String _FontName = "System";
    //使用字体类型
    private int _FontType = Font.BOLD;
    //使用字体大小
    private int _FontSize = 40;
    //干扰线颜色
    private Color _NoiseColor = Color.BLACK;
    //干扰线大小
    private int _NoiseSize = 2;
    //干扰线条数
    private int _NoiseCount = 1;
    //验证图形码时是否开启大小写
    private boolean whetherOpenBigOrSmall = false;


    //验证码储存
    private String CodeMemory;


    //获取验证码
    public void getCode(HttpServletResponse resp) {
        Captcha.Builder captcha = new Captcha.Builder(_width, _height);
        List<Font> fontList = new ArrayList<Font>();
        List<Color> colorList = new ArrayList<Color>();
        colorList.add(_CodeColor);
        fontList.add(new Font(_FontName, _FontType, _FontSize));
        WordRenderer dwr = new ColoredEdgesWordRenderer(colorList, fontList);
        captcha.addText(new DefaultTextProducer(_CodeCount, numberChar), dwr);
        for (int i = 0; i < _NoiseCount; i++) {
            captcha.addNoise(new CurvedLineNoiseProducer(_NoiseColor, _NoiseSize));
        }
        captcha.gimp(new FishEyeGimpyRenderer(new Color(0, 0, 0, 0), new Color(0, 0, 0, 0)));
        captcha.gimp(new RippleGimpyRenderer());
        captcha.build();
        Captcha captchas = captcha.build();
        CaptchaServletUtil.writeImage(resp, captchas.getImage());
        CodeMemory = captchas.getAnswer();
    }


    //比较验证码
    public boolean compareCode(String Code) {
        if (null == Code || "".equals(Code)) {
            return false;
        } else {
            boolean bz;
            System.out.println(whetherOpenBigOrSmall);
            if (whetherOpenBigOrSmall) {
                bz = CodeMemory.equals(Code);
            } else {
                bz = CodeMemory.equalsIgnoreCase(Code);
            }
            return bz;
        }
    }


    public boolean isWhetherOpenBigOrSmall() {
        return whetherOpenBigOrSmall;
    }


    public void setWhetherOpenBigOrSmall(boolean whetherOpenBigOrSmall) {
        this.whetherOpenBigOrSmall = whetherOpenBigOrSmall;
    }


    public char[] getNumberChar() {
        return numberChar;
    }


    public void setNumberChar(char[] numberChar) {
        this.numberChar = numberChar;
    }


    public int getCodeCount() {
        return _CodeCount;
    }


    public void setCodeCount(int _CodeCount) {
        this._CodeCount = _CodeCount;
    }


    public int getWidth() {
        return _width;
    }


    public void setWidth(int _width) {
        this._width = _width;
    }


    public int getHeight() {
        return _height;
    }


    public void setHeight(int _height) {
        this._height = _height;
    }


    public Color getCodeColor() {
        return _CodeColor;
    }


    public void setCodeColor(Color _CodeColor) {
        this._CodeColor = _CodeColor;
    }


    public String getFontName() {
        return _FontName;
    }


    public void setFontName(String _FontName) {
        this._FontName = _FontName;
    }


    public int getFontType() {
        return _FontType;
    }


    public void setFontType(int _FontType) {
        this._FontType = _FontType;
    }


    public int getFontSize() {
        return _FontSize;
    }


    public void setFontSize(int _FontSize) {
        this._FontSize = _FontSize;
    }


    public Color getNoiseColor() {
        return _NoiseColor;
    }


    public void setNoiseColor(Color _NoiseColor) {
        this._NoiseColor = _NoiseColor;
    }


    public int getNoiseSize() {
        return _NoiseSize;
    }


    public void setNoiseSize(int _NoiseSize) {
        this._NoiseSize = _NoiseSize;
    }


    public int getNoiseCount() {
        return _NoiseCount;
    }


    public void setNoiseCount(int _NoiseCount) {
        this._NoiseCount = _NoiseCount;
    }


}


调用和比较:


    @RequestMapping(value = "/imagesanpeng", method = RequestMethod.GET)
    protected void imagesanpeng(HttpServletRequest req, HttpServletResponse resp)
            throws Exception {
        CodeMaker c = new CodeMaker();
        c.getCode(resp);
        req.getSession().setAttribute("code", c);
    }


    @RequestMapping(value = "/txmbj", method = RequestMethod.POST)
    protected void txmbj(HttpServletRequest req, HttpServletResponse resp,
            @RequestParam(value = "txyzm", required = true) String txyzm)
            throws Exception {
        CodeMaker c = (CodeMaker) req.getSession().getAttribute("code");
        c.setWhetherOpenBigOrSmall(true);
        resp.getWriter().print(c.compareCode(txyzm));
        resp.getWriter().flush();
        resp.getWriter().close();
//        CodeMaker c = new CodeMaker();
//        c.setWhetherOpenBigOrSmall(true);
//        resp.getWriter().print(c.compareCode(req, resp, txyzm));
//        resp.getWriter().flush();
//        resp.getWriter().close();
    }

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ufo2910628/article/details/41074369

智能推荐

react之antd组件InputNumber控制小数点_antd inputnumber decimalseparator-程序员宅基地

文章浏览阅读3.9k次。InputNumber控制用户输入小数点的个数在项目过程中有个需求,需要控制用户输入小数点的个数问题。话不多说上代码://输入框绑定方法<InputNumber style={{ width: '100%' }} formatter={limitDecimals} parser={limitDecimals}/>方法:const limitDecimals = (value: string | number): string => { const reg_antd inputnumber decimalseparator

matlab 约束条件下三元函数的图像问题_三元函数图像-程序员宅基地

文章浏览阅读4.1k次,点赞5次,收藏15次。题目:绘制z=x+y,0<x<1,0<y<1,0.5<x+y<1(0.5<z)约束条件下的图像代码:clc;clear;x=0:0.01:1;y=0:0.01:1;[xx,yy]=meshgrid(x,y);[m,n]=size(xx); for i=1:m for j=1:n z(i,j)=x(i)+y..._三元函数图像

linux下恢复误删除oracle的数据文件_oracle 数据文件误删 linux-程序员宅基地

文章浏览阅读480次。场景描述:操作系统级别的删除数据文件(/oracle/oradata/ora10g/system1.dbf),而且数据库没有崩溃,仍然处于open状态。原理:在Linux操作系统中,如果文件从操作系统级别被删除掉,之前打开该文件的进程仍然持有相应的文件句柄,所指向的文件仍然可以读写,并且该文件的文件描述符可以从/proc目录中获得。如果关闭数据库,则句柄就会丢失。恢复步骤如..._oracle 数据文件误删 linux

VScode 自定义代码颜色、背景颜色、方法名、括号颜色-程序员宅基地

文章浏览阅读10w+次,点赞108次,收藏323次。自学前端刚开始的时候使用Hbuilder,Hbuilder界面设计的很小清新,我特别喜欢,代码提示啥方面做的也特别好,很好上手,作为小白我用了很长一段时间。后来浅浅学习微信小程序开发,得写wxml,Hbilder上没有wxml格式的代码提示,代码高亮等。于是我用了一段时间vscode,在网上按推荐安装了很多花里胡哨的插件呢。给我感觉是不好上手。于是后来又接触了sublime,sublime相比..._vscode 自定义代码颜色、背景颜色、方法名、括号颜色

深度学习框架Tensorflow学习与应用 图像数据处理之二_tensorflow深度学习框架实现了对图像亮度的调整-程序员宅基地

文章浏览阅读255次。四:图像色彩调整 和图像翻转类似,调整图像的亮度、对比度、饱和度和色相在很多图像识别应用中都不会影响识别结果。所以在训练神经网络模型时,可以随机的调整训练图像的这些属性,从而使训练得到的模型尽可能地受到无关因素的影响。话不多说,上代码了。注意:路径要用英文,不要有中文(一)调整亮度与调整对比度import matplotlib.pyplot as pltimport tensorf..._tensorflow深度学习框架实现了对图像亮度的调整

《ASP.NET5》无法路由到Web API Controller控制器_.net的web无法访问到控制器-程序员宅基地

文章浏览阅读5.6k次。这个标题不知道恰当不恰当,具体的问题就是我在一个现有的项目上创建了一个Web API Controller Class,但是按F5启动调试后,通过给定的路径如“http://localhost:3753/api/values/5”访问Get(int id)方法时,没有反应,设置了断点也进不去。但这个问题在网上又没有找到解决方法,于是又新建了一个ASP.NET Web Application类型的项_.net的web无法访问到控制器

随便推点

嵌入式Linux开发板_迅为iTOP-4412精英版入门篇(一)_4412开发入门-程序员宅基地

文章浏览阅读911次,点赞2次,收藏4次。迅为iTOP-4412开发板平台,ARM Cortex A9架构,主频1.4GHz-1.6GHz,1GB 双通道 DDR3(2GB 可选),4GB EMMC(16GB 可选),提供多种外接模块,如:RFID模块、继电器模块、CAN总线 RS485总线模块、串口转接板、矩阵键盘模块、AVIN模块、GPS模块、VGA模块、500万摄像头模块、WIFI/蓝牙模块等。拥有丰富的板载接口以及众多配套扩展模块,并提供丰富的学习教程与资源,是嵌入式Linux学习与开发最佳选择。_4412开发入门

扫荡倾斜摄影单体化难题_进行模型单体化容易遇到哪些问题-程序员宅基地

文章浏览阅读5.2k次,点赞6次,收藏27次。单体化问题的由来 我们先来说说什么是“单体化”。“单体化”其实指的就是每一个我们想要单独管理的对象,是一个个单独的、可以被选中的实体(Entity);即用鼠标点击时可以显示为不同颜色(称为“高亮”)显示,可以附加属性,可以被查询统计等等。只有具备了“单体化”的能力,数据才可以被管理,而不仅仅是被用来查看。 对于人工建模而言,单体化是一个不言而喻的事情。即在人工建模的过程中,自然会把需要单独管理的_进行模型单体化容易遇到哪些问题

snakeyaml自定义pojo写入yml文件时属性字段排序问题_snakeyaml 写入yaml-程序员宅基地

文章浏览阅读1.1k次。snakeyaml自定义pojo写入yml文件时属性排序问题解决。_snakeyaml 写入yaml

使用jbpm出现异常 java.lang.NoClassDefFoundError: de/odysseus/el/ExpressionFactoryImpl-程序员宅基地

文章浏览阅读2.9k次。java.lang.NoClassDefFoundError: de/odysseus/el/ExpressionFactoryImpl at org.jbpm.pvm.internal.script.JuelScriptEngine.(JuelScriptEngine.java:66) at org.jbpm.pvm.internal.script.JuelS_java.lang.noclassdeffounderror: de/odysseus/el/expressionfactoryimpl

浅谈安全运营中心-程序员宅基地

文章浏览阅读1.9k次,点赞2次,收藏6次。最近两年安全运营中心这个概念被提到的次数越来越多了,虽然没有一个建设的标准模式,但很多大厂都在提,也号称有了各种落地。那么,到底是怎样呢。_安全运营中心

mysql 序列化缓存到txt文件查找数据与直接查找数据 性能对比,13,601条数据文件缓存平均0.085秒后只需0.025秒 推荐 程序员导航网http://www.je666.com_je666com-程序员宅基地

文章浏览阅读2.8k次。推荐 程序员导航网http://www.je666.comCREATE TABLE IF NOT EXISTS `ylmf_site_search` ( `id` int(11) NOT NULL, `displayorder` int(11) NOT NULL, `pinyin` varchar(255) CHARACTER SET gbk NOT NULL,_je666com

推荐文章

热门文章

相关标签