java生成动态gif格式与png格式的验证码(代码4)

import java.util.Random;

/**
 * 随机工具类
 * @author wzztestin
 *
 */
public class Randoms {
	private static final Random RANDOM = new Random();
	// 定义验证码字符.去除了O和I等容易混淆的字母
	public static final char shuzi[] = { '1', '2', '3', '4', '5', '6', '7','8', '9'};
	public static final char chaozuofu[] = { '+', '-', '×', '÷'};

	/**
	 * 产生两个数之间的随机数
	 * 
	 * @param min
	 *            小数
	 * @param max
	 *            比min大的数
	 * @return int 随机数字
	 */
	public static int num(int min, int max) {
		return min + RANDOM.nextInt(max - min);
	}

	/**
	 * 产生0--num的随机数,不包括num
	 * 
	 * @param num
	 *            数字
	 * @return int 随机数字
	 */
	public static int num(int num) {
		return RANDOM.nextInt(num);
	}

	public static char getNum() {
		return shuzi[num(0, shuzi.length)];
	}
	
	public static char getChaoZuoFu() {
		return chaozuofu[num(0, 2)];
	}
}

 

import java.awt.AlphaComposite;
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;

/**
 * png格式验证码
 * @author wzztestin
 *
 */
public class SpecCaptcha extends Captcha {
	public SpecCaptcha() {
	}
	
	/**
	 * 初始化宽高
	 * @param width
	 * @param height
	 */
	public SpecCaptcha(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	public SpecCaptcha(int width, int height, int alpha) {
		this(width, height);
	}

	public SpecCaptcha(int width, int height, int len, Font font) {
		this(width, height, len);
		this.font = font;
	}

	/**
	 * 生成验证码
	 * 
	 * @throws java.io.IOException
	 *             IO异常
	 */
	@Override
	public void out(OutputStream out) {
		graphicsImage(getVilidCode().toCharArray(), out);
	}

	/**
	 * 画随机码图
	 * 
	 * @param strs
	 *            文本
	 * @param out
	 *            输出流
	 */
	private boolean graphicsImage(char[] strs, OutputStream out) {
		boolean ok = false;
		try {
			BufferedImage bi = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = (Graphics2D) bi.getGraphics();
			AlphaComposite ac3;
			Color color;
			int len = strs.length;
			g.setColor(Color.WHITE);
			g.fillRect(0, 0, width, height);
			// 随机画干扰的蛋蛋
			for (int i = 0; i < 15; i++) {
				color = color(150, 250);
				g.setColor(color);
				g.drawOval(Randoms.num(width), Randoms.num(height),
						5 + Randoms.num(10), 5 + Randoms.num(10));// 画蛋蛋,有蛋的生活才精彩
				color = null;
			}
			g.setFont(font);
			int h = height - ((height - font.getSize()) >> 1), w = width / len, size = w
					- font.getSize() + 1;
			/* 画字符串 */
			for (int i = 0; i < len; i++) {
				ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);// 指定透明度
				g.setComposite(ac3);
				color = new Color(20 + Randoms.num(110), 20 + Randoms.num(110),
						20 + Randoms.num(110));// 对每个字符都用随机颜色
				g.setColor(color);
				g.drawString(strs[i] + "", (width - (len - i) * w) + size,
						h - 4);
				color = null;
				ac3 = null;
			}
			ImageIO.write(bi, "png", out);
			out.flush();
			ok = true;
		} catch (IOException e) {
			ok = false;
		} finally {
			Streams.close(out);
		}
		return ok;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值