From 71e73ee51661b3bbb83df032dc66c3df3751a629 Mon Sep 17 00:00:00 2001 From: synchronized Date: Fri, 27 Jul 2018 17:08:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD(?= =?UTF-8?q?=E7=BA=AF=E6=95=B0=E5=AD=97=E3=80=81=E7=BA=AF=E5=AD=97=E6=AF=8D?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 18 ++++++++++++- src/main/java/com/wf/captcha/Captcha.java | 25 ++++++++++++++++++- src/main/java/com/wf/captcha/Randoms.java | 3 +++ src/test/java/com/wf/captcha/CaptchaTest.java | 9 ++++--- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 43f803c..ff2ac4c 100644 --- a/README.md +++ b/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(); @@ -194,4 +200,14 @@ public class Test { specCaptcha.out(outputStream); } } -``` \ No newline at end of file +``` + +### 4.3.验证码类型 + + 类型 | 描述 + :--- | :--- + TYPE_DEFAULT | 数字和字母混合 + TYPE_ONLY_NUMBER | 纯数字 + TYPE_ONLY_CHAR | 纯字母 + + diff --git a/src/main/java/com/wf/captcha/Captcha.java b/src/main/java/com/wf/captcha/Captcha.java index fd30d31..51989f1 100644 --- a/src/main/java/com/wf/captcha/Captcha.java +++ b/src/main/java/com/wf/captcha/Captcha.java @@ -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; + } } \ No newline at end of file diff --git a/src/main/java/com/wf/captcha/Randoms.java b/src/main/java/com/wf/captcha/Randoms.java index 6c4127e..6a871c0 100644 --- a/src/main/java/com/wf/captcha/Randoms.java +++ b/src/main/java/com/wf/captcha/Randoms.java @@ -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; // 字符的最大索引,不包括最大值 /** * 产生两个数之间的随机数 diff --git a/src/test/java/com/wf/captcha/CaptchaTest.java b/src/test/java/com/wf/captcha/CaptchaTest.java index 2385e9d..b5d14b8 100644 --- a/src/test/java/com/wf/captcha/CaptchaTest.java +++ b/src/test/java/com/wf/captcha/CaptchaTest.java @@ -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