随机数验证码:
@Data
public class ImageCode {
//图形中的内容
private String code;
//图片
private ByteArrayInputStream image;
private int width = 400;
private int height = 100;
public static ImageCode getInstance() throws IOException {
return new ImageCode();
}
private ImageCode() throws IOException {
//图像缓冲区 (黑板)
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
//画笔
Graphics graphics = image.getGraphics();
//拿笔涂色画图形
graphics.setColor(new Color(255,255,255));
//画矩形
graphics.fillRect(0,0,width,height);
//字体
graphics.setFont(new Font("宋体",Font.PLAIN,30));
Random random = new Random();
this.code="";
for(int i=0;i<6;i++){
String s = String.valueOf(random.nextInt(10));
this.code += s;
graphics.setColor(new Color(45,31,37));
graphics.drawString(s,(width/6)*i,40);
//画线
graphics.setColor(new Color(100,100,100));
graphics.drawLine((width/6)*i,40,(width/6)*i+25,40-30);
}
graphics.setColor(new Color(100,100,100));
for (int i=0;i<100;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(20);
int y1 = random.nextInt(20);
graphics.drawLine(x,y,x+x1,y+y1);
}
//收笔
graphics.dispose();
ByteArrayInputStream inputStream = null;
ByteOutputStream outputStream = new ByteOutputStream();
try {
//赋值给byteArrayInputStream
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
ImageIO.write(image,"jpeg",imageOutputStream);
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
}catch (Exception e){
System.out.println("生成验证码失败");
}
this.image = inputStream;
}
}
加法验证码(1+8=?):
@Data
public class ImageCode {
//图形中的内容
private String code;
//图片
private ByteArrayInputStream image;
private int width = 400;
private int height = 100;
public static ImageCode getInstance() throws IOException {
return new ImageCode();
}
private ImageCode() throws IOException {
//图像缓冲区 (黑板)
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
//画笔
Graphics graphics = image.getGraphics();
//拿笔涂色画图形
graphics.setColor(new Color(255,255,255));
//画矩形
graphics.fillRect(0,0,width,height);
//字体
graphics.setFont(new Font("宋体",Font.PLAIN,30));
Random random = new Random();
int num1 = random.nextInt(20);
int num2 = random.nextInt(20);
graphics.setColor(new Color(0,200,100));
graphics.drawString(num1+"", (width/6)*0+2,60);
graphics.drawString("+", (width/6)*1+2,60);
graphics.drawString(num2+"", (width/6)*2+2,60);
graphics.drawString("=", (width/6)*3+2,60);
graphics.drawString("?", (width/6)*4+2,60);
int result = num1 + num2;
this.code = result + "";
//画100段干扰线段
graphics.setColor(new Color(100,100,100));
for (int i=0;i<100;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(20);
int y1 = random.nextInt(20);
graphics.drawLine(x,y,x+x1,y+y1);
}
//收笔
graphics.dispose();
ByteArrayInputStream inputStream = null;
ByteOutputStream outputStream = new ByteOutputStream();
try {
//赋值给byteArrayInputStream
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
ImageIO.write(image,"jpeg",imageOutputStream);
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
}catch (Exception e){
System.out.println("生成验证码失败");
}
this.image = inputStream;
}
}
保存验证码图片:
@RestController
@RequestMapping("/code")
public class VerifyCodeController {
String attrName = "verifyCode";
@GetMapping("/generator")
@TokenCheck(required = false)
public void generatorCode(HttpServletRequest request,HttpServletResponse response){
try {
ImageCode imageCode = ImageCode.getInstance();
//验证码的值
String code = imageCode.getCode();
request.getSession().setAttribute(attrName,code);
//验证码图片
ByteArrayInputStream image = imageCode.getImage();
response.setContentType("image/jpeg");
byte[] bytes = new byte[1024];
try(ServletOutputStream out = response.getOutputStream()){
while (image.read(bytes)!=-1){
out.write(bytes);
}
}
}catch (Exception e){
System.out.println("异常");
}
}
@GetMapping("/verity")
@TokenCheck(required = false)
public String verity(String verityCode, HttpServletRequest request){
String s = request.getSession().getAttribute(attrName).toString();
if(verityCode.equals(s)){
return "效验码验证通过";
}
return "效验码验证不通过";
}
}
base64字符串验证码:
String attrName = "verifyCode";
@GetMapping("/generator-base64")
@TokenCheck(required = false)
public String generatorCodeBase64(HttpServletRequest request,HttpServletResponse response){
try {
ImageCode imageCode = ImageCode.getInstance();
//验证码的值
String code = imageCode.getCode();
request.getSession().setAttribute(attrName,code);
//验证码图片
ByteArrayInputStream image = imageCode.getImage();
request.getSession().setAttribute(attrName,code);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int r = 0;
while ((r=image.read(buff,0,1024))>0){
swapStream.write(buff,0,r);
}
byte[] data = swapStream.toByteArray();
return Base64.getEncoder().encodeToString(data);
}catch (Exception e){
System.out.println("异常");
return "";
}
}
@GetMapping("/verity")
@TokenCheck(required = false)
public String verity(String verityCode, HttpServletRequest request){
String s = request.getSession().getAttribute(attrName).toString();
if(verityCode.equals(s)){
return "效验码验证通过";
}
return "效验码验证不通过";
}
}
识别验证码:
<dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>4.5.4</version> </dependency>
public class TesseractTest {
public static void main(String[] args) throws TesseractException {
ITesseract iTesseract = new Tesseract();
//语言包,加进来
iTesseract.setDatapath("语言包具体路径");
//iTesseract.setLanguage("chi_sim");
iTesseract.setLanguage("eng");
File fileDir = new File("文件具体路径");
for(File file:fileDir.listFiles()){
String s = iTesseract.doOCR(file);
System.out.println(file.getName()+"识别后的数字:"+s);
}
}
}