1、首先是基本属性类CaptchaConstant.java
package com.baofoo.web.captcha; import java.awt.Color; import java.awt.Font; public class CaptchaConstant { /** * 默认字体名 */ public static final String DEFAULT_FONT_NAME = "Courier"; /** * 默认字体大小 */ public static final int DEFAULT_FONT_SIZE = 25; /** * 粗体,斜体等 */ public static final int DEFAULT_FONT_TYPE = Font.BOLD; /** * 无干扰 */ public static final String NOISE_NONE = "none"; /** * 随机类型的干扰 */ public static final String NOISE_RANDOM = "random"; /** * 对应 nl.captcha.noise.CurvedLineNoiseProducer */ public static final String NOISE_CURVED = "curved"; /** * 对应 nl.captcha.noise.StraightLineNoiseProducer */ public static final String NOISE_STRAIGHT = "straight"; /** * 默认干扰 */ public static final String DEFAULT_NOISE = NOISE_RANDOM; /** * 默认干扰线颜色 */ public static final Color DEFAULT_NOISE_COLOR = Color.darkGray; /** * 默认干扰线宽度 */ public static final int DEFAULT_NOISE_WIDTH = 2; /** * 默认宽度 */ public static final int DEFAULT_WIDTH = 120; /** * 默认高度 */ public static final int DEFAULT_HEIGHT = 30; /** * 对应 nl.captcha.text.producer.DefaultTextProducer,除i,o以外的字母 */ public static final String TEXT_WORD = "word"; /** * 对应 nl.captcha.text.producer.ChineseTextProducer */ public static final String TEXT_CHINESE = "chinese"; /** * 对应 nl.captcha.text.producer.DefaultTextProducer,除i,o以外的字母及除1,0以外的数字 */ public static final String TEXT_DEFAULT = "default"; /** * 对应 nl.captcha.text.producer.DefaultTextProducer,除1,0以外的数字 */ public static final String TEXT_NUMBER = "number"; /** * 默认字符 */ public static final String DEFAULT_TEXT = TEXT_DEFAULT; /** * 默认字符长度 */ public static final int DEFAULT_TEXT_LENGTH = 6; /** * 默认字体边框齿轮效果 */ public static final int DEFAULT_GIMP = 3; /** * 默认字体颜色为黑色 */ public static final Color DEFAULT_TEXT_COLOR = Color.black; /** * 随机字体颜色 */ public static final String TEXT_COLOR_RANDOM = "random"; /** * 默认背景颜色为白色 */ public static final Color DEFAULT_BACKGROUND_COLOR = Color.white; /** * 这里没有0和1是为了避免歧义 和字母I和O */ public static final char[] NUMBER_CHAR = new char[] { ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘ }; /** * 这里没有I和O是为了避免歧义 和数字0和1 */ public static final char[] WORD_CHAR = new char[] { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘r‘, ‘w‘, ‘x‘, ‘y‘ }; /** * 这里没有I和O和数字0和1是为了避免歧义 */ public static final char[] DEFAULT_CHAR = new char[] { ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘k‘, ‘m‘, ‘n‘, ‘p‘, ‘r‘, ‘w‘, ‘x‘, ‘y‘ }; }
2、这个是验证码配置类,可以按照自己的需求来更改属性SimpleCaptchaServlet.java
package com.baofoo.web.captcha; import java.awt.Color; import java.awt.Font; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import nl.captcha.Captcha; import nl.captcha.Captcha.Builder; import nl.captcha.backgrounds.FlatColorBackgroundProducer; import nl.captcha.backgrounds.GradiatedBackgroundProducer; import nl.captcha.gimpy.BlockGimpyRenderer; import nl.captcha.noise.CurvedLineNoiseProducer; import nl.captcha.noise.NoiseProducer; import nl.captcha.noise.StraightLineNoiseProducer; import nl.captcha.servlet.CaptchaServletUtil; import nl.captcha.text.producer.ChineseTextProducer; import nl.captcha.text.producer.DefaultTextProducer; import nl.captcha.text.renderer.DefaultWordRenderer; import nl.captcha.text.renderer.WordRenderer; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.math.RandomUtils; import com.baofoo.exception.ServiceException; /** * 扩展默认的simpleCaptcha * * @author yuqih * */ public class SimpleCaptchaServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * 宽度 */ protected int _width; /** * 高度 */ protected int _height; /** * 干扰 */ protected List<NoiseProducer> _noiseList; /** * 是否加边框 */ protected boolean _border; /** * 文字 */ protected List<Text> _text; /** * 字体 */ protected Font _font; /** * 背景色 */ protected Color _backgroundColor; /** * 渐进开始背景色 */ protected Color _backgroundColorFrom; /** * 渐进结束背景色 */ protected Color _backgroundColorTo; /** * 是否渐进背景 */ private boolean _gradiatedBackground; /** * 字体颜色 */ protected Color _textColor; /** * 字体边框齿轮效果, 默认3 */ protected int _gimp; @Override public void init() throws ServletException { initHeight(); initWidth(); initNoise(); initBorder(); initText(); initBackgroundColor(); initTextColor(); initFont(); initGimp(); } /** * 获取图片只会有get方法 */ @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String captchaName = request.getParameter("captcha"); if (StringUtils.isBlank(captchaName)) { captchaName = Captcha.NAME; } Builder builder = new Captcha.Builder(_width, _height); // 增加边框 if (_border) { builder.addBorder(); } // 增加干扰 if (_noiseList != null && _noiseList.size() != 0) { builder.addNoise(_noiseList.get(RandomUtils.nextInt(_noiseList .size()))); } // ----------------自定义字体大小----------- // 自定义设置字体颜色和大小 最简单的效果 多种字体随机显示 List<Font> fontList = new ArrayList<Font>(); fontList.add(_font); WordRenderer wr = new DefaultWordRenderer(Arrays.asList(_textColor), fontList); // 增加文本 for (Text text : _text) { if (CaptchaConstant.TEXT_CHINESE.equals(text.type)) { builder.addText(new ChineseTextProducer(text.length), wr); } else if (CaptchaConstant.TEXT_NUMBER.equals(text.type)) { builder.addText(new DefaultTextProducer(text.length, CaptchaConstant.NUMBER_CHAR), wr); } else if (CaptchaConstant.TEXT_WORD.equals(text.type)) { builder.addText(new DefaultTextProducer(text.length, CaptchaConstant.WORD_CHAR), wr); } else { builder.addText(new DefaultTextProducer(text.length, CaptchaConstant.DEFAULT_CHAR), wr); } } if (_gradiatedBackground) { // --------------添加背景------------- // 设置背景渐进效果 以及颜色 form为开始颜色,to为结束颜色 GradiatedBackgroundProducer gbp = new GradiatedBackgroundProducer(); gbp.setFromColor(_backgroundColorFrom); gbp.setToColor(_backgroundColorTo); builder.addBackground(gbp); } else { FlatColorBackgroundProducer fbp = new FlatColorBackgroundProducer( _backgroundColor); builder.addBackground(fbp); } // ---------装饰字体--------------- // 字体边框齿轮效果 builder.gimp(new BlockGimpyRenderer(_gimp)); Captcha captcha = builder.build(); request.getSession().setAttribute(captchaName, captcha); CaptchaServletUtil.writeImage(response, captcha.getImage()); } private void initGimp() { String gimp = getInitParameter("gimp"); if (gimp != null) { _gimp = Integer.parseInt(gimp); } else { _gimp = CaptchaConstant.DEFAULT_GIMP; } } private void initHeight() { String height = getInitParameter("height"); if (height != null) { _height = Integer.valueOf(height); } else { _height = CaptchaConstant.DEFAULT_HEIGHT; } } private void initWidth() { String width = getInitParameter("width"); if (width != null) { _width = Integer.valueOf(width); } else { _width = CaptchaConstant.DEFAULT_WIDTH; } } private void initNoise() { String noise = getInitParameter("noise"); String noiseColorStr = getInitParameter("noiseColor"); String noiseWidthStr = getInitParameter("noiseWidth"); Color noiseColor; int noiseWidth; if (!StringUtils.isBlank(noiseColorStr)) { noiseColor = new Color(Integer.parseInt(noiseColorStr, 16)); } else { noiseColor = CaptchaConstant.DEFAULT_NOISE_COLOR; } if (!StringUtils.isBlank(noiseWidthStr)) { noiseWidth = Integer.parseInt(noiseWidthStr); } else { noiseWidth = CaptchaConstant.DEFAULT_NOISE_WIDTH; } _noiseList = getNoise(noise, noiseColor, noiseWidth); } private List<NoiseProducer> getNoise(String noiseType, Color noiseColor, int noiseWidth) { List<NoiseProducer> noise; if (noiseType == null) { noise = null; } else if (noiseType.equals(CaptchaConstant.NOISE_NONE)) { noise = null; } else if (noiseType.equals(CaptchaConstant.NOISE_CURVED)) { noise = new ArrayList<NoiseProducer>(); noise.add(new CurvedLineNoiseProducer(noiseColor, noiseWidth)); } else if (noiseType.equals(CaptchaConstant.NOISE_STRAIGHT)) { noise = new ArrayList<NoiseProducer>(); noise.add(new StraightLineNoiseProducer(noiseColor, noiseWidth)); } else if (noiseType.equals(CaptchaConstant.NOISE_RANDOM)) { noise = new ArrayList<NoiseProducer>(); noise.add(new StraightLineNoiseProducer(noiseColor, noiseWidth)); noise.add(new CurvedLineNoiseProducer(noiseColor, noiseWidth)); } else { noise = getNoise(CaptchaConstant.DEFAULT_NOISE, noiseColor, noiseWidth); } return noise; } private void initBorder() { String border = getInitParameter("border"); if (border != null) { _border = Boolean.valueOf(border); } else { _border = true; } } private void initText() { _text = new ArrayList<Text>(); String text = getInitParameter("text"); if (text == null) { _text.add(new Text(CaptchaConstant.DEFAULT_TEXT, CaptchaConstant.DEFAULT_TEXT_LENGTH)); return; } String[] ts = text.split(","); for (int i = 0; i < ts.length; i++) { String[] ts1 = ts[i].split(":"); String textType = ts1[0]; int length = Integer.parseInt(ts1[1]); if (isTextValid(textType, length)) { _text.add(new Text(textType, length)); } else { throw new ServiceException("暂不支持"); } } } private boolean isTextValid(String text, int length) { if (length < 1) { return false; } if (StringUtils.isBlank(text)) { return false; } if (text.equals(CaptchaConstant.TEXT_CHINESE) || text.equals(CaptchaConstant.TEXT_DEFAULT) || text.equals(CaptchaConstant.TEXT_NUMBER) || text.equals(CaptchaConstant.TEXT_WORD)) { return true; } return false; } private void initBackgroundColor() { String backgroundColor = getInitParameter("backgroundColor"); String backgroundColorFrom = getInitParameter("backgroundColorFrom"); String backgroundColorTo = getInitParameter("backgroundColorTo"); if (backgroundColorFrom == null || backgroundColorTo == null) { _gradiatedBackground = false; if (backgroundColor == null) { _backgroundColor = CaptchaConstant.DEFAULT_BACKGROUND_COLOR; } else { _backgroundColor = new Color(Integer.parseInt(backgroundColor, 16)); } } else { _gradiatedBackground = true; _backgroundColorFrom = new Color(Integer.parseInt( backgroundColorFrom, 16)); _backgroundColorTo = new Color(Integer.parseInt(backgroundColorTo, 16)); } } private void initTextColor() { String textColor = getInitParameter("textColor"); if (textColor == null) { _textColor = CaptchaConstant.DEFAULT_TEXT_COLOR; } else { _textColor = new Color(Integer.parseInt(textColor, 16)); } } private void initFont() { String font = getInitParameter("fontName"); String fontType = getInitParameter("fontType"); String fontSize = getInitParameter("fontSize"); int type; int size; if (font == null) { font = CaptchaConstant.DEFAULT_FONT_NAME; } if (StringUtils.isBlank(fontType)) { type = CaptchaConstant.DEFAULT_FONT_TYPE; } else { type = Integer.parseInt(fontType); } if (StringUtils.isBlank(fontSize)) { size = CaptchaConstant.DEFAULT_FONT_SIZE; } else { size = Integer.parseInt(fontSize); } _font = new Font(font, type, size); } private class Text { String type; int length; Text(String type, int length) { this.type = type; this.length = length; } } }
3、web.xml文件的配置(这里可以自定义一些基本属性)
<servlet> <servlet-name>VerifyCodeServlet</servlet-name> <servlet-class>com.baofoo.web.captcha.SimpleCaptchaServlet</servlet-class> <init-param> <param-name>backgroundColorFrom</param-name> <param-value>3F526C</param-value> </init-param> <init-param> <param-name>backgroundColorTo</param-name> <param-value>3E506A</param-value> </init-param> <init-param> <param-name>textColor</param-name> <param-value>ffffff</param-value> </init-param> <init-param> <param-name>border</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>width</param-name> <param-value>100</param-value> </init-param> <init-param> <param-name>height</param-name> <param-value>40</param-value> </init-param> <init-param> <param-name>text</param-name> <param-value>number:6</param-value> </init-param> <init-param> <param-name>noise</param-name> <param-value>random</param-value> </init-param> <init-param> <param-name>gimp</param-name> <param-value>2</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>VerifyCodeServlet</servlet-name> <url-pattern>/verifyCodeServlet</url-pattern> </servlet-mapping>
4、JSP页面的使用
<img id="CaptchaImg" src="${ctx}/verifyCodeServlet?refresh= <%=System.currentTimeMillis()%>" onclick="refreshImg()" />
这里${ctx}是web项目路径
时间: 2024-10-10 18:50:01