1 添加easy-captcha依赖

<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
2 汉字验证码
package com.wanmait.demo;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
//中文验证码
Captcha captcha = new ChineseCaptcha();
//验证码保存到图片文件
captcha.out(new FileOutputStream("c:/code.gif"));
//获得生成的验证码
String code = captcha.text();
System.out.println(code);
}
}
3 字符数字验证码
package com.wanmait.demo;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Captcha captcha = new SpecCaptcha(100,40,4);
//SpecCaptcha(宽,高,位数)
//设置类型
captcha.setCharType(Captcha.TYPE_DEFAULT);
//设置字体大小
captcha.setCharType(Captcha.FONT_9);
//保存成为图片
captcha.out(new FileOutputStream("c:/code.gif"));
String checkCode = captcha.text();
System.out.println(checkCode);//验证码
}
}
4 easy-captcha提供的验证码类
SpecCaptcha
png类型的静态图片验证码
主要是数字字符组成的验证码
GifCaptcha
gif类型的图片验证码
ChineseCaptcha
gif类型的中文汉字验证码
ArithmeticCaptcha
算术类型的验证码
数字字符类型的验证码,可以选择验证码组合的方式
TYPE_DEFAULT 数字字母混合
TYPE_ONLY_NUMBER 数字
TYPE_ONLY_CHAR 字符
TYPE_NUM_AND_UPPER 数字和大写字母混合

0条评论
点击登录参与评论