java 多种验证码
- 1.SpringBoot 引入jar包
- 2. java 导入jar包
- 3. 代码
- 4. 效果图
1.SpringBoot 引入jar包
<dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId><version>1.6.2</version>
</dependency>
2. java 导入jar包
easy-captcha-1.6.2-RELEASE.jar
// 下载地址
// https://www.123pan.com/s/8oxtVv-uypph.html提取码:ddrl
3. 代码
public class verificationCode {public static void main(String[] args) throws FileNotFoundException {// TODO Auto-generated method stubcode1();code2();code3();code4();}public static void code1() throws FileNotFoundException {//字符验证码SpecCaptcha captcha = new SpecCaptcha(130, 48);String text = captcha.text();// 获取验证码的字符System.out.println("验证码:"+text);captcha.out(new FileOutputStream(new File("D:\\aSaveProject\\codePlay\\test1.png")));输出图片流System.out.println(captcha.toBase64());不加 data:image/png;base64captcha.toBase64("");}public static void code2() throws FileNotFoundException {//算术验证码Captcha captcha1 = new ArithmeticCaptcha();//获取本次生成的验证码String text1 = captcha1.text();System.out.println(text1);captcha1.out(new FileOutputStream(new File("D:\\aSaveProject\\codePlay\\test2.png")));}public static void code3() throws FileNotFoundException {//中文验证码Captcha captcha2 = new ChineseCaptcha();//获取本次生成的验证码String text1 = captcha2.text();System.out.println(text1);captcha2.out(new FileOutputStream(new File("D:\\aSaveProject\\codePlay\\test3.png")));}public static void code4() throws FileNotFoundException {//gif验证码// 三个参数分别为宽、高、位数GifCaptcha gifCaptcha = new GifCaptcha(100, 48, 4);// 设置类型:字母数字混合gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);//获取验证码String text = gifCaptcha.text();System.out.println(text);
// System.out.println(gifCaptcha.toBase64());gifCaptcha.out(new FileOutputStream(new File("D:\\aSaveProject\\codePlay\\test4.gif")));}
}
4. 效果图