Package Com.g.core.common.jcaptcha;
Import Java.awt.Color;
Import Java.awt.Font;
Import Com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
Import Com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
Import Com.octo.captcha.component.image.color.RandomListColorGenerator;
Import Com.octo.captcha.component.image.fontgenerator.FontGenerator;
Import Com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
Import Com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
Import Com.octo.captcha.component.image.textpaster.TextPaster;
Import Com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
Import Com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
Import Com.octo.captcha.component.image.wordtoimage.WordToImage;
Import Com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
Import Com.octo.captcha.component.word.wordgenerator.WordGenerator;
Import Com.octo.captcha.engine.image.ListImageCaptchaEngine;
Import Com.octo.captcha.image.gimpy.GimpyFactory;
/**
* Generate Verification Code picture
*/
public class Jcaptchaengine extends Listimagecaptchaengine {
public static final String Image_captcha_key = "Imagecaptcha";//Imagecaptcha object stored in session KEY
public static final String captcha_input_name = "J_captcha";//Authentication code Enter form name
public static final String Captcha_image_url = "/captcha.jpg";//Verify Code picture URL
private static final Integer Min_word_length = 4;//Verification Code Minimum length
private static final Integer Max_word_length = 4;//Verification Code Maximum length
private static final Integer Image_height = 28;//verification code Picture height
private static final Integer image_width = 80;//verification Code picture width
private static final Integer min_font_size = 16;//Authenticode Minimum font
private static final Integer max_font_size = 16;//authentication Code Max font
private static final String Random_word = "abcdefghijklmnopqrstuvwxyz";//random character
private static final String image_path = "./captcha/";//random background picture path
Verification Code Random Font
private static final font[] Random_font = new font[] {
New Font ("Nyala", Font.Bold, Min_font_size),
New Font ("Arial", Font.Bold, Min_font_size),
New Font ("Bell MT", Font.Bold, Min_font_size),
New Font ("Credit Valley", Font.Bold, Min_font_size),
New Font ("Impact", Font.Bold, Min_font_size)
};
Validation code random Color
private static final color[] Random_color = new color[] {
New Color (255, 255, 255),
New Color (255, 220, 220),
New Color (220, 255, 255),
New Color (220, 220, 255),
New Color (255, 255, 220),
New Color (220, 255, 220)
};
Generate Verification Code
@Override
protected void Buildinitialfactories () {
Randomlistcolorgenerator randomlistcolorgenerator = new Randomlistcolorgenerator (Random_color);
Backgroundgenerator backgroundgenerator = new Filereaderrandombackgroundgenerator (image_width, IMAGE_HEIGHT, IMAGE_ PATH);
Wordgenerator wordgenerator = new Randomwordgenerator (Random_word);
Fontgenerator fontgenerator = new Randomfontgenerator (min_font_size, Max_font_size, Random_font);
textdecorator[] Textdecorator = new textdecorator[] {};
Textpaster textpaster = new Decoratedrandomtextpaster (min_word_length, Max_word_length, RandomListColorGenerator, Textdecorator);
Wordtoimage wordtoimage = new Composedwordtoimage (Fontgenerator, Backgroundgenerator, Textpaster);
Addfactory (New Gimpyfactory (Wordgenerator, wordtoimage));
}
}
Package com.g.core.render;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Javax.imageio.ImageIO;
Import Javax.servlet.ServletOutputStream;
Import Com.g.core.common.jcaptcha.captchaservicesingleton;
Import Com.jfinal.kit.StringKit;
Import Com.jfinal.render.Render;
public class Jcaptcharender extends Render {
Private String Randomcodekey;
Public Jcaptcharender (String randomcodekey) {
if (Stringkit.isblank (Randomcodekey))
throw new IllegalArgumentException ("Randomcodekey can not be blank");
This.randomcodekey = Randomcodekey;
}
@Override
public void render () {
Response.setheader ("Cache-control", "No-store");
Response.setheader ("Pragma", "No-cache");
Response.setdateheader ("Expires", 0);
Response.setcontenttype ("Image/jpeg");
Servletoutputstream SOS = null;
try {
SOS = Response.getoutputstream ();
String Captchaid = Request.getsession (True). GetId ();
BufferedImage challenge = (bufferedimage) captchaservicesingleton.getinstance (). Getchallengeforid (RandomCodeKey, Request.getlocale ());
Imageio.write (Challenge, "JPG", SOS);
Sos.flush ();
catch (Exception e) {
throw new RuntimeException (e);
}
finally {
if (SOS!= null)
try {sos.close ();} catch (IOException e) {e.printstacktrace ();}
}
}
}