JAVA 图像验证码,可直接输出到浏览器

时间:2022-10-21 13:08:22
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;
/**
 * 图片验证码
 * @author Administrator
 *
 */
public class ValidCode implements Serializable{
	/**
	 * 序列化ID
	 */
	private static final long serialVersionUID = -8725769201698918197L;
	private static int WIDTH = 80;
	private static int HEIGHT = 28;
	private byte[] imgBytes=null;
	private String validCode;
	public ValidCode() {

	}
	public void create()throws Exception{
		char[] rands =generateCheckCode();
		validCode =new String(rands);
		imgBytes = createValidCode(rands);
	}
	public byte[] getImgBytes() {
		return imgBytes;
	}
	
	public void setImgBytes(byte[] imgBytes) {
		this.imgBytes = imgBytes;
	}
	
	public String getValidCode() {
		return validCode;
	}
	public void setValidCode(String validCode) {
		this.validCode = validCode;
	}

	/***
	 * 创建验证码
	 * @return
	 * @throws IOException
	 */
	private  byte[] createValidCode( char[] rands ) throws IOException{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		// 创建内存图象
		BufferedImage image =new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		// 产生图像
		drawBackground(g);
		
		drawRands(g, rands);
		// 结束图像的绘制过程,完成图像
		g.dispose();
		// 将图像输出到客户端
		ImageIO.write(image, "JPEG", bos);
		byte[] retBytes = bos.toByteArray();
		bos.close();
		return retBytes;
	}
	
	// 产生随机的认证码
	private  char[] generateCheckCode()
	{
		// 定义验证码的字符表
		String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char[] rands = new char[4];
		for (int i = 0; i < 4; i++)
		{
			int rand = (int) (Math.random() * 36);
			rands[i] = chars.charAt(rand);
		}
		return rands;
	}
	
	private  void drawRands(Graphics g, char[] rands) {
		g.setColor(Color.white);
		g.setFont(new Font(null, Font.ITALIC, 22));
		// 在不同的高度上输出验证码的每个字符
		g.drawString("" + rands[0], 6, 17);
		g.drawString("" + rands[1], 22, 22);
		g.drawString("" + rands[2], 43, 18);
		g.drawString("" + rands[3], 61, 24);
	}
	
	private  void drawBackground(Graphics g)
	{
		// 画背景
		g.setColor(new Color(0x68AF02));
		g.fillRect(0, 0, WIDTH, HEIGHT);
		// 随机产生80个干扰点
		for (int i = 0; i < 20; i++)
		{
			int x = (int) (Math.random() * WIDTH);
			int y = (int) (Math.random() * HEIGHT);
			int red = (int) (Math.random() * 255);
			int green = (int) (Math.random() * 255);
			int blue = (int) (Math.random() * 255);
			g.setColor(new Color(red, green, blue));
			g.drawOval(x, y, 1, 0);
		}
	}
	
	/*
	public static void main(String[] args){
		ValidCode validCode =new ValidCode();
		try {
			validCode.create();
			System.out.println(validCode.getValidCode());
			System.out.println(validCode.getImgBytes().length);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	*/
	
}

以下为输出到浏览器方法

ServletOutputStream sos = response.getOutputStream();
ValidCode validCode = new ValidCode();
validCode.create();
byte[] imageBytes = validCode.getImgBytes();
String strValidCode = validCode.getValidCode();
response.setContentLength(imageBytes.length);
sos.write(imageBytes);
sos.close();