生成验证码的方法
2016-02-23 12:58
204 查看
调用生成验证码方法时候需要加上时间戳,这样验证码会及时更新,比如$("#xx").attr("src", "xxx.actiont?" + new Date().getTime());
第一种方法:方法写在一个类中,然后调用这个类来获取验证码
/**
* 验证码
* Date 2015-1-7
* Project
* FileName VerifyCode
*/
public class VerifyCode {
// 字符宽度
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);
}
}
/**
* 将数字改为字符
* @author
* @since 2015-1-7
* @return char
*/
private char randomChar () {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
/**
* 生成一张图片
* @author
* @since 2015-1-7
* @return BufferedImage
*/
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;
}
/**
* 将其生成一张验证码图片
* @author
* @since 2015-1-7
* @return BufferedImage
*/
public BufferedImage getImage () {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();
// 向图片中画4个字符
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;
}
/**
* 得到text值
* @author
* @since 2015-1-7
* @return String
*/
public String getText () {
return text;
}
/**
* 输出验证码
* @author
* @param image 图片
* @param out 输出流
* @throws IOException
* @since 2015-1-7
* @return void
*/
public static void output (BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
调用上述方法:
VerifyCode vc = new VerifyCode();
// 获取一次性验证码图片
BufferedImage image = vc.getImage();
// 把图片写到指定流中
VerifyCode.output(image, response.getOutputStream());
// 把文本保存到session中,为LoginServlet验证做准备
request.getSession().setAttribute("vCode", vc.getText());
第二种:直接在jsp中写
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8"%>
<%
//验证码图片中可以出现的字符集,可根据需要修改
char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'};
/**
* 参数width为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
int width=80;
int height=22;
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(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码
String strEnsure = "";
// 4代表4位验证码,如果要生成更多位的认证码,则加大数值
for(int i=0; i<4; ++i) {
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
// 将认证码显示到图像中,如果要生成更多位的认证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 随机产生10个干扰点
Random rand = new Random();
for (int i=0;i<10;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
// 释放图形上下文
g.dispose();
// 输出图像到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
session.setAttribute("htmlCode", strEnsure);
out.clear();
out=pageContext.pushBody();
%>
第一种方法:方法写在一个类中,然后调用这个类来获取验证码
/**
* 验证码
* Date 2015-1-7
* Project
* FileName VerifyCode
*/
public class VerifyCode {
// 字符宽度
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);
}
}
/**
* 将数字改为字符
* @author
* @since 2015-1-7
* @return char
*/
private char randomChar () {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
/**
* 生成一张图片
* @author
* @since 2015-1-7
* @return BufferedImage
*/
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;
}
/**
* 将其生成一张验证码图片
* @author
* @since 2015-1-7
* @return BufferedImage
*/
public BufferedImage getImage () {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();
// 向图片中画4个字符
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;
}
/**
* 得到text值
* @author
* @since 2015-1-7
* @return String
*/
public String getText () {
return text;
}
/**
* 输出验证码
* @author
* @param image 图片
* @param out 输出流
* @throws IOException
* @since 2015-1-7
* @return void
*/
public static void output (BufferedImage image, OutputStream out)
throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
调用上述方法:
VerifyCode vc = new VerifyCode();
// 获取一次性验证码图片
BufferedImage image = vc.getImage();
// 把图片写到指定流中
VerifyCode.output(image, response.getOutputStream());
// 把文本保存到session中,为LoginServlet验证做准备
request.getSession().setAttribute("vCode", vc.getText());
第二种:直接在jsp中写
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8"%>
<%
//验证码图片中可以出现的字符集,可根据需要修改
char mapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'};
/**
* 参数width为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流
*/
int width=80;
int height=22;
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(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码
String strEnsure = "";
// 4代表4位验证码,如果要生成更多位的认证码,则加大数值
for(int i=0; i<4; ++i) {
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
// 将认证码显示到图像中,如果要生成更多位的认证码,增加drawString语句
g.setColor(Color.black);
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String str = strEnsure.substring(0,1);
g.drawString(str,8,17);
str = strEnsure.substring(1,2);
g.drawString(str,20,15);
str = strEnsure.substring(2,3);
g.drawString(str,35,18);
str = strEnsure.substring(3,4);
g.drawString(str,45,15);
// 随机产生10个干扰点
Random rand = new Random();
for (int i=0;i<10;i++) {
int x = rand.nextInt(width);
int y = rand.nextInt(height);
g.drawOval(x,y,1,1);
}
// 释放图形上下文
g.dispose();
// 输出图像到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
session.setAttribute("htmlCode", strEnsure);
out.clear();
out=pageContext.pushBody();
%>
相关文章推荐
- onActivtyForResult这个方法不执行
- CSS高级_笔记
- Windows下ping命令常用参数及实例
- 异常捕获和log4net使用
- 欢迎使用CSDN-markdown编辑器
- java回调函数的初步理解
- Cocos2d-x实例:设置背景音乐与音效-HelloWorld场景实现
- cakephp事务处理
- JavaBean与表单
- chrom 后端调试插件 Postman
- ps制作一种特效人像
- Android自定义ViewGroup打造各种风格的SlidingMenu
- gradle 默认属性
- 数据挖掘之客户流失模型
- Arcgis10.1破解教程
- 结庐在人境
- 在白纸上写几种排序的代码
- Datadog Agent是啥?它消耗什么资源?
- NFC概述
- 从C10K到C10M高性能网络的探索与实践