<dependency>
<groupId>com.octo.captcha</groupId>
<artifactId>jcaptcha</artifactId>
<version>1.0</version>
</dependency>
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.CaptchaEngine;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import java.awt.*;
/**
* 产生验证码图片的类
*/
public class CapchaHelper {
private static final Integer MIN_WORD_LENGTH = 4;// 验证码最小长度
private static final Integer MAX_WORD_LENGTH = 4;// 验证码最大长度
private static final Integer IMAGE_HEIGHT = 30;// 验证码图片高度
private static final Integer IMAGE_WIDTH = 130;// 验证码图片宽度
private static final Integer MIN_FONT_SIZE = 15;// 验证码最小字体
private static final Integer MAX_FONT_SIZE = 15;// 验证码最大字体
private static final String RANDOM_WORD = "0123456789";// 随机字符
// 验证码随机字体
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)
};
// 验证码随机颜色
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)
};
private static ListImageCaptchaEngine captchaEngine;
public static CaptchaEngine getCaptchaEngine(final String imgPath) {
if (captchaEngine == null) {
synchronized (CapchaHelper.class) {
if (captchaEngine == null && imgPath != null) {
captchaEngine = new ListImageCaptchaEngine() {
@Override
protected void buildInitialFactories() {
RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(RANDOM_COLOR);
BackgroundGenerator backgroundGenerator = new FileReaderRandomBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, imgPath);
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));
}
};
}
}
}
return captchaEngine;
}
}
//获取验证码背景图片的路径,这路径放了很多作为背景的图像
String captcha_backgrounds = session.getServletContext().getRealPath("/WEB-INF/img/captcha");
CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds);
//需要admin网页中用js定时从服务端获取当前的验证码
Captcha captcha = ce.getNextCaptcha();
//为了验证,把captcha对象放到session中,以在客户端提交验证码时进行验证
req.getSession().setAttribute("captcha", captcha);
//获取验证码图片,这是未压缩的位图
BufferedImage image = (BufferedImage) captcha.getChallenge();
resp.setContentType("image/jpeg");
ImageIO.write(image, "jpg", resp.getOutputStream());
//获取验证码背景图片的路径,这路径放了很多作为背景的图像
String captcha_backgrounds = session.getServletContext().getRealPath("/WEB-INF/img/captcha");
CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds);
//需要admin网页中用js定时从服务端获取当前的验证码
Captcha captcha = ce.getNextCaptcha();
//为了验证,把captcha对象放到session中,以在客户端提交验证码时进行验证
session.setAttribute("captcha", captcha);
//获取验证码图片,这是未压缩的位图
BufferedImage image = (BufferedImage) captcha.getChallenge();
ByteArrayOutputStream bao=new ByteArrayOutputStream();
//应缩成jpg并写到输出流中
ImageIO.write(image, "jpg", bao);
return bao.toByteArray();
<img id="captcha" src="/captcha_img" onclick="refreshCaptchaImg()" />
function refreshCaptchaImg() {
//从服务端重新下载验证码图片
//给这个地加参数纯为了强制刷新,否则由于src指向的url地址没变,浏览器不会真正生刷新图片
var now = new Date()
$("#captcha").attr("src","/captcha_img?"+now.getTime());
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有