图片验证码的方法

1.生成验证码的工具类

 

 
  1. package com.quanran.common.util;

  2.  
  3. import java.awt.BasicStroke;

  4. import java.awt.Color;

  5. import java.awt.Font;

  6. import java.awt.Graphics2D;

  7. import java.awt.image.BufferedImage;

  8. import java.util.Random;

  9. /**

  10. * <p>Description: [生成验证码的控制类]</p>

  11. * Created on 2018年3月1日 下午5:52:58

  12. * @author <a href="mailto: 15175223269@163.com">全冉</a>

  13. * @version 1.0

  14. * Copyright (c) 2018 北京全冉科技有限公司

  15. */

  16. public class VerifyCodeUtils {

  17. /**

  18. * 图片的宽度

  19. */

  20. private int w = 70;

  21. /**

  22. * 图片的高度

  23. */

  24. private int h = 35;

  25. /**

  26. * 定义有那些字体

  27. */

  28. private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };

  29. /**

  30. * 定义有那些验证码的随机字符

  31. */

  32. private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

  33. /**

  34. * 用于gettext 方法 获得生成的验证码文本

  35. */

  36. private String text;

  37. /**

  38. * 随机数对象

  39. */

  40. private Random r = new Random();

  41. /**

  42. * <p>Discription:[生成随机颜色]</p>

  43. * Created on 2018年3月1日 下午5:56:19

  44. * @return Color 返回颜色类

  45. * @author:[全冉]

  46. */

  47. private Color randomColor() {

  48. int red = r.nextInt(150);

  49. int green = r.nextInt(150);

  50. int blue = r.nextInt(150);

  51. return new Color(red, green, blue);

  52. }

  53. /**

  54. * <p>Discription:[生成随机字体]</p>

  55. * Created on 2018年3月1日 下午5:56:42

  56. * @return Font 返回字体类

  57. * @author:[全冉]

  58. */

  59. private Font randomFont() {

  60. int index = r.nextInt(fontNames.length);

  61. String fontName = fontNames[index];

  62. int style = r.nextInt(4);

  63. int size = r.nextInt(5) + 24;

  64. return new Font(fontName, style, size);

  65. }

  66. /**

  67. * <p>Discription:[画干扰线]</p>

  68. * Created on 2018年3月1日 下午5:57:00

  69. * @param image 图片

  70. * @author:[全冉]

  71. */

  72. private void drawLine(BufferedImage image) {

  73. // 干扰线的个数

  74. int num = 3;

  75. Graphics2D g2 = (Graphics2D) image.getGraphics();

  76. for (int i = 0; i < num; i++) {

  77. // (x1,y1)为干扰线的起始点

  78. int x1 = r.nextInt(w);

  79. int y1 = r.nextInt(h);

  80. // (x2,y2)为干扰线的结束点

  81. int x2 = r.nextInt(w);

  82. int y2 = r.nextInt(h);

  83. // 设置干扰线的宽度

  84. g2.setStroke(new BasicStroke(1.5F));

  85. // 干扰线的颜色

  86. g2.setColor(Color.blue);

  87. // 将当前这条干扰线画出来

  88. g2.drawLine(x1, y1, x2, y2);

  89. }

  90. }

  91.  
  92. /**

  93. * <p>Discription:[得到codes的长度内的随机数 并使用charAt 取得随机数位置上的codes中的字符]</p>

  94. * Created on 2018年3月1日 下午5:58:25

  95. * @return char 返回随机字符

  96. * @author:[全冉]

  97. */

  98. private char randomChar() {

  99. int index = r.nextInt(codes.length());

  100. return codes.charAt(index);

  101. }

  102.  
  103. /**

  104. * <p>Discription:[创建一张验证码的图片]</p>

  105. * Created on 2018年3月1日 下午6:00:42

  106. * @return BufferedImage 返回一张图片

  107. * @author:[全冉]

  108. */

  109. public BufferedImage createImage() {

  110. // BufferedImage的构造(宽度,高度和图片类型)

  111. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

  112. Graphics2D g2 = (Graphics2D) image.getGraphics();

  113. // 下面两行代码是为了解决图片背景色为黑色的问题,我设置成了白色

  114. g2.setColor(Color.white);

  115. g2.fillRect(0, 0, w, h);

  116. // 可变字符串存储图片里的文本

  117. StringBuilder sb = new StringBuilder();

  118. // 向图中画四个字符

  119. for (int i = 0; i < 4; i++) {

  120. String s = randomChar() + "";

  121. sb.append(s);

  122. float x = i * 1.0F * w / 4;

  123. g2.setFont(randomFont());

  124. g2.setColor(randomColor());

  125. g2.drawString(s, x, h - 5);

  126. }

  127. this.text = sb.toString();

  128. drawLine(image);

  129. // 返回图片

  130. return image;

  131. }

  132.  
  133. /**

  134. * <p>Discription:[得到验证码的文本]</p>

  135. * Created on 2018年3月1日 下午6:01:00

  136. * @return String 返回验证码文本

  137. * @author:[全冉]

  138. */

  139. public String getText() {

  140. return text;

  141. }

  142. }

