圖片驗證碼的方法
1.生成驗證碼的工具類
-
package com.quanran.common.util;
-
import java.awt.BasicStroke;
-
import java.awt.Color;
-
import java.awt.Font;
-
import java.awt.Graphics2D;
-
import java.awt.image.BufferedImage;
-
import java.util.Random;
-
/**
-
* <p>Description: [生成驗證碼的控制類]</p>
-
* Created on 2018年3月1日 下午5:52:58
-
* @author <a href="mailto: [email protected]">全冉</a>
-
* @version 1.0
-
* Copyright (c) 2018 北京全冉科技有限公司
-
*/
-
public class VerifyCodeUtils {
-
/**
-
* 圖片的寬度
-
*/
-
private int w = 70;
-
/**
-
* 圖片的高度
-
*/
-
private int h = 35;
-
/**
-
* 定義有那些字型
-
*/
-
private String[] fontNames = { "宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312" };
-
/**
-
* 定義有那些驗證碼的隨機字元
-
*/
-
private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
-
/**
-
* 用於gettext 方法 獲得生成的驗證碼文字
-
*/
-
private String text;
-
/**
-
* 隨機數物件
-
*/
-
private Random r = new Random();
-
/**
-
* <p>Discription:[生成隨機顏色]</p>
-
* Created on 2018年3月1日 下午5:56:19
-
* @return Color 返回顏色類
-
* @author:[全冉]
-
*/
-
private Color randomColor() {
-
int red = r.nextInt(150);
-
int green = r.nextInt(150);
-
int blue = r.nextInt(150);
-
return new Color(red, green, blue);
-
}
-
/**
-
* <p>Discription:[生成隨機字型]</p>
-
* Created on 2018年3月1日 下午5:56:42
-
* @return Font 返回字型類
-
* @author:[全冉]
-
*/
-
private Font randomFont() {
-
int index = r.nextInt(fontNames.length);
-
String fontName = fontNames[index];
-
int style = r.nextInt(4);
-
int size = r.nextInt(5) + 24;
-
return new Font(fontName, style, size);
-
}
-
/**
-
* <p>Discription:[畫干擾線]</p>
-
* Created on 2018年3月1日 下午5:57:00
-
* @param image 圖片
-
* @author:[全冉]
-
*/
-
private void drawLine(BufferedImage image) {
-
// 干擾線的個數
-
int num = 3;
-
Graphics2D g2 = (Graphics2D) image.getGraphics();
-
for (int i = 0; i < num; i++) {
-
// (x1,y1)為干擾線的起始點
-
int x1 = r.nextInt(w);
-
int y1 = r.nextInt(h);
-
// (x2,y2)為干擾線的結束點
-
int x2 = r.nextInt(w);
-
int y2 = r.nextInt(h);
-
// 設定干擾線的寬度
-
g2.setStroke(new BasicStroke(1.5F));
-
// 干擾線的顏色
-
g2.setColor(Color.blue);
-
// 將當前這條幹擾線畫出來
-
g2.drawLine(x1, y1, x2, y2);
-
}
-
}
-
/**
-
* <p>Discription:[得到codes的長度內的隨機數 並使用charAt 取得隨機數位置上的codes中的字元]</p>
-
* Created on 2018年3月1日 下午5:58:25
-
* @return char 返回隨機字元
-
* @author:[全冉]
-
*/
-
private char randomChar() {
-
int index = r.nextInt(codes.length());
-
return codes.charAt(index);
-
}
-
/**
-
* <p>Discription:[建立一張驗證碼的圖片]</p>
-
* Created on 2018年3月1日 下午6:00:42
-
* @return BufferedImage 返回一張圖片
-
* @author:[全冉]
-
*/
-
public BufferedImage createImage() {
-
// BufferedImage的構造(寬度,高度和圖片型別)
-
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
-
Graphics2D g2 = (Graphics2D) image.getGraphics();
-
// 下面兩行程式碼是為了解決圖片背景色為黑色的問題,我設定成了白色
-
g2.setColor(Color.white);
-
g2.fillRect(0, 0, w, h);
-
// 可變字串儲存圖片裡的文字
-
StringBuilder sb = new StringBuilder();
-
// 向圖中畫四個字元
-
for (int i = 0; i < 4; i++) {
-
String s = randomChar() + "";
-
sb.append(s);
-
float x = i * 1.0F * w / 4;
-
g2.setFont(randomFont());
-
g2.setColor(randomColor());
-
g2.drawString(s, x, h - 5);
-
}
-
this.text = sb.toString();
-
drawLine(image);
-
// 返回圖片
-
return image;
-
}
-
/**
-
* <p>Discription:[得到驗證碼的文字]</p>
-
* Created on 2018年3月1日 下午6:01:00
-
* @return String 返回驗證碼文字
-
* @author:[全冉]
-
*/
-
public String getText() {
-
return text;
-
}
-
}
2.呼叫工具類
-
package com.quanran.invite.controller;
-
import io.swagger.annotations.Api;
-
import io.swagger.annotations.ApiImplicitParam;
-
import io.swagger.annotations.ApiImplicitParams;
-
import io.swagger.annotations.ApiOperation;
-
import io.swagger.annotations.ApiParam;
-
import java.awt.image.BufferedImage;
-
import java.io.OutputStream;
-
import javax.imageio.ImageIO;
-
import javax.servlet.http.HttpServletRequest;
-
import javax.servlet.http.HttpServletResponse;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.bind.annotation.RestController;
-
import com.quanran.visitor.common.util.VerifyCodeUtils;
-
/**
-
* <p>Description: [invite系統裡的邀請碼重新發送介面]</p>
-
* Created on 2017年11月16日 下午2:42:59
-
* @author <a href="mailto: [email protected]">全冉</a>
-
* @version 1.0
-
* Copyright (c) 2017 北京全冉科技有限公司
-
*/
-
@Api(value="invite系統裡的邀請碼重新發送介面", description="invite系統裡的邀請碼重新發送介面")
-
@RestController
-
@RequestMapping("/invitationCode")
-
public class InvitationCodeController {
-
@ApiOperation("簡訊重發的驗證碼介面")
-
@GetMapping("getVerifyCode")
-
@ApiImplicitParams({
-
@ApiImplicitParam(name = "verifyCodeKey", value = "驗證碼的key", required = true, paramType = "query")
-
})
-
public void getVerifyCode(HttpServletRequest request, HttpServletResponse response,
-
@ApiParam(value="驗證碼的key", required = true) @RequestParam String verifyCodeKey) {
-
// 生成驗證碼的圖片
-
VerifyCodeUtils code = new VerifyCodeUtils();
-
BufferedImage image = code.createImage();
-
// 設定響應頭通知瀏覽器以圖片的形式開啟
-
response.setContentType("image/jpeg");
-
// 設定響應頭控制瀏覽器不要快取
-
response.setHeader("Pragma","no-cache");
-
response.setHeader("Cache-Control","no-cache");
-
response.setIntHeader("Expires",-1);
-
// 將圖片變成流寫給瀏覽器
-
OutputStream os=response.getOutputStream();
-
ImageIO.write(image, "jpg", os);
-
// 清空關閉流
-
os.flush();
-
os.close();
-
os=null;
-
response.flushBuffer();
-
}
-
}
備註:verifyCodeKey引數是前臺傳過來的時間戳,開發中,我們會將此時間戳當做key,生成的驗證碼內容當做value,存到redis中