国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

用jsp做個(gè)有彩紋的隨機(jī)碼

2024-09-05 00:19:55
字體:
供稿:網(wǎng)友
<%@ page contenttype="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
color getrandcolor(int fc,int bc){//給定范圍獲得隨機(jī)顏色
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);
}
%>
<%
//設(shè)置頁面不緩存
response.setheader("pragma","no-cache");
response.setheader("cache-control","no-cache");
response.setdateheader("expires", 0);

// 在內(nèi)存中創(chuàng)建圖象
int width=60, height=20;
bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);

// 獲取圖形上下文
graphics g = image.getgraphics();

//生成隨機(jī)類
random random = new random();

// 設(shè)定背景色
g.setcolor(getrandcolor(200,250));
g.fillrect(0, 0, width, height);

//設(shè)定字體
g.setfont(new font("times new roman",font.plain,18));

//畫邊框
g.setcolor(new color(255,255,255));
g.drawrect(0,0,width-1,height-1);

// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測到
g.setcolor(getrandcolor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextint(width);
int y = random.nextint(height);
int xl = random.nextint(12);
int yl = random.nextint(12);
g.drawline(x,y,x+xl,y+yl);
}

// 取隨機(jī)產(chǎn)生的認(rèn)證碼(4位數(shù)字)
string srand="";
for (int i=0;i<4;i++){
string rand=string.valueof(random.nextint(10));
srand+=rand;
// 將認(rèn)證碼顯示到圖象中
g.setcolor(new color(20+random.nextint(110),20+random.nextint(110),20+random.nextint(110)));
//調(diào)用函數(shù)出來的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
g.drawstring(rand,13*i+6,16);
}

// 將認(rèn)證碼存入session
session.setattribute("rand",srand);

// 圖象生效
g.dispose();

// 輸出圖象到頁面
imageio.write(image, "jpeg", response.getoutputstream());

%>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 铜梁县| 宁津县| 聊城市| 鄂尔多斯市| 和硕县| 武清区| 徐闻县| 松阳县| 老河口市| 会宁县| 高平市| 张家口市| 太湖县| 筠连县| 南川市| 东丰县| 新民市| 留坝县| 赣州市| 凉山| 闽侯县| 古交市| 准格尔旗| 津市市| 双流县| 宝兴县| 长武县| 扶风县| 永顺县| 松滋市| 靖西县| 德庆县| 金溪县| 那曲县| 噶尔县| 六安市| 通江县| 新竹县| 涟水县| 明溪县| 凤城市|