带加减法的图片验证码(java编程实现)
2013-11-12 16:21
447 查看
最近用java做了一个加法验证码,是在kaptcha的基础上改写的,而kaptcha是一个扩展自 simplecaptcha的验证码库。
// create the text for the image
List<String> capText = createText();
// store the text in the session
String sessionid = UserCookieUtil.getSessionId(request);
cacheManager.setCode(VerifyTypeEnum.IMG_CODE, sessionid, capText.get(1), timeout);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText.get(0));
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
// create the text for the image
private List<String> createText() {
int intFirst, intSec, intTemp, validCodeResult;
String validCode = null;
Random rand = new Random();
intFirst = (int) (Math.random() * 10);
intSec = (int) (Math.random() * 10);
switch (rand.nextInt(3)) {
case 0:
if (intFirst < intSec) {
intTemp = intFirst;
intFirst = intSec;
intSec = intTemp;
}
validCode = intFirst + "-" + intSec + "=?";
validCodeResult = intFirst - intSec;
break;
case 1:
validCode = intFirst + "+" + intSec + "=?";
validCodeResult = intFirst + intSec;
break;
default:
validCode = intFirst + "*" + intSec + "=?";
validCodeResult = intFirst * intSec;
break;
}
List<String> list = new ArrayList<String>();
list.add(validCode);
list.add(String.valueOf(validCodeResult));
return list;
}
下面再补充几种验证码
(1)// 由0-9组成的全数字验证码
public static void numCode() {
System.out.print("获取4位数字验证码:");
for (int i = 0; i < 4; i++) {
int n = rd.nextInt(10);
System.out.print(n + " ");
}
System.out.println();
}
(2)// 英文字母和标点符号组成的字符验证码
public static void charCode() {
System.out.print("获取4位字符验证码:");
for (int i = 0; i < 4; i++) {
int n = 65 + rd.nextInt(58);
System.out.print((char) n);
}
System.out.println();
}
(3)// 全部由中文组成的验证码
public static void chineseCode() {
System.out.print("获取4位汉字验证码:");
for (int i = 0; i < 4; i++) {
int n = 20000 + rd.nextInt(10000);
System.out.print((char) n);
}
System.out.println();
}
(4)// 字符+数字的混合验证码
public static void mixCode() {
System.out.print("\n获取的5位混合验证码:");
for (int i = 0; i < 4; i++) {
int n = rd.nextInt(123);
if (n < 65) {
System.out.print(n % 10);
} else {
System.out.print((char) n);
}
}
}
// create the text for the image
List<String> capText = createText();
// store the text in the session
String sessionid = UserCookieUtil.getSessionId(request);
cacheManager.setCode(VerifyTypeEnum.IMG_CODE, sessionid, capText.get(1), timeout);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText.get(0));
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
// create the text for the image
private List<String> createText() {
int intFirst, intSec, intTemp, validCodeResult;
String validCode = null;
Random rand = new Random();
intFirst = (int) (Math.random() * 10);
intSec = (int) (Math.random() * 10);
switch (rand.nextInt(3)) {
case 0:
if (intFirst < intSec) {
intTemp = intFirst;
intFirst = intSec;
intSec = intTemp;
}
validCode = intFirst + "-" + intSec + "=?";
validCodeResult = intFirst - intSec;
break;
case 1:
validCode = intFirst + "+" + intSec + "=?";
validCodeResult = intFirst + intSec;
break;
default:
validCode = intFirst + "*" + intSec + "=?";
validCodeResult = intFirst * intSec;
break;
}
List<String> list = new ArrayList<String>();
list.add(validCode);
list.add(String.valueOf(validCodeResult));
return list;
}
下面再补充几种验证码
(1)// 由0-9组成的全数字验证码
public static void numCode() {
System.out.print("获取4位数字验证码:");
for (int i = 0; i < 4; i++) {
int n = rd.nextInt(10);
System.out.print(n + " ");
}
System.out.println();
}
(2)// 英文字母和标点符号组成的字符验证码
public static void charCode() {
System.out.print("获取4位字符验证码:");
for (int i = 0; i < 4; i++) {
int n = 65 + rd.nextInt(58);
System.out.print((char) n);
}
System.out.println();
}
(3)// 全部由中文组成的验证码
public static void chineseCode() {
System.out.print("获取4位汉字验证码:");
for (int i = 0; i < 4; i++) {
int n = 20000 + rd.nextInt(10000);
System.out.print((char) n);
}
System.out.println();
}
(4)// 字符+数字的混合验证码
public static void mixCode() {
System.out.print("\n获取的5位混合验证码:");
for (int i = 0; i < 4; i++) {
int n = rd.nextInt(123);
if (n < 65) {
System.out.print(n % 10);
} else {
System.out.print((char) n);
}
}
}
相关文章推荐
- SpringMVC 实现国际化与图片验证码
- 无线OSS-高精度整数加法(加数可以为负数,应实现高精度加减法)
- Java编程思想里的泛型实现一个堆栈类
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- 复数类(能实现加减法)
- Java编程实现邻接矩阵表示稠密图代码示例
- 使用JSP+Servlet实现图片验证码
- java编程中字节流转换成字符流的实现方法
- php中日期加减法运算实现代码
- Java编程实现Math.sqart(开根号)方法
- asp汉字中文图片验证码的实现代码
- 实现高效Java编程的9条基础规则
- Java编程实现文件编码转换
- 扑克牌24点游戏的所有组合解决方案,java编程实现
- ASP.NET 2.0 HttpHandler实现生成图片验证码(示例代码下载)
- java编程!---实现任意数组的倒叙
- jsp图片验证码实现及getOutputStream() has already been called for this response异常
- node实现登录图片验证码的示例代码
- Java编程实现直接插入排序代码示例
- 用java编程实现Diffie-Hellman算法