去官方网站下载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();

    }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

JAVA 代码生成。SimpleCaptcha的更多相关文章

  1. java代码生成二维码

    java代码生成二维码一般步骤 常用的是Google的Zxing来生成二维码,生成的一般步骤如下: 一.下载zxing-core的jar包: 二.需要创建一个MatrixToImageWriter类, ...

  2. java代码生成二维码以及解析二维码

    package com.test; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedI ...

  3. Android 通过Java代码生成创建界面。动态生成View,动态设置View属性。addRules详解

    废话不多说,本文将会层层深入给大家讲解如何动态的生成一个完整的界面. 本文内容: Java代码中动态生成View Java代码中动态设置View的位置,以及其他的属性 LayoutParams详解 一 ...

  4. 如何将java代码生成一个bat文件

    java -cp classes;lib/* beans.FileUpload  列出所要带的参数,用空格分开Pause

  5. java代码生成Excel文件3000条自定义属性的的域账户名

    一个项目为了测试需要模拟3000条域用户,将数据保存在Excel表格,然后导入到与服务器里. 我们今天要做的是自动生成3000条数据,并将这些数据保存在excel表格里面. 需要jar包:poi-3. ...

  6. 使用原生Java代码生成可执行Jar包

    最近想做一个功能,就是把我们编译后的字节码及其资源文件打包成一个可执行的jar包,在装有jre的机器上双击就能运行. 首先是我们需要选择哪些字节码和文件需要打包到文件中,这个我们用JFileChoos ...

  7. 利用java代码生成keyStore

    在前面的章节中介绍了如何利用KeyTool工具生成keyStore:传送门. 但是很多时候,在javaWeb项目中,比如给每个用户加上独特的数字签名,那么我们需要在创建用户的时候,给其生成独一无二的k ...

  8. 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl

    目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...

  9. 基于mybatis的java代码生成存储过程

    问题: 项目中目前使用mybatis操作数据库,使用插件(mybatis-generator)自动生成代码,对于增改查,使用存储过程实现了一版本,方便使用. insert代码生成器用法: insert ...

随机推荐

  1. Nio学习4——EchoServer在IO,NIO,NIO.2中的实现

    堵塞IO实现: public class PlainEchoServer { public void serve(int port) throws IOException { final Server ...

  2. 2014ACM上海邀请赛A解释称号

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; cons ...

  3. Dynamic proxy

    import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflec ...

  4. USACO dualpal

    /* ID:kevin_s1 PROG:dualpal LANG:C++ */ #include <iostream> #include <cstdio> #include & ...

  5. C#版的抓包软件

    C#版的抓包软件   [创建时间:2015-09-10 22:37:04] NetAnalyzer下载地址 不好意思啊,NetAnalyzer停更有点长了,今天继续填坑^&^ NetAnaly ...

  6. HDU 4123 Bob’s Race 树的直径+单调队列

    题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...

  7. Angular规范

     只记录一些自己未曾用过,但觉得对以后的项目有帮助的规范 一 Javascript闭包 把Angular组件包装到一个立即调用函数表达式中(IIFE). 为什么?:把变量从全局作用域中删除了,这有助于 ...

  8. VisualStudio 怎么使用Visual Leak Detector

    VisualStudio 怎么使用Visual Leak Detector 那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测 ...

  9. Linux GPIO 注册和应用

    Linux GPIO 注册和应用 Linux Kernel, GPIO, ARM 于Linux kernel代码.经常使用 GPIO 作为一个特殊的信号,如芯片片选信号. GPIO 功能应用,我们经常 ...

  10. Oracle + EF5 疑难杂症

    原文:Oracle + EF5 疑难杂症 PDF 版 http://files.cnblogs.com/xling/Oracle.pdf Oracle 环境准备 ODAC ODAC 全称 Oracle ...