1
0
mirror of https://github.com/whvcse/EasyCaptcha.git synced 2024-11-14 21:59:19 +08:00

增加验证码类型设置功能(纯数字、纯字母)

This commit is contained in:
synchronized 2018-07-27 17:08:16 +08:00
parent 371acb3198
commit 71e73ee516
4 changed files with 50 additions and 5 deletions

View File

@ -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();
@ -194,4 +200,14 @@ public class Test {
specCaptcha.out(outputStream);
}
}
```
```
### 4.3.验证码类型
类型 | 描述
:--- | :---
TYPE_DEFAULT | 数字和字母混合
TYPE_ONLY_NUMBER | 纯数字
TYPE_ONLY_CHAR | 纯字母

View File

@ -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,9 +25,20 @@ public abstract class Captcha extends Randoms {
* @return 验证码字符数组
*/
protected char[] alphas() {
"".toLowerCase();
"".toUpperCase();
char[] cs = new char[len];
for (int i = 0; i < len; i++) {
cs[i] = alpha();
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;
}
}

View File

@ -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; // 字符的最大索引不包括最大值
/**
* 产生两个数之间的随机数

View File

@ -14,9 +14,12 @@ public class CaptchaTest {
@Test
public void test() throws Exception {
SpecCaptcha specCaptcha = new SpecCaptcha();
System.out.println(specCaptcha.text());
//specCaptcha.out(new FileOutputStream(new File("D:/a/aa.png")));
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" + i + ".png")));
}
}
@Test