2.调用工具类

 

 

 
  1. package com.quanran.invite.controller;

  2.  
  3. import io.swagger.annotations.Api;

  4. import io.swagger.annotations.ApiImplicitParam;

  5. import io.swagger.annotations.ApiImplicitParams;

  6. import io.swagger.annotations.ApiOperation;

  7. import io.swagger.annotations.ApiParam;

  8.  
  9. import java.awt.image.BufferedImage;

  10. import java.io.OutputStream;

  11.  
  12. import javax.imageio.ImageIO;

  13. import javax.servlet.http.HttpServletRequest;

  14. import javax.servlet.http.HttpServletResponse;

  15.  
  16. import org.springframework.web.bind.annotation.GetMapping;

  17. import org.springframework.web.bind.annotation.RequestMapping;

  18. import org.springframework.web.bind.annotation.RequestParam;

  19. import org.springframework.web.bind.annotation.RestController;

  20.  
  21. import com.quanran.visitor.common.util.VerifyCodeUtils;

  22.  
  23. /**

  24. * <p>Description: [invite系统里的邀请码重新发送接口]</p>

  25. * Created on 2017年11月16日 下午2:42:59

  26. * @author <a href="mailto: 15175223269@163.com">全冉</a>

  27. * @version 1.0

  28. * Copyright (c) 2017 北京全冉科技有限公司

  29. */

  30. @Api(value="invite系统里的邀请码重新发送接口", description="invite系统里的邀请码重新发送接口")

  31. @RestController

  32. @RequestMapping("/invitationCode")

  33. public class InvitationCodeController {

  34.  
  35. @ApiOperation("短信重发的验证码接口")

  36. @GetMapping("getVerifyCode")

  37. @ApiImplicitParams({

  38. @ApiImplicitParam(name = "verifyCodeKey", value = "验证码的key", required = true, paramType = "query")

  39. })

  40. public void getVerifyCode(HttpServletRequest request, HttpServletResponse response,

  41. @ApiParam(value="验证码的key", required = true) @RequestParam String verifyCodeKey) {

  42. // 生成验证码的图片

  43. VerifyCodeUtils code = new VerifyCodeUtils();

  44. BufferedImage image = code.createImage();

  45. // 设置响应头通知浏览器以图片的形式打开

  46. response.setContentType("image/jpeg");

  47. // 设置响应头控制浏览器不要缓存

  48. response.setHeader("Pragma","no-cache");

  49. response.setHeader("Cache-Control","no-cache");

  50. response.setIntHeader("Expires",-1);

  51. // 将图片变成流写给浏览器

  52. OutputStream os=response.getOutputStream();

  53. ImageIO.write(image, "jpg", os);

  54. // 清空关闭流

  55. os.flush();

  56. os.close();

  57. os=null;

  58. response.flushBuffer();

  59. }

  60. }

备注:verifyCodeKey参数是前台传过来的时间戳,开发中,我们会将此时间戳当做key,生成的验证码内容当做value,存到redis中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值