首页>代码>JSP验证码简单例子>/ValidateCodeDemo/src/demo/ValidateCodeHelper.java
package demo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ValidateCodeHelper {
	public static final String V_CODE = "v_code";
	private static int width = 80;

	private static int heigth = 25;

	private static int font_size = 20;

	private static Random random = new Random();

	public String buildCode(HttpServletRequest request, HttpServletResponse response) {
		try {
			response.setContentType("image/jpeg");
			String content = this.getRandomStr();
			request.getSession().setAttribute("correctCode", content);
			ServletOutputStream out = response.getOutputStream();
			// 构建缓冲图像
			BufferedImage bi = new BufferedImage(80, 25,
					BufferedImage.TYPE_INT_BGR);
			// 设置绘图上下文
			Graphics2D g = bi.createGraphics();
			//g.setColor(Color.white);
			g.setColor(getRandColor( 200 , 250 ));
			g.fillRect(0, 0, width, heigth);
			// 设置字体
			g.setFont(new Font("宋体", Font.BOLD, font_size));
			//g.setColor(Color.black);
			g.setColor(getRandColor( 160 , 200 ));
			
			for (int i = 0; i < 30; i++) {
				int x1 = random.nextInt(80);
				int y1 = random.nextInt(25);
				int x2 = random.nextInt(80);
				int y2 = random.nextInt(25);
				//g.setColor(this.getRandomColor());
				g.drawLine(x1, y1, x2, y2);
			}
			
			for (int i = 0; i < 500; i++) {
				g.setColor(this.getRandomColor());
				g.drawOval(random.nextInt(80), random.nextInt(25), 0, 0);
			}
			g.setColor( new  Color( 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 )));
			g.drawString(content, 10, 18);// ----------------------------------------
			JPEGImageEncoder coder = JPEGCodec.createJPEGEncoder(out);
			coder.encode(bi);

			request.getSession().setAttribute(V_CODE, content);
			out.flush();
			out.close();
			return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取四个随机数
	 * 
	 * @return String
	 */
	private String getRandomStr() {
		String str = "";
		for (int i = 0; i < 4; i++) {
			int temp = random.nextInt(10);
			str += temp;
		}
		return str;
	}

	/**
	 * get a random color
	 * 
	 * @return Color
	 */
	private Color getRandomColor() {
		int R = random.nextInt(255);
		int G = random.nextInt(255);
		int B = random.nextInt(255);
		Color color = new Color(R, G, B);
		return color;
	}
	
	private Color getRandColor( int  fc, int  bc){
		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);
	}
}
最近下载更多
lvhongquan  LV11 2023年12月30日
a1677596408  LV23 2022年7月1日
fangen0005  LV25 2022年4月23日
welcome丶  LV8 2021年12月30日
ewan007  LV29 2021年10月24日
nnnnny  LV1 2021年9月23日
wk61339532  LV7 2021年8月20日
1000-7  LV5 2021年7月2日
浅笑心柔  LV1 2021年6月30日
明123456  LV11 2021年5月27日
最近浏览更多
lvhongquan  LV11 2023年12月30日
susitian  LV2 2023年12月28日
DongYingdie  LV2 2023年12月21日
ds9009  LV7 2023年8月23日
qqwerty  LV3 2022年12月12日
陈佳兴  LV4 2022年11月21日
ming_123_9715  LV23 2022年7月19日
a1677596408  LV23 2022年7月1日
2998919365  LV5 2022年5月23日
fangen0005  LV25 2022年4月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友