JAVA 代碼產生。SimpleCaptcha

來源:互聯網
上載者:User

標籤:

去官方網站下載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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.