java加减法验证码_带加减法的图片验证码(java编程实现)

最近用java做了一个加法验证码,是在kaptcha的基础上改写的,而kaptcha是一个扩展自simplecaptcha的验证码库。

// create the text for the image

ListcapText = 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 ListcreateText() {

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;

}

Listlist = new ArrayList();

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值