mirror of
https://github.com/whvcse/EasyCaptcha.git
synced 2024-11-23 01:59:19 +08:00
增加验证码类型设置功能(纯数字、纯字母)
This commit is contained in:
parent
371acb3198
commit
71e73ee516
16
README.md
16
README.md
@ -164,6 +164,9 @@ public class Test {
|
||||
// 设置字体
|
||||
gifCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32)); // 有默认字体,可以不用设置
|
||||
|
||||
// 设置类型,纯数字、纯字母、字母数字混合
|
||||
gifCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
|
||||
|
||||
// 生成的验证码
|
||||
String code = gifCaptcha.text();
|
||||
|
||||
@ -187,6 +190,9 @@ public class Test {
|
||||
// 设置字体
|
||||
specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32)); // 有默认字体,可以不用设置
|
||||
|
||||
// 设置类型,纯数字、纯字母、字母数字混合
|
||||
specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
|
||||
|
||||
// 生成的验证码
|
||||
String code = specCaptcha.text();
|
||||
|
||||
@ -195,3 +201,13 @@ public class Test {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4.3.验证码类型
|
||||
|
||||
类型 | 描述
|
||||
:--- | :---
|
||||
TYPE_DEFAULT | 数字和字母混合
|
||||
TYPE_ONLY_NUMBER | 纯数字
|
||||
TYPE_ONLY_CHAR | 纯字母
|
||||
|
||||
|
||||
|
@ -14,6 +14,10 @@ public abstract class Captcha extends Randoms {
|
||||
protected int width = 130; // 验证码显示宽度
|
||||
protected int height = 48; // 验证码显示高度
|
||||
private String chars = null; // 当前验证码
|
||||
protected int charType = TYPE_DEFAULT; // 验证码类型,1字母数字混合,2纯数字,3纯字母
|
||||
public static final int TYPE_DEFAULT = 1; // 字母数字混合
|
||||
public static final int TYPE_ONLY_NUMBER = 2; // 纯数字
|
||||
public static final int TYPE_ONLY_CHAR = 3; // 纯字母
|
||||
|
||||
/**
|
||||
* 生成随机验证码
|
||||
@ -21,10 +25,21 @@ public abstract class Captcha extends Randoms {
|
||||
* @return 验证码字符数组
|
||||
*/
|
||||
protected char[] alphas() {
|
||||
"".toLowerCase();
|
||||
"".toUpperCase();
|
||||
char[] cs = new char[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
switch (charType) {
|
||||
case 2:
|
||||
cs[i] = alpha(numMaxIndex);
|
||||
break;
|
||||
case 3:
|
||||
cs[i] = alpha(charMinIndex, charMaxIndex);
|
||||
break;
|
||||
default:
|
||||
cs[i] = alpha();
|
||||
}
|
||||
}
|
||||
chars = new String(cs);
|
||||
return cs;
|
||||
}
|
||||
@ -115,4 +130,12 @@ public abstract class Captcha extends Randoms {
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getCharType() {
|
||||
return charType;
|
||||
}
|
||||
|
||||
public void setCharType(int charType) {
|
||||
this.charType = charType;
|
||||
}
|
||||
}
|
@ -12,6 +12,9 @@ public class Randoms {
|
||||
public static final char ALPHA[] = {'2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'G', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
|
||||
protected static final int numMaxIndex = 8; // 数字的最大索引,不包括最大值
|
||||
protected static final int charMinIndex = numMaxIndex; // 字符的最小索引,包括最小值
|
||||
protected static final int charMaxIndex = ALPHA.length; // 字符的最大索引,不包括最大值
|
||||
|
||||
/**
|
||||
* 产生两个数之间的随机数
|
||||
|
@ -14,9 +14,12 @@ public class CaptchaTest {
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
SpecCaptcha specCaptcha = new SpecCaptcha();
|
||||
specCaptcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
|
||||
System.out.println(specCaptcha.text());
|
||||
//specCaptcha.out(new FileOutputStream(new File("D:/a/aa.png")));
|
||||
specCaptcha.out(new FileOutputStream(new File("D:/a/aa" + i + ".png")));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Loading…
Reference in New Issue
Block a user