懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 package com.jcdm.framework.config;
2
3 import java.util.Random;
4 import com.google.code.kaptcha.text.impl.DefaultTextCreator;
5
6 /**
7  * 验证码文本生成器
8  *
9  * @author jc
10  */
11 public class KaptchaTextCreator extends DefaultTextCreator
12 {
13     private static final String[] CNUMBERS = "0,1,2,3,4,5,6,7,8,9,10".split(",");
14
15     @Override
16     public String getText()
17     {
18         Integer result = 0;
19         Random random = new Random();
20         int x = random.nextInt(10);
21         int y = random.nextInt(10);
22         StringBuilder suChinese = new StringBuilder();
23         int randomoperands = random.nextInt(3);
24         if (randomoperands == 0)
25         {
26             result = x * y;
27             suChinese.append(CNUMBERS[x]);
28             suChinese.append("*");
29             suChinese.append(CNUMBERS[y]);
30         }
31         else if (randomoperands == 1)
32         {
33             if ((x != 0) && y % x == 0)
34             {
35                 result = y / x;
36                 suChinese.append(CNUMBERS[y]);
37                 suChinese.append("/");
38                 suChinese.append(CNUMBERS[x]);
39             }
40             else
41             {
42                 result = x + y;
43                 suChinese.append(CNUMBERS[x]);
44                 suChinese.append("+");
45                 suChinese.append(CNUMBERS[y]);
46             }
47         }
48         else
49         {
50             if (x >= y)
51             {
52                 result = x - y;
53                 suChinese.append(CNUMBERS[x]);
54                 suChinese.append("-");
55                 suChinese.append(CNUMBERS[y]);
56             }
57             else
58             {
59                 result = y - x;
60                 suChinese.append(CNUMBERS[y]);
61                 suChinese.append("-");
62                 suChinese.append(CNUMBERS[x]);
63             }
64         }
65         suChinese.append("=?@" + result);
66         return suChinese.toString();
67     }
68 }