Java生成圖片驗證碼

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//下面是springmvc的包,僅用於路徑跳轉
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("checkCode")

public class ZTestCodeController {

//定義圖片的寬度
private int width = 200;

//定義圖片的高度
private int height = 40;

//生成驗證碼的個數
private int codeCount = 4;

//x座標
private int xx = 30;

//y座標
private int codeY = 32;

//字體高度
private int fontHeight = 38;

//驗證碼圖片上的字符系列
char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
			'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

/**
*生成的驗證碼會用servletOutputStream輸出出去
*所以使用void做爲返回類型
*/
@RequestMapping("/checkCode")
public void getCheckCode(HttpServletRequest req,HttpServletResponse resp,HttpSession session) throws IOException

//定義圖像BufferedImage,用於描述具備可訪問圖像數據緩衝區的Image
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//getGraphics和createGraphics都返回相同的graphics2D
Graphics gd = buffImg.getGraphics();

//建立隨機數生成器
Random r = new Random();

//將圖像上下文的當前顏色設置爲指定顏色
gd.setColor(Color.WHITE);

//由於要生成矩形驗證框,這裏先填充指定的矩形,該矩形四個座標分別爲x,y,x+width-1,y+height-1,獲得的矩形覆蓋width像素寬height像素高的區域,使用圖形上下文的當前顏色填充該矩形
gd.fillRect(0, 0, width, height);

//設置字體樣式
Font font = new Font("Fixedsys",Font.BOLD,fontHeight);

//將此圖形上下文的字體設置問指定字體
gd.setFont(font);

//默認左上角的矩形點座標爲0,0,這裏繪製指定矩形的邊框
gd.setColor(Color.BLACK);
gd.drawRect(0, 0, width - 1, height - 1);

// 隨機產生40條幹擾線,使圖象中的認證碼不易被其它程序探測到。
gd.setColor(Color.BLACK);
for (int i = 0; i < 20; i++) {
	int x = random.nextInt(width);
	int y = random.nextInt(height);
	int xl = random.nextInt(12);
	int yl = random.nextInt(12);
	gd.drawLine(x, y, x + xl, y + yl);
}

// randomCode用於保存隨機產生的驗證碼,以便用戶登陸後進行驗證。
StringBuffer randomCode = new StringBuffer();

//設置驗證碼的顏色
int red = 0, green = 0, blue = 0;

// 隨機產生codeCount數字的驗證碼,並完成拼接到一塊兒
for (int i = 0; i < codeCount; i++) {

        // 獲得隨機產生的驗證碼數字。
	String code = String.valueOf(codeSequence[random.nextInt(36)]);
	
	// 產生隨機的顏色份量來構造顏色值,這樣輸出的每位數字的顏色值都將不一樣。
	red = random.nextInt(255);
	green = random.nextInt(255);
	blue = random.nextInt(255);

	// 用隨機產生的顏色將驗證碼繪製到圖像中。
	gd.setColor(new Color(red, green, blue));
	gd.drawString(code, (i + 1) * xx, codeY);

	// 將產生的四個隨機數組合在一塊兒。
	randomCode.append(code);
}

// 將四位數字的驗證碼保存到Session中。
httpSession.setAttribute("code", randomCode.toString());

// 禁止圖像緩存。
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);

resp.setContentType("image/jpeg");

// 將圖像輸出到Servlet輸出流中。
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();

}

}


路徑:http://www.cnblogs.com/seed_lee/archive/2011/05/10/2042355.html
相關文章
相關標籤/搜索