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

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();


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值