当前位置: 首页 > 工具软件 > JCaptcha > 使用案例 >

JCaptcha基本使用

笪波鸿
2023-12-01

JCaptcha是一个开源的用来生成图形验证码的Java开源组件,使用起来也是非常的简单方便。不光是可以生成图片式的验证码,还可以生成声音式的。


生成验证码Servlet:

package org.hz.captcha;
public class CaptchaImageServlet extends HttpServlet {

	private static final long serialVersionUID = 1214253565574396379L;
	private static Log log = LogFactory.getLog(CaptchaImageServlet.class);

	public static final String CAPTCHA_IMAGE_FORMAT = "jpeg";

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.debug("into doGet of captcha image");
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		byte[] captchaChallengeAsJpeg = null;
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {

			String captchaId = request.getSession().getId();
			
			AbstractManageableImageCaptchaService captchaService = new DefaultManageableImageCaptchaService();
			captchaService.setCaptchaEngine(new MyCaptchaEngine());
			BufferedImage challenge = captchaService.getImageChallengeForID(
					captchaId, request.getLocale());

			ImageIO.write(challenge, CAPTCHA_IMAGE_FORMAT, jpegOutputStream);
		} catch (IllegalArgumentException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return;
		} catch (CaptchaServiceException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			return;
		}

		captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

		// flush it in the response
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragma", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("image/" + CAPTCHA_IMAGE_FORMAT);

		ServletOutputStream responseOutputStream = response.getOutputStream();
		responseOutputStream.write(captchaChallengeAsJpeg);
		responseOutputStream.flush();
		responseOutputStream.close();
	}

}


某些时候你可能需要达到自己想要的验证码是,你就可以:

package org.hz.captcha;

public class MyCaptchaEngine extends ListImageCaptchaEngine {

	public static final String DEFAULT_WORD_CHARS = "0123456789ABCD";
	public static final Color DEFAULT_BG_COLOR = Color.WHITE;

	private int minWordLength;
	private int maxWordLength;

	private int imageWidth;
	private int imageHeight;

	private int minFontSize;
	private int maxFontSize;

	private String wordChars;

	private Color bgcolor;

	private int[] fontColorRange = { 25, 225 };

	private void initSettings() {

		minWordLength = 4;
		maxWordLength = 4;

		imageWidth = 100;
		imageHeight = 40;

		minFontSize = 15;
		maxFontSize = 25;

		wordChars = DEFAULT_WORD_CHARS;

		bgcolor = DEFAULT_BG_COLOR;

		fontColorRange = new int[] { 25, 25 };
	}

	protected void buildInitialFactories() {
		initSettings();
		WordGenerator wordGenerator = new RandomWordGenerator(wordChars);
		BackgroundGenerator backgroundGenerator = new GradientBackgroundGenerator(
				imageWidth, imageHeight, bgcolor, bgcolor);
		Font[] fontList= new Font[]{new Font("Arial", 0, 10)};
		FontGenerator fontGenerator = new RandomFontGenerator(minFontSize, maxFontSize,fontList);
		ColorGenerator colorGenerator = new RandomRangeColorGenerator(
				fontColorRange, fontColorRange, fontColorRange);
		TextDecorator[] textDecorators = new TextDecorator[] {};
		TextPaster textPaster = new DecoratedRandomTextPaster(minWordLength,
				maxWordLength, colorGenerator, textDecorators);
		WordToImage wordToImage = new ComposedWordToImage(fontGenerator,
				backgroundGenerator, textPaster);	
		addFactory(new GimpyFactory(wordGenerator, wordToImage));
	}

}




 类似资料: