如何生成验证码

我们先来看一个基本的应用:

//Image ImageIO BufferedImage Icon ImageIcon类
		BufferedImage bufferedImage = new BufferedImage(70, 30, BufferedImage.TYPE_INT_BGR);  //构建一个图片缓冲区
		Graphics2D g2 = (Graphics2D) bufferedImage.getGraphics(); //通过图片缓冲区获得一个图像上下文对象
		g2.setColor(Color.WHITE); //将当前上下文的颜色修改为指定颜色
		g2.fillRect(0, 0, 70, 30);  //填充指定矩形
		g2.setColor(Color.BLACK);//将当前上下文的颜色修改为指定颜色
		Font font = new Font("宋体", Font.BOLD + Font.ITALIC, 10);//创建字体对象
		g2.setFont(font);//设置上下文字体对象
		g2.drawString("奔跑的面包", 5, 15); //使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本
		ImageIO.write(bufferedImage, "JPG", new FileOutputStream("E:/黑马/javaweb/传智播客_超全面的JavaWeb视频教程vedio/JavaWeb视频教程_Day08_tomcat与web程序结构与Http协议/day08_avi/a.jpg"));// ImageWriter 将一个图像写入 OutputStream

好了,我们来看一个工具类吧,很实用,你可以收藏这段代码:

public class YanZheng {
	private int w = 70;
	private int h = 35;
 	private Random r = new Random();
 	
	private String[] fontNames  = {"宋体", "华为楷体", "黑体", "华文新魏", "楷体_GB2312"};
	private String codes  = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
	private Color bgColor  = new Color(255, 255, 255);
	private String text ;
	//随机生成颜色
	private Color randomColor () {
		int red = r.nextInt(150);
		int green = r.nextInt(150);
		int blue = r.nextInt(150);
		return new Color(red, green, blue);
	}
	//随机生成字体,加粗,斜线,及大小
	private Font randomFont () {
		int index = r.nextInt(fontNames.length);
		String fontName = fontNames[index];
		int style = r.nextInt(4);
		int size = r.nextInt(5) + 24; 
		return new Font(fontName, style, size);
	}
	//在图片上加横线
	private void drawLine (BufferedImage image) {
		int num  = 3;
		Graphics2D g2 = (Graphics2D)image.getGraphics();
		for(int i = 0; i < num; i++) {
			int x1 = r.nextInt(w);
			int y1 = r.nextInt(h);
			int x2 = r.nextInt(w);
			int y2 = r.nextInt(h); 
			g2.setStroke(new BasicStroke(1.5F)); 
			g2.setColor(Color.BLUE); 
			g2.drawLine(x1, y1, x2, y2);
		}
	}
	
	private char randomChar () {
		int index = r.nextInt(codes.length());
		return codes.charAt(index);
	}
	
	private BufferedImage createImage () {
		BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
		Graphics2D g2 = (Graphics2D)image.getGraphics(); 
		g2.setColor(this.bgColor);
		g2.fillRect(0, 0, w, h);
 		return image;
	}
	
	public BufferedImage getImage () {
		BufferedImage image = createImage();   //获得图片缓冲区
		Graphics2D g2 = (Graphics2D)image.getGraphics(); //获得上下文
		StringBuilder sb = new StringBuilder(); 
		for(int i = 0; i < 4; i++)  {   
			String s = randomChar() + ""; //随机获取字符创
			sb.append(s); 
			float x = i * 1.0F * w / 4; 
			g2.setFont(randomFont()); 
			g2.setColor(randomColor()); 
			g2.drawString(s, x, h-5); 
		}
		this.text = sb.toString(); 
		drawLine(image); 
		return image;		
	}
	
	public String getText () {
		return text;
	}
	
	public static void output (BufferedImage image, OutputStream out) 
				throws IOException {
		ImageIO.write(image, "JPEG", out);
	}
}
运行程序得到如图结果:

还不错吧!!嘻嘻

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值