package com.file.test.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.RandomStringUtils;
public class RandomImageServlet extends HttpServlet {
//隨即生成包含驗證碼的字符串
public static String random() {
//20060320 add by wyx
//因爲o和0,l和1很難區分,所以,去掉大小寫的o和l
String str = "";
str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";//初始化種子
return RandomStringUtils.random(4, str);//返回6爲的字符串
}
/**
* 根據要求的數字生成圖片,背景爲白色,字體大小16,字體顏色黑色粗體
* @param num 要生成的數字
* @param out 輸出流
* @throws IOException
*/
public static void render(String num, OutputStream out) throws IOException {
if (num.getBytes().length > 6) {
throw new IllegalArgumentException(
"The length of param num cannot exceed 6.");
}
//設定寬度和高度
int width = 100;
int height = 30;
// 在內存中創建圖象
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics2D g = (Graphics2D) bi.getGraphics();
//畫邊框
java.util.Random random = new java.util.Random();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
//設置字體
Font mFont = new Font("Tahoma", Font.BOLD, 16);
g.setFont(mFont);
g.setColor(Color.BLACK);//設置字體顏色
//畫認證碼,每個認證碼在不同的水平位置
String str1[] = new String[4];
for (int i = 0; i < str1.length; i++) {
str1[i] = num.substring(i, i + 1);
int w = 0;
int x = (i + 1) % 3;
//隨即生成驗證碼字符的水平偏移量
if (x == random.nextInt(3)) {
w = 19 - random.nextInt(7);
} else {
w = 19 + random.nextInt(7);
}
//隨即生成顏色
Color color1 = new Color(random.nextInt(180), random.nextInt(180),
random.nextInt(180));
g.setColor(color1);
g.drawString(str1[i], 20 * i + 10, w);
}
// 隨機產生干擾點,並用不同的顏色表示,使圖象中的認證碼不易被其它程序探測到
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
Color color1 = new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
g.setColor(color1); //隨即畫各種顏色的點
g.drawOval(x, y, 0, 0);
}
//畫干擾線
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
Color color1 = new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
g.setColor(color1); //隨即畫各種顏色的線
g.drawLine(x, y, x1, y1);
}
//圖像生效
g.dispose();
//輸出頁面
ImageIO.write(bi, "jpg", out);
}
public final static String RANDOM_LOGIN_KEY = "RANDOM_LOGIN_KEY";
public void init() throws ServletException {
System.setProperty("java.awt.headless","true");
}
public static String getRandomLoginKey(HttpServletRequest req) {
return (String)req.getSession().getAttribute(RANDOM_LOGIN_KEY);
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
HttpSession ssn = req.getSession();
if(ssn!=null) {
String randomString = random();//生成種子
ssn.setAttribute(RANDOM_LOGIN_KEY,randomString);//將種子放到session裏面
res.setContentType("image/jpeg");//設置圖像生成格式
render(randomString,res.getOutputStream());//輸出到頁面
}
}
}
隨即生成包含驗證碼的字符串
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.