forked from GabrielNat1/SpringBootApplication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaptchaUtil.java
More file actions
28 lines (23 loc) · 834 Bytes
/
CaptchaUtil.java
File metadata and controls
28 lines (23 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.example.spring_boot_project.Security;
import cn.apiclub.captcha.Captcha;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
public class CaptchaUtil {
public static Captcha createCaptcha(int width, int height){
return new Captcha.Builder(width, height)
.addText()
.addBackground()
.addNoise()
.build();
}
public static String encodeCaptcha(Captcha c){
try(ByteArrayOutputStream o = new ByteArrayOutputStream()){
ImageIO.write(c.getImage(), "jpg", o);
return Base64.getEncoder().encodeToString(o.toByteArray());
} catch (IOException e){
throw new RuntimeException("error encoding Captcha", e);
}
}
}