天天看點

圖檔驗證碼的方法

1.生成驗證碼的工具類

  1. package com.quanran.common.util;

  2. import java.awt.BasicStroke;

  3. import java.awt.Color;

  4. import java.awt.Font;

  5. import java.awt.Graphics2D;

  6. import java.awt.image.BufferedImage;

  7. import java.util.Random;

  8. public class VerifyCodeUtils {

  9. private int w = 70;

  10. private int h = 35;

  11. private String[] fontNames = { "宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312" };

  12. private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";

  13. private String text;

  14. private Random r = new Random();

  15. private Color randomColor() {

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

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

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

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

  20. }

  21. private Font randomFont() {

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

  23. String fontName = fontNames[index];

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

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

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

  27. }

  28. private void drawLine(BufferedImage image) {

  29. // 幹擾線的個數

  30. int num = 3;

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

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

  33. // (x1,y1)為幹擾線的起始點

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

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

  36. // (x2,y2)為幹擾線的結束點

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

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

  39. // 設定幹擾線的寬度

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

  41. // 幹擾線的顔色

  42. g2.setColor(Color.blue);

  43. // 将目前這條幹擾線畫出來

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

  45. }

  46. }

  47. private char randomChar() {

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

  49. return codes.charAt(index);

  50. }

  51. public BufferedImage createImage() {

  52. // BufferedImage的構造(寬度,高度和圖檔類型)

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

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

  55. // 下面兩行代碼是為了解決圖檔背景色為黑色的問題,我設定成了白色

  56. g2.setColor(Color.white);

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

  58. // 可變字元串存儲圖檔裡的文本

  59. StringBuilder sb = new StringBuilder();

  60. // 向圖中畫四個字元

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

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

  63. sb.append(s);

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

  65. g2.setFont(randomFont());

  66. g2.setColor(randomColor());

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

  68. }

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

  70. drawLine(image);

  71. // 傳回圖檔

  72. return image;

  73. }

  74. public String getText() {

  75. return text;

  76. }

  77. }

2.調用工具類

  1. package com.quanran.invite.controller;

  2. import io.swagger.annotations.Api;

  3. import io.swagger.annotations.ApiImplicitParam;

  4. import io.swagger.annotations.ApiImplicitParams;

  5. import io.swagger.annotations.ApiOperation;

  6. import io.swagger.annotations.ApiParam;

  7. import java.awt.image.BufferedImage;

  8. import java.io.OutputStream;

  9. import javax.imageio.ImageIO;

  10. import javax.servlet.http.HttpServletRequest;

  11. import javax.servlet.http.HttpServletResponse;

  12. import org.springframework.web.bind.annotation.GetMapping;

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

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

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

  16. import com.quanran.visitor.common.util.VerifyCodeUtils;

  17. @Api(value="invite系統裡的邀請碼重新發送接口", description="invite系統裡的邀請碼重新發送接口")

  18. @RestController

  19. @RequestMapping("/invitationCode")

  20. public class InvitationCodeController {

  21. @ApiOperation("短信重發的驗證碼接口")

  22. @GetMapping("getVerifyCode")

  23. @ApiImplicitParams({

  24. @ApiImplicitParam(name = "verifyCodeKey", value = "驗證碼的key", required = true, paramType = "query")

  25. })

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

  27. @ApiParam(value="驗證碼的key", required = true) @RequestParam String verifyCodeKey) {

  28. // 生成驗證碼的圖檔

  29. VerifyCodeUtils code = new VerifyCodeUtils();

  30. BufferedImage image = code.createImage();

  31. // 設定響應頭通知浏覽器以圖檔的形式打開

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

  33. // 設定響應頭控制浏覽器不要緩存

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

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

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

  37. // 将圖檔變成流寫給浏覽器

  38. OutputStream os=response.getOutputStream();

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

  40. // 清空關閉流

  41. os.flush();

  42. os.close();

  43. os=null;

  44. response.flushBuffer();

  45. }

  46. }

備注:verifyCodeKey參數是前台傳過來的時間戳,開發中,我們會将此時間戳當做key,生成的驗證碼内容當做value,存到redis中
           

繼續閱讀