java 随机生成图片与html表单中的验证码实现

原文:http://codingxiaxw.cn

java随机生成图片

用java语言生成一个带有字符串文本的图片需要以下四步

  • 1.创建图片缓冲区
  • 2.设置图片缓冲区的参数和保存的类型
  • 3.得到这个图片的绘制环境(拿到画笔)
  • 4.将图片保存起来
//第一、二步,传入参数为缓冲区宽、高、保存图片类型
BufferedImage image=new BufferedImage(width1,height1,BufferedImage.TYPE_INT_RGB);

//第三步,得到绘制环境(需要用到Graphics类)
Graphics paint=image.getGraphics();
paint.setColor(Color.WHITE);//设置画笔颜色为白色
paint.fillRect(start,end,width2,height2);//绘制矩形并填充,将图片缓冲区的(start,end)坐标作为绘制图片的左上角坐标,绘制图片宽为width2、高为height2
paint.setColor(Color.RED);//设置画笔颜色为红色
paint.drawString(str,x,y);//设置需要绘制在图片上的文本,参数为字符串、字符串所在的x坐标和y坐标

//第四步,将图片保存起来(需要用到ImageIO类)
ImageIO.write(image,"JPEG",out);//参数为图片缓冲区、图片类型、输出流 

以下为一个具体实例:

public class VerfifyCode {

	// 设置图片缓冲区的宽高
	private int width = 70;
	private int height = 35; 
	private Random r = new Random(); // 生成随机数字
	private String[] fontNames = {"宋体","华文楷体","黑体","微软雅黑"}; // 创建一个字体数组
	private Color bgColor = new Color(255, 255, 255); // 白色的背景色
	private String codes = "1234567890qwertyuiopasdfghjklzxcvbnm"; // 可提供选择的随机文字
	private String text; // 要在图片上生成的文本
	
	/**
	 * 创建图片缓冲区
	 */
	public BufferedImage getImage() {
		BufferedImage image = createImage();
		Graphics g = image.getGraphics();
		StringBuilder sb = new StringBuilder();
		// 循环四次 每次添加一个字符
		for (int i = 0; i < 4; i++) {
			sb.append(randomChar()); // 调用产生随机字符的方法, 随机生成一个字符  并添加到sb后面
			g.setFont(randomFont()); // 调用产生随机字体
			g.setColor(randomColor()); // 调用产生随机颜色方法 生成随机颜色
			g.drawString(sb.toString(), i*width/4, height - 5); // 在图片中绘制文本
		}
		this.text=sb.toString();// 把生成的字符串赋值给文本
		drawLine(image); // 调用添加干扰线的方法对图片中的文本进行干扰
		return image;
	}
	
	/**
	 * 创建图片缓冲区(背景图)
	 */
	public BufferedImage createImage() {
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setColor(bgColor);
		g.fillRect(0, 0, width, height); // 设置整个图片背景为白色
		return image;
	}
	
	/**
	 * 生成随机字符的方法
	 */
	public char randomChar() {
		int index = r.nextInt(codes.length());
		return codes.charAt(index);
	}
	/**
	 * 生成随机字体
	 */
	public Font randomFont() {
		int index = r.nextInt(fontNames.length);
		int style = r.nextInt(4); // 设置字体样式,0表示无样式,1表示粗体,2表示斜体 3表示粗体加斜体 
		int size = r.nextInt(4) + 24;
		return new Font(fontNames[index], style, size);
	}
	/**
	 * 生成随机颜色
	 */
	public Color randomColor() {
		int red = r.nextInt(255);
		int green = r.nextInt(255);
		int blue = r.nextInt(255);
		return new Color(red, green, blue);
	}
	/**
	 * 生成干扰线
	 */
	public void drawLine(BufferedImage image) {
		// 取画笔 画线  画三条
		Graphics g = image.getGraphics();
		for(int i = 0; i < 3; i++) {
			int x1 = r.nextInt(width);
			int y1 = r.nextInt(height);
			int x2 = r.nextInt(width);
			int y2 = r.nextInt(height); // 起点和终点坐标
			g.setColor(Color.yellow); // 
			g.drawLine(x1, y1, x2, y2);
		}
	}
	
	/**
	 * 返回验证码内容 用于验证
	 */
	public String getText() {
		return this.text;
	}
	
	/**
	 * 保存图片到指定的输出流
	 */
	public static void output(BufferedImage image, OutputStream out) throws IOException {
		ImageIO.write(image, "JPEG", out);
	}
}

通过VerifyCode vc = new VerifyCode(); VerifyCode.output(vc.createImage, out)方法进行输出

猜你喜欢

转载自blog.csdn.net/dirtyman_/article/details/80932157