彩色驗證碼實現
??1
public
?
class
?VerifyCode?
{
??2
????
static
?Random?r?
=
?
new
?Random();
??3
????
static
?String?ssource?
=
?
"
ABCDEFGHIJKLMNOPQRSTUVWXYZ
"
??
+
?
"
abcdefghijklmnopqrstuvwxyz
"
?
+
?
"
0123456789
"
;
??4
????
static
?
char
[]?src?
=
?ssource.toCharArray();
??5
????
??6
????
??7
????
//
產生隨機字符串
??8
????
??9
????
private
?
static
?String?randString?(
int
?length)
{
?10
????????
char
[]?buf?
=
?
new
?
char
[length];
?11
????????
int
?rnd;
?12
????????
for
(
int
?i
=
0
;i
<
length;i
++
)
{
?13
????????????rnd?
=
?Math.abs(r.nextInt())?
%
?src.length;
?14
????????????
?15
????????????buf[i]?
=
?src[rnd];
?16
????????}
?17
????????
return
?
new
?String(buf);
?18
????}
?19
????
?20
????
//
調用該方法,產生隨機字符串,
?21
????
//
參數i:?為字符串的長度
?22
????
public
?String?runVerifyCode(
int
?i)
{
?23
????????String?VerifyCode?
=
?randString(i);
?24
????????
return
?VerifyCode;
?25
????}
?26
????
?27
????
?28
????
//
給定范圍獲得隨機顏色
?29
????
public
?Color?getRandColor(
int
?fc,
int
?bc)
?30
????
{
?31
???????Random?random?
=
?
new
?Random();
?32
???????
if
(fc
>
255
)?fc
=
255
;
?33
???????
if
(bc
>
255
)?bc
=
255
;
?34
???????
int
?r
=
fc
+
random.nextInt(bc
-
fc);
?35
???????
int
?g
=
fc
+
random.nextInt(bc
-
fc);
?36
???????
int
?b
=
fc
+
random.nextInt(bc
-
fc);
?37
???????
return
?
new
?Color(r,g,b);
?38
???????}
?39
??
?40
??????
//
調用該方法將得到的驗證碼生成圖象
?41
??????
//
sCode:傳遞驗證碼?w:圖象寬度?h:圖象高度
?42
??????
public
?BufferedImage?CreateImage(String?sCode)
?43
??????
{
?44
??????????
try
{????
?45
??????????????
//
字符的字體
?46
????????????Font?CodeFont?
=
?
new
?Font(
"
Arial?Black
"
,Font.PLAIN,
16
);
?47
????????????
int
?iLength?
=
?sCode.length();????????????????????
//
得到驗證碼長度
?48
????????????
int
?width
=
22
*
iLength,?height
=
20
;????????????????
//
圖象寬度與高度
?49
????????????
int
?CharWidth?
=
?(
int
)(width
-
24
)
/
iLength;????????
//
字符距左邊寬度
?50
????????????
int
?CharHeight?
=
?
16
;????????????????????????????
//
字符距上邊高度
?51
????????????
?52
????????????
//
?在內存中創建圖象
?53
????????????BufferedImage?image?
=
?
new
?BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
?54
????????????
?55
????????????
//
?獲取圖形上下文
?56
????????????Graphics?g?
=
?image.getGraphics();
?57
????????????
?58
????????????
//
生成隨機類
?59
????????????Random?random?
=
?
new
?Random();
?60
????????????
?61
????????????
//
?設定背景色
?62
????????????g.setColor(getRandColor(
200
,
240
));
?63
????????????g.fillRect(
0
,?
0
,?width,?height);
?64
????????????
?65
????????????
//
設定字體
?66
????????????g.setFont(CodeFont);
?67
????????????
?68
????????????
//
畫隨機顏色的邊框
?69
????????????g.setColor(getRandColor(
10
,
50
));
?70
????????????g.drawRect(
0
,
0
,width
-
1
,height
-
1
);
?71
????????????
?72
????????????
//
?隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到
?73
????????????g.setColor(getRandColor(
160
,
200
));
?74
????????????
for
?(
int
?i
=
0
;i
<
155
;i
++
)
?75
????????????
{
?76
??????????????????
int
?x?
=
?random.nextInt(width);
?77
??????????????????
int
?y?
=
?random.nextInt(height);
?78
??????????????????
int
?xl?
=
?random.nextInt(
12
);
?79
??????????????????
int
?yl?
=
?random.nextInt(
12
);
?80
??????????????????g.drawLine(x,y,x
+
xl,y
+
yl);
?81
????????????}
?82
????????????
?83
????
?84
????????????
for
?(
int
?i
=
0
;i
<
iLength;i
++
)
?85
????????????
{
?86
????????????????String?rand?
=
?sCode.substring(i,i
+
1
);?
?87
????????????????
//
?將認證碼顯示到圖象中
?88
????????????????g.setColor(
new
?Color(
20
+
random.nextInt(
60
),
20
+
random.nextInt(
120
),
20
+
random.nextInt(
180
)));
?89
????????????????g.drawString(rand,CharWidth
*
i
+
14
,CharHeight);
?90
????????????}
?91
????????????
//
?圖象生效
?92
????????????g.dispose();
?93
????????????
return
?image;
?94
????????}
catch
(Exception?e)
{
?95
????????????
//
e.printStackTrace();????
?96
????????????
//
System.out.println(e.getMessage());
?97
????????????}
?98
????????
return
?
null
;
?99
????}
100
????
101
????
//
測試
102
????
public
?
static
?
void
?main(String[]?args)
{????
103
????????????
//
VerifyCode?vc?=?new?VerifyCode();
104
????????????
//
String?s1?=?vc.runVerifyCode(4);
105
????????????
//
Fun.DreamNewsTitle;System.out.println(s1);????
106
????????????
//
Image?im?=?vc.CreateImage(s1);
107
????????????
//
Graphics?g?=?im.getGraphics();
108
????????????
//
g.drawImage(im,20,20,this);
109
????????????
//
g.drawString(s1,20,20);
110
????????????
111
????}
????
112
}



??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

posted on 2006-07-17 11:17 javajohn 閱讀(758) 評論(0) 編輯 收藏 所屬分類: 我的記憶