A-A+

使用patchca生成验证码

2016年01月10日 技术 暂无评论 阅读 5,900 次

patchca是一个验证码开源类库,使用它生成验证码的步骤如下。

1.在Maven的pom.xml中添加如下节点:

  1. <dependency>
  2.     <groupId>com.github.bingoohuang</groupId>
  3.     <artifactId>patchca</artifactId>
  4.     <version>0.0.1</version>
  5. </dependency>

2.编写一个生成验证码的工具类,本文中命名其为“ValidatCodeUtils”,代码如下:

  1. public class ValidatCodeUtils {
  2.     private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
  3.     private static Random random = new Random();
  4.     static {
  5.         cs.setColorFactory(new ColorFactory() {
  6.             public Color getColor(int x) {
  7.                 int[] c = new int[3];
  8.                 int i = random.nextInt(c.length);
  9.                 for (int fi = 0; fi < c.length; fi++) {
  10.                     if (fi == i) {
  11.                         c[fi] = random.nextInt(71);
  12.                     } else {
  13.                         c[fi] = random.nextInt(256);
  14.                     }
  15.                 }
  16.                 return new Color(c[0], c[1], c[2]);
  17.             }
  18.         });
  19.         cs.setBackgroundFactory(new BackgroundFactory() {
  20.             public void fillBackground(BufferedImage bufferedImage) {
  21.                 Graphics graphics = bufferedImage.getGraphics();
  22.                 // 验证码图片的宽高
  23.                 int imgWidth = bufferedImage.getWidth();
  24.                 int imgHeight = bufferedImage.getHeight();
  25.                 // 画100个噪点(颜色及位置随机) 
  26.                 for(int i = 0; i < 100; i++) {
  27.                     // 随机颜色 
  28.                     int rInt = random.nextInt(255);
  29.                     int gInt = random.nextInt(255);
  30.                     int bInt = random.nextInt(255);
  31.                     graphics.setColor(new Color(rInt, gInt, bInt));
  32.                     // 随机位置 
  33.                     int xInt = random.nextInt(imgWidth - 3);
  34.                     int yInt = random.nextInt(imgHeight - 2);
  35.                     // 随机旋转角度 
  36.                     int sAngleInt = random.nextInt(360);
  37.                     int eAngleInt = random.nextInt(360);
  38.                     // 随机大小 
  39.                     int wInt = random.nextInt(6);
  40.                     int hInt = random.nextInt(6);
  41.                     graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
  42.                     // 画5条干扰线 
  43.                     if (i % 20 == 0) {
  44.                         int xInt2 = random.nextInt(imgWidth);
  45.                         int yInt2 = random.nextInt(imgHeight);
  46.                         graphics.drawLine(xInt, yInt, xInt2, yInt2);
  47.                     }
  48.                 }
  49.             }
  50.         });
  51.         RandomWordFactory wf = new RandomWordFactory();
  52.         wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");
  53.         wf.setMaxLength(4);
  54.         wf.setMinLength(4);
  55.         cs.setWordFactory(wf);
  56.     }
  57.     public static String create(OutputStream os) throws IOException {
  58.         switch (random.nextInt(5)) {
  59.             case 0:
  60.                 cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));
  61.                 break;
  62.             case 1:
  63.                 cs.setFilterFactory(new MarbleRippleFilterFactory());
  64.                 break;
  65.             case 2:
  66.                 cs.setFilterFactory(new DoubleRippleFilterFactory());
  67.                 break;
  68.             case 3:
  69.                 cs.setFilterFactory(new WobbleRippleFilterFactory());
  70.                 break;
  71.             case 4:
  72.                 cs.setFilterFactory(new DiffuseRippleFilterFactory());
  73.                 break;
  74.         }
  75.         return EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
  76.     }
  77. }

3.在controller中调用此工具类,并将其输出。

  1. @RequestMapping(value = "/vcode", method = RequestMethod.GET)
  2. public void getValidatCode(HttpServletRequest request, HttpServletResponse response){
  3.         response.setContentType("image/png");
  4.         response.setHeader("Cache-Control""no-cache, no-store");
  5.         response.setHeader("Pragma""no-cache");
  6.         long time = System.currentTimeMillis();
  7.         response.setDateHeader("Last-Modified", time);
  8.         response.setDateHeader("Date", time);
  9.         response.setDateHeader("Expires", time);
  10.         try {
  11.             String validatCode = ValidatCodeUtils.create(response.getOutputStream());
  12.         } catch (Exception e) {
  13.         }
  14. }

4.在前端HTML页面中访问上文中的地址,展示验证码图片。

  1. <div class="form-group has-feedback col-xs-4">
  2.     <img src="${request.contextPath}/vcode.html" style="height: 34px" id="imgcode">
  3. </div>

5.最终的效果如下:

vcode

6.环境

SpringMVC4

给我留言

Copyright © 字痕随行 保留所有权利.   Theme  Ality

用户登录

分享到: