紫氣東來
天下皆知美之為美,斯惡已;皆知善之為善,斯不善已。故有無相生,難易相成,長短相形,高下相傾,音聲相和,前後相隨。是聖人處無為之事,行不言之教,萬物作焉而不辭。生而不有,為而不恃,功成而不居。夫唯弗居,是以不去。
servlet生成驗證碼
import
java.awt.Color;
import
java.awt.Font;
import
java.awt.Graphics2D;
import
java.awt.image.BufferedImage;
import
java.io.File;
import
java.util.LinkedList;
import
java.util.Random;
import
javax.imageio.ImageIO;
import
javax.servlet.http.HttpServletResponse;
import
com.sun.image.codec.jpeg.JPEGCodec;
import
com.sun.image.codec.jpeg.JPEGImageEncoder;
//
import java.awt.Graphics;
public
class
testimage
{
public
String sRand
=
""
;
public
testimage()
{
}
public
Color getRandColor(
int
fc,
int
bc)
{
//
給定范圍獲得隨機顏色
Random random
=
new
Random();
if
(fc
>
255
) fc
=
255
;
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);
}
/** */
/**
* 通過文件創(chuàng)建圖像
* 格式為jpg類型
*
*/
public
void
creatImage(String fileName,String content)
{
//
在內(nèi)存中創(chuàng)建圖象
int
width
=
60
, height
=
20
;
BufferedImage image
=
new
BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//
獲取圖形上下文
Graphics2D g
=
image.createGraphics();
//
Graphics g=image.getGraphics();
//
生成隨機類
Random random
=
new
Random();
//
設定背景色
g.setColor(getRandColor(
200
,
250
));
g.fillRect(
0
,
0
, width, height);
//
設定字體
g.setFont(
new
Font(
"
Times New Roman
"
,Font.PLAIN,
18
));
g.setColor(Color.black);
//
黑色文字
g.drawString(content,
10
,
15
);
g.dispose();
try
{
File f
=
new
File(fileName);
if
(
!
f.exists())
{
f.createNewFile();
}
else
{
Thread.sleep(
200
);
f.delete();
f.createNewFile();
}
ImageIO.write(image,
"
jpg
"
, f);
}
catch
(Exception e)
{
e.printStackTrace();
}
}
/** */
/**
* 創(chuàng)建圖像
* 格式為jpg類型
*
@param
content - String 圖片輸出內(nèi)容
*
@return
java.awt.image.BufferedImage
*
*/
public
BufferedImage getBufferedImage(String content)
{
int
width
=
60
, height
=
20
;
return
getBufferedImage(content,width,height);
}
/** */
/**
* 創(chuàng)建圖像
* 格式為jpg類型
*
@param
content - String 圖片輸出內(nèi)容
*
@param
width - int 圖片寬度
*
@param
height - int 圖片高度
*
@return
java.awt.image.BufferedImage
*
*/
public
BufferedImage getBufferedImage(String content,
int
width,
int
height)
{
//
在內(nèi)存中創(chuàng)建圖象
BufferedImage image
=
new
BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//
獲取圖形上下文
Graphics2D g
=
image.createGraphics();
//
Graphics g=image.getGraphics();
//
生成隨機類
Random random
=
new
Random();
//
設定背景色
g.setColor(getRandColor(
200
,
250
));
g.fillRect(
0
,
0
, width, height);
//
設定字體
g.setFont(
new
Font(
"
Times New Roman
"
,Font.PLAIN,
18
));
g.setColor(Color.black);
//
黑色文字
g.drawString(content,
10
,
15
);
g.dispose();
return
image;
}
/** */
/**
* 將現(xiàn)有BufferedImage融合進Response
*
@param
response - javax.servlet.http.ServletResponse 將使用的response對象
*
@param
img - java.awt.image.BufferedImage
*
*/
public
void
response(HttpServletResponse response,BufferedImage img)
{
try
{
response.setContentType(
"
image/jpg;charset=GB2312
"
);
//
設定輸出的類型
JPEGImageEncoder encoder
=
JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(img);
//
對圖片進行輸出編碼
}
catch
(Exception e)
{
e.printStackTrace();
}
}
/** */
/**
* 返回一個4位的驗證碼
*
*/
private
String getContent()
throws
InterruptedException
{
String content
=
""
;
for
(
int
i
=
0
;i
<
4
;i
++
)
{
content
+=
getChar();
Thread.sleep(
new
Random().nextInt(
10
)
+
10
);
//
休眠以控制字符的重復問題
}
return
content;
}
/** */
/**
* 獲取隨機字符
*
*/
private
char
getChar()
{
Random random
=
new
Random();
char
ch
=
'
0
'
;
LinkedList ls
=
new
LinkedList();
for
(
int
i
=
0
;i
<
10
;i
++
)
{
//
0-9
ls.add(String.valueOf(
48
+
i));
}
for
(
int
i
=
0
;i
<
26
;i
++
)
{
//
A-Z
ls.add(String.valueOf(
65
+
i));
}
for
(
int
i
=
0
;i
<
26
;i
++
)
{
//
a-z
ls.add(String.valueOf(
97
+
i));
}
int
index
=
random.nextInt(ls.size());
if
(index
>
(ls.size()
-
1
))
{
index
=
ls.size()
-
1
;
}
ch
=
(
char
)Integer.parseInt(String.valueOf(ls.get(index)));
return
ch;
}
/** */
/**
測試
*/
//
public static void main(String []args) throws Exception{
//
testimage c=new testimage();
//
c.creatImage("WebRoot/WEB-INF/image/me.jpg",c.getContent());
//
}
}
posted on 2007-07-21 22:39
hugh
閱讀(238)
評論(0)
編輯
收藏
所屬分類:
JAVA
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
IDEA的一些快捷鍵
jdbc 數(shù)據(jù)庫連接池
(轉)正則表達式之道
連接各種數(shù)據(jù)庫寫法
(轉)hashCode()與equals()
(轉)ofbiz工具類介紹
(轉)ofbiz服務引擎
OFBIZ2.0 精簡版本應用概論
(轉)ofbiz入門
(轉)Ofbiz標簽說明
Powered by:
BlogJava
Copyright © hugh
<
2007年7月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
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
1
2
3
4
導航
BlogJava
首頁
新隨筆
聯(lián)系
聚合
管理
統(tǒng)計
隨筆 - 31
文章 - 0
評論 - 4
引用 - 0
公告
小弟打算把硬盤里的所有資料整理到博客里,但是其中收藏了不少網(wǎng)上的文章(個人也記不住作者),請作者見諒!請知道作者的朋友(或作者本人)看到了請告訴小弟,小弟好把作者加上!
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(2)
給我留言
查看公開留言
查看私人留言
隨筆分類
Ajax(1)
(rss)
c/c++
(rss)
DB(3)
(rss)
JAVA(20)
(rss)
Linux(2)
(rss)
other(2)
(rss)
心情隨筆(2)
(rss)
隨筆檔案
2007年12月 (2)
2007年11月 (1)
2007年10月 (2)
2007年9月 (1)
2007年8月 (1)
2007年7月 (24)
收藏夾
常用Eclipse插件地址
(rss)
網(wǎng)站地址
(rss)
搜索
最新評論
1.?re: (轉)Ofbiz標簽說明
頁面中是如何引用的呀
--pwj
2.?re: 連接各種數(shù)據(jù)庫寫法
Class.forName("org.postgresql.Driver").newInstance();
呵呵
--ycyyww
3.?re: (轉)ofbiz服務引擎
我擦。什么亂玩意 。
--我弄你
4.?re: (轉)ofbiz服務引擎
請樓主轉載文章的時候閱后轉載,
很明顯是金山詞霸或google直翻譯的文章!
--chain
閱讀排行榜
1.?Linux關閉 開啟 防火墻 命令(2504)
2.?(轉)ofbiz服務引擎(1296)
3.?一些String方法(1185)
4.?(轉)Ofbiz標簽說明(997)
5.?(轉)ofbiz標簽(773)
評論排行榜
1.?(轉)ofbiz服務引擎(2)
2.?連接各種數(shù)據(jù)庫寫法(1)
3.?(轉)Ofbiz標簽說明(1)
4.?(轉)ofbiz標簽(0)
5.?(轉)jsp語言處理(0)
主站蜘蛛池模板:
洛扎县
|
新乡市
|
华亭县
|
松溪县
|
蓝田县
|
新乡县
|
泽库县
|
布尔津县
|
丰顺县
|
密云县
|
清流县
|
平武县
|
澄江县
|
定结县
|
民勤县
|
铜川市
|
鹤庆县
|
镇平县
|
邻水
|
仪征市
|
靖安县
|
大埔区
|
博客
|
新龙县
|
武胜县
|
克什克腾旗
|
广安市
|
乌审旗
|
崇左市
|
西贡区
|
天长市
|
宁南县
|
新源县
|
阳谷县
|
时尚
|
商都县
|
锡林郭勒盟
|
德清县
|
龙山县
|
腾冲县
|
筠连县
|