java 多种验证码

java 多种验证码

  • 1.SpringBoot 引入jar包
  • 2. java 导入jar包
  • 3. 代码
  • 4. 效果图

1.SpringBoot 引入jar包


   com.github.whvcse
   easy-captcha
   1.6.2

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 stub
        code1();
        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;base64
        captcha.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. 效果图

1在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

你可能感兴趣的:(SpringBoot,java,java,开发语言)