JAVA code generation. Simplecaptcha

Source: Internet
Author: User

Go to the official website to download the jar package:

http://simplecaptcha.sourceforge.net/

Javadocs:

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

Write your own tool class:

/*
* To-Change the 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 {


Verification Code Content
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 ';
Number of verification codes
private int _codecount = 4;
Verification Code width
private int _width = 110;
Verification Code Height
private int _height = 50;
Verification Code Color
Private Color _codecolor = Color.Black;
Use font name
Private String _fontname = "System";
Use font type
private int _fonttype = Font.Bold;
Use font size
private int _fontsize = 40;
Interference Line Color
Private Color _noisecolor = Color.Black;
Interference line Size
private int _noisesize = 2;
Number of interfering lines
private int _noisecount = 1;
Whether to turn on uppercase and lowercase when verifying graphics code
Private Boolean whetheropenbigorsmall = false;


Verification Code Storage
Private String codememory;


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


Comparison Verification Code
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;
}


}


Call and compare:


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

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

JAVA code generation. Simplecaptcha

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.