复杂验证码生成java代码_用java代码 生成验证码的一个示例类

在jsp里这样调用

 

<

/td>

js里脚本

function updateValidateCode(width, height) {

  document.getElementById('validateCodeImage').innerHTML = '如果看不清验证码,请点图片刷新';

}

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

/**

* MyEclipse Struts Creation date: 05-31-2007

*

* XDoclet definition:

*

* @struts.action validate="true"

*/

public class ValidateCodeAction extends Action {

/*

* Generated Methods

*/

/**

* Method execute

*

* @param mapping

* @param form

* @param request

* @param response

* @return ActionForward

*/

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response) {

// 设置图片的长宽

int width = StringUtils.isNotBlank(request.getParameter("width")) ? Integer

.parseInt(request.getParameter("width"))

: 0;

int height = StringUtils.isNotBlank(request.getParameter("height")) ? Integer

.parseInt(request.getParameter("height"))

: 0;

if (width < 70 || width > 140) {

width = 70;

}

if (height < 25 || height > 50) {

height = 25;

}

// 创建内存图像

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

// 获取图形上下文

Graphics g = image.createGraphics();

// 设定图像背景色(因为是做背景,所以偏淡)

g.setColor(getRandColor(180, 250));

g.fillRect(0, 0, width, height);

// 设置字体

g.setFont(new Font("Arial", Font.BOLD, 19));

// 设置默认生成4个验证码

int length = 4;

// 设置随机种子

java.util.Random rand = new Random();

// 设置备选验证码:包括"a-z"和数字"0-9",其中去掉了一些容易混淆的字符

String base = "abcdefghijkmnqrstuvwxyz23456789";

int size = base.length();

StringBuffer str = new StringBuffer();

for (int i = 0; i < length; i++) {

int start = rand.nextInt(size);

String tmpStr = base.substring(start, start + 1);

str.append(tmpStr);

// 生成随机颜色(因为是做前景,所以偏深)

g.setColor(getRandColor(10, 150));

// 将此字画到图片上

// g.drawString(str.toString(), 4, 17);

g.drawString(tmpStr, 13 * i + 6 + rand.nextInt(5), 14 + rand

.nextInt(6));

// 随机画点

for (int j = 0; j < 20; j++) {

int x = rand.nextInt(width);

int y = rand.nextInt(height);

g.drawOval(x, y, 0, 0);

}

/*

// 随机画线

for (int j = 0; j < 2; j++) {

int x = rand.nextInt(width);

int y = rand.nextInt(height);

int x1 = rand.nextInt(12);

int y1 = rand.nextInt(12);

g.drawLine(x, y, x + x1, y + y1);

}

*/

}

// 将认证码存入session

request.getSession().setAttribute("validateCode", str.toString());

// 图象生效

g.dispose();

// 设置页面不缓存

response.setDateHeader("Expires", -1);

response.setHeader("Cache-Control",

"no-store, private, post-check=0, pre-check=0, max-age=0");

response.setHeader("Pragma", "no-cache");

// 输出图象到页面

try {

ImageIO.write(image, "JPEG", response.getOutputStream());

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

// 给定范围获得一个随机颜色

Color getRandColor(int fc, int bc) {

Random random = new Random();

if (fc > 255) {

fc = 255;

}

if (bc > 255) {

bc = 255;

}

int r = fc + random.nextInt(bc - fc);

int g = fc + random.nextInt(bc - fc);

int b = fc + random.nextInt(bc - fc);

return new Color(r, g, b);

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值