?? radnom(6wei).jsp
字號:
<%@ page language="java" pageEncoding="gb2312"%>
<%@ page contentType="image/jpeg" %>
<%@ page import="java.awt.*,java.awt.image.*" %>
<%@ page import="java.util.*,javax.imageio.*" %>
<%!
//產生隨機顏色函數
Color getRandColor(int fc,int bc){
Random r=new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int red=fc+r.nextInt(bc-fc);
int green=fc+r.nextInt(bc-fc);
int blue=fc+r.nextInt(bc-fc);
return new Color(red,green,blue);
}
%>
<%
//設置頁面不緩存
response.setHeader("Pragma","NO-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
//創建隨機類
Random r=new Random();
//在內存中創建圖像,寬度為width,高度為height
int width=90,height=20;
BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取圖形上下文環境
Graphics gc=pic.getGraphics();
//設定背景色并進行填充
gc.setColor(getRandColor(200,250));
gc.fillRect(0,0,width,height);
//設定圖形上下文環境字體
gc.setFont(new Font("Times New Roman",Font.PLAIN,18));
//隨機產生200條干擾直線,使圖像中的認證碼不易被其他分析程序探測到
gc.setColor(getRandColor(160,200));
for(int i=0;i<200;i++){
int x1=r.nextInt(width);
int y1=r.nextInt(height);
int x2=r.nextInt(15);
int y2=r.nextInt(15);
gc.drawLine(x1,y1,x2,y2);
}
//隨機產生100個干擾點
gc.setColor(getRandColor(120,240));
for(int i=0;i<100;i++){
int x=r.nextInt(width);
int y=r.nextInt(height);
gc.drawOval(x,y,0,0);
}
//隨機產生4位數字的驗證碼
String RS="";
String rn="";
for(int i=0;i<6;i++){
//產生10以內的隨機數字rn
rn=String.valueOf(r.nextInt(10));
RS+=rn;
//將認證碼用drawString函數顯示到圖像里
gc.setColor(new Color(20+r.nextInt(110),20+r.nextInt(110),20+r.nextInt(110)));
gc.drawString(rn,13*i+6,16);
}
//釋放圖形上下文環境
gc.dispose();
//將認證碼RS存放到session中共享
session.setAttribute("random",RS);
//輸出生成后的驗證碼圖像到頁面
ImageIO.write(pic,"JPEG",response.getOutputStream());
%>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -