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();
    }

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

posted @ 2015-08-08 19:27  mengfanrong  阅读(397)  评论(0编辑  收藏  举报