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

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

  1. // create the text for the image
  2. List<String> capText = createText();
  3. // store the text in the session
  4. String sessionid = UserCookieUtil.getSessionId(request);
  5. cacheManager.setCode(VerifyTypeEnum.IMG_CODE, sessionid, capText.get(1), timeout);
  6. // create the image with the text
  7. BufferedImage bi = captchaProducer.createImage(capText.get(0));
  8. ServletOutputStream out = response.getOutputStream();
  9. // write the data out
  10. ImageIO.write(bi, "jpg", out);
  11. try {
  12.     out.flush();
  13. } finally {
  14.     out.close();
  15. } 

 

 

  1. // create the text for the image
  2. private List<String> createText() {
  3.         int intFirst, intSec, intTemp, validCodeResult;
  4.         String validCode = null;
  5.         Random rand = new Random();
  6.         intFirst = (int) (Math.random() * 10);
  7.         intSec = (int) (Math.random() * 10);
  8.         switch (rand.nextInt(3)) {
  9.         case 0:
  10.             if (intFirst < intSec) {
  11.                 intTemp = intFirst;
  12.                 intFirst = intSec;
  13.                 intSec = intTemp;
  14.             }
  15.             validCode = intFirst + "-" + intSec + "=?";
  16.             validCodeResult = intFirst - intSec;
  17.             break;
  18.         case 1:
  19.             validCode = intFirst + "+" + intSec + "=?";
  20.             validCodeResult = intFirst + intSec;
  21.             break;
  22.         default:
  23.             validCode = intFirst + "*" + intSec + "=?";
  24.             validCodeResult = intFirst * intSec;
  25.             break;
  26.         }
  27.         List<String> list = new ArrayList<String>();
  28.         list.add(validCode);
  29.         list.add(String.valueOf(validCodeResult));
  30.         return list;
  31.     }

下面再补充几种验证码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值