这里有新鲜出炉的Java函数式编程,程序狗速度看过来!
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
JCaptcha 是一个用来生成验证码的开源Java类库,使用起来也是非常的简单方便。本文通过代码实例介绍了JCaptcha类库。
JCaptcha是非常强大的,不光是可以生成图片式的验证码,还可以生成声音式的(新浪就使用了双重验证码)。本文简单的介绍了JCaptcha库以及使用实例,下面一起来看看。
下载JCaptcha库
maven依赖如此添加:
- <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;
- }
- }
响应网页中对验正码图像的请求
可以定义一个
响应这个请求,如果用
- servlet
,也可以用某个
- springMVC
中的某个方法响应这个请求,不管怎样,都需要指定一个路径对应到
- Controller
的方法,比如路径是
- servlet或controller
- :”/aaa/captcha”
那么在响应对这个路径的请求的Servlet中可以这样写:
- //获取验证码背景图片的路径,这路径放了很多作为背景的图像
- 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());
如果用springMVC,就这样写:
- //获取验证码背景图片的路径,这路径放了很多作为背景的图像
- 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();
这两种方式,向客户端返回的都是二进制数据。
- String captcha_backgrounds = session.getServletContext().getRealPath(“/WEB-INF/img/captcha”);
表示路径
下面放的是作为验证码图像的背景的多张图片,必须是jpeg,大小可能没限制,你可以自己试一下。
- /WEB-INF/img/captcha
网页中用 <IMG> 指向这个地址
- <img id="captcha" src="/captcha_img" onclick="refreshCaptchaImg()" />
js函数
响应图片的点击,每点击一次,就重新获取一张新的验证码图片。如何重新获取验正码图片呢
- refreshCaptchaImg()
只需改变img的src属性,但这里是每次都是用同一个地址来设置这个属性,这样不会引起真正的刷新,所以方法
是这样实现的:
- refreshCaptchaImg()
- function refreshCaptchaImg() {
- //从服务端重新下载验证码图片
- //给这个地加参数纯为了强制刷新,否则由于src指向的url地址没变,浏览器不会真正生刷新图片
- var now = new Date()
- $("#captcha").attr("src","/captcha_img?"+now.getTime());
以上就是Java中验证码生成库JCaptcha的介绍及使用,希望对大家学习java有所帮助。
来源: http://www.phperz.com/article/17/1110/360314.html