admin
2021-04-08 d7a3014c38dbb1061cba70e7dbb49d58831e6399
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.yeshi.buwan.util;
 
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.Random;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
/**
 * 图片验证码生成器
 * 
 * @author Administrator
 * 
 */
@Entity
public class IdentifyingCode {
    /**
     * 验证码图片的宽度。
     */
    @Id
    @GeneratedValue
    private int width = 80;
    /**
     * 验证码图片的高度。
     */
    private int height = 30;
    /**
     * 验证码的数量。
     */
    private Random random = new Random();
 
    public IdentifyingCode() {
    }
 
    /**
     * 生成随机颜色
     * 
     * @param fc
     *            前景色
     * @param bc
     *            背景色
     * @return Color对象,此Color对象是RGB形式的。
     */
    public Color getRandomColor(int fc, int bc) {
        if (fc > 255)
            fc = 200;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }
 
    /**
     * 绘制干扰线
     * 
     * @param g
     *            Graphics2D对象,用来绘制图像
     * @param nums
     *            干扰线的条数
     */
    public void drawRandomLines(Graphics2D g, int nums) {
        g.setColor(this.getRandomColor(160, 200));
        for (int i = 0; i < nums; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(12);
            int y2 = random.nextInt(12);
            g.drawLine(x1, y1, x2, y2);
        }
    }
 
    /**
     * 获取随机字符串, 此函数可以产生由大小写字母,汉字,数字组成的字符串
     * 
     * @param length
     *            随机字符串的长度
     * @return 随机字符串
     */
    public String drawRandomString(int length, Graphics2D g) {
        StringBuffer strbuf = new StringBuffer();
        String temp = "";
        int itmp = 0;
        for (int i = 0; i < length; i++) {
            itmp = random.nextInt(26) + 65;
            temp = String.valueOf((char) itmp);
            Color color = new Color(20 + random.nextInt(20),
                    20 + random.nextInt(20), 20 + random.nextInt(20));
            g.setColor(color);
            // 想文字旋转一定的角度
            AffineTransform trans = new AffineTransform();
            trans.rotate(random.nextInt(45) * 3.14 / 180, 15 * i + 8, 7);
            // 缩放文字
            float scaleSize = random.nextFloat() + 0.8f;
            if (scaleSize > 1f)
                scaleSize = 1f;
            trans.scale(scaleSize, scaleSize);
            g.setTransform(trans);
            g.drawString(temp, 15 * i + 18, 14);
 
            strbuf.append(temp);
        }
        g.dispose();
        return strbuf.toString();
    }
 
    public int getWidth() {
        return width;
    }
 
    public void setWidth(int width) {
        this.width = width;
    }
 
    public int getHeight() {
        return height;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
}