java验证码生成代码

        int width = 120;
		int height = 25;
		BufferedImage image = new BufferedImage(120, 25, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		// 边线
		g.setColor(Color.GREEN);
		g.drawRect(0, 0, width, height);
		// 填充颜色
		g.setColor(Color.YELLOW);
		g.fillRect(1, 1, width - 2, height - 2);
		// 干扰线
		g.setColor(Color.GRAY);
		Random r = new Random();
		for (int i = 0; i < 9; i++)
			g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width),      r.nextInt(height));
		g.setColor(Color.RED);
		// Font.BOLD|Font.ITALIC 粗体加斜体
		g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 15));
		int x = 20;
		for (int i = 0; i < 4; i++) {
			String num = r.nextInt(10) + " ";
			g.drawString(num, x, 20);
			x += 20;
		}
		ImageIO.write(image, "jpg", response.getOutputStream());

这种方法是自己创建验证码的样式。

还有一种方法是使用已经建好的包ValidateCode.jar

ValidateCode vc = new ValidateCode(120, 25, 4, 9);	
	       vc.write(response.getOutputStream());  

也可以生成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值