JSP中生成驗證碼

JSP中生成驗證碼
這是我根據別人代碼完善的,現在可以運行
VerifyCode.java
package image;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 
 * 
@author Wencheng Ma
 *
 
*/

public class VerifyCode {
    
    
public String s = "";

    
public VerifyCode(){
        
    }

    
    
/**
     * get a new color by ranges
     * 
@param i
     * 
@param j
     * 
@return Color
     
*/

    
public Color getColor(int i, int j) {
        Random random 
= new Random();
        
        
if (i > 255{
            i 
= 255;
        }

        
if (j > 255{
            j 
= 255;
        }

        
        
int r = i + random.nextInt(j - i);
        
int g = i + random.nextInt(j - i);
        
int b = i + random.nextInt(j - i);
        
        
return new Color(r, g, b);
    }

    
    
/**
     * create a verify code of image
     * 
@return
     
*/

    
public BufferedImage createVerifyCode() {
        
        
//在內存中創建圖像
        int width = 60;
        
int height = 20;
        BufferedImage bufferedImage 
= new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        
        
//獲取圖像上下文
        Graphics graphics = bufferedImage.getGraphics();
        
        
//隨機類
        Random random = new Random();
        
        
//設置背景色
        graphics.setColor(getColor(200250));
        graphics.fillRect(
00, width, height);
        
        
//設置字體
        graphics.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        
        
//155條隨機干擾點
        graphics.setColor(getColor(160200));
        
for (int i = 0; i < 155; i++{
            
int x1 = random.nextInt(width);
            
int y1 = random.nextInt(height);
            
int x2 = random.nextInt(12);
            
int y2 = random.nextInt(12);
            graphics.drawOval(x1, y1, x1 
+ x2, y1 + y2);
//            干擾線
//            graphics.drawLine(x1, y1, x1 + x2, y1 + y2);
        }

        
        
//4位隨機驗證碼
        for (int i = 0; i < 4; i++{
            String rand 
= String.valueOf(random.nextInt(10));
            s 
+= rand;
            graphics.setColor(
new Color(20 + random.nextInt(100), 20 + random.nextInt(100), 20 + random.nextInt(100)));
            graphics.drawString(rand, 
13 * i + 616);
        }

        
        
//圖像生效
        graphics.dispose();
        
        
return bufferedImage;
    }

}



JSP頁面
<jsp:directive.page import="image.VerifyCode" />
<jsp:directive.page import="javax.imageio.ImageIO"/>
<jsp:useBean id="image" scope="session" class="image.VerifyCode"/> 

<%
                
//設置頁面不緩存 
                response.setHeader("Pragma""No-cache");
                response.setHeader(
"Cache-Control""no-cache");
                response.setDateHeader(
"Expires"0);

                
// 將認證碼存入SESSION 
                session.setAttribute("rand", image.s);

                
// 輸出圖象到頁面 
                ImageIO.write(image.createVerifyCode(), "JPEG", response.getOutputStream());
%>


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章