国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 高要市| 中阳县| 茂名市| 双柏县| 上虞市| 周口市| 西林县| 来凤县| 武冈市| 建阳市| 庐江县| 酉阳| 平罗县| 荥经县| 鄂尔多斯市| 普兰店市| 翼城县| 海宁市| 琼海市| 桐城市| 东城区| 印江| 和静县| 石渠县| 兴国县| 宝应县| 正镶白旗| 房山区| 渑池县| 玛沁县| 明溪县| 惠水县| 安徽省| 百色市| 防城港市| 勐海县| 依安县| 苗栗市| 四川省| 霍州市| 铁力市|