2.生成检验码带彩条和彩点

本文详细介绍了Java中验证码的生成原理及其实现方法,并通过实例展示了如何在Servlet中绘制图形并将其转换为JPEG格式,同时解释了如何在JSP页面中使用验证码。此外,文章还演示了如何将生成的验证码应用于实际场景,例如图像加载和点击事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虽然在实际开发中很少去写一个检验码的功能毕竟网上有现有的例子,但有时在有空时开发这些控件也是一件提高编程能力的很好方法,

原理:在servlet中“画出”图形然后在写入response的outputStream,然后将jsp属性设为jpeg即可

Bean中画出图形

package com;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageTest {

	public ImageTest() {
	}

	public char mapTable[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
			'j', 'k', 'l', 'm', 'n', '1', '0', '2', '3', '4', '5', '6', '7',
			'8', '9' };

	public String getEnsure(int width, int height, OutputStream out) {
		Random random = new Random();
		if (width <= 0 || height <= 0) {
			width = 85;
			height = 40;
		}
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		// 设置各种参数
		g.setColor(new Color(0xDCDCDC));
		g.fillRect(0, 0, width, height);
		g.setColor(new Color(0xff0000));
		g.drawRect(0, 0, width - 1, height - 1);
		String strNum = "";
		for (int i = 0; i < 4; i++) {
			strNum += mapTable[(int) (mapTable.length * Math.random())];
		}
		g.setColor(Color.black);
		g.setFont(new Font("Atlantic Inline", Font.PLAIN, 23));
		String str = strNum.substring(0, 1);
		g.drawString(str, 6, 27);
		str = strNum.substring(1, 2);
		g.drawString(str, 24, 25);
		str = strNum.substring(2, 3);
		g.drawString(str, 44, 28);
		str = strNum.substring(3, 4);
		g.drawString(str, 64, 25);
		
		// 生成干扰点
		for (int i = 0; i < 100; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			g.drawOval(x, y, 1, 1);
		}
		// 生成干扰线
		for (int i = 0; i < 5; i++) {
			int x1 = random.nextInt(width);
			int x2 = random.nextInt(width);

			int y1 = random.nextInt(height);
			int y2 = random.nextInt(height);
			g.setColor(new Color(random.nextInt(0xffffff)));//彩色的
			g.drawLine(x1, y1, x2, y2);
			
		}
		g.dispose();
		try {
			ImageIO.write(image, "JPEG", out);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strNum;
	}
}
image.jsp关键是contenttype的属性

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="image/jpeg"%>
<jsp:useBean id="image" class="com.ImageTest" scope="page" />
<%
	String str = image.getEnsure(0, 0, response.getOutputStream());
	session.setAttribute("strNum", str);
%>
测试

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript">
	function getImage(object) {
		object.src="image.jsp?"+Math.random();
	}
</script>

</head>

<body>
	<img alt="加载中..." src="image.jsp" onclick="getImage(this)">
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值