java开发_使用BufferedImage生成验证码
2013-07-24 09:27
477 查看
前台处理返回的ByteArrayOutputStream
//获取验证码写入ByteArrayOutputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
String rand = this.getValidateImage(RANGE, 70, 21, 4, 1, os);
req.getSession().setAttribute(KEY, rand);
return new ByteArrayInputStream(os.toByteArray());
} finally {
if (os != null) os.close();
}
//生成验证码方法
public static String getValidateImage(String str, int width, int height, int show, int lineNum, OutputStream output)
{
Random random = new Random();
BufferedImage image = new BufferedImage(width, height, 5);
Font font = new Font("Arial", 0, height - 1);
int distance = 18;
Graphics2D d = (Graphics2D)image.getGraphics();
d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
d.setColor(new Color(0X8FB9EB));
d.fillRect(0, 0, image.getWidth(), image.getHeight());
d.setColor(new Color(random.nextInt(100) + 100, random.nextInt(100) + 100, random.nextInt(100) + 100));
for (int i = 0; i < lineNum; i++) {
d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()),
random.nextInt(image.getHeight()));
}
d.setColor(new Color(0X8FB9EB));
d.setFont(font);
String checkCode = "";
int x = -distance;
for (int i = 0; i < show; i++) {
char tmp = str.charAt(random.nextInt(str.length()));
checkCode = checkCode + tmp;
x += distance;
d.setColor(new Color(random.nextInt(100) + 50, random.nextInt(100) + 50, random.nextInt(100) + 50));
d.drawString(String.valueOf(tmp), x, random.nextInt(image.getHeight() - font.getSize()) + font.getSize());
}
d.dispose();
try {
ImageIO.write(image, "jpg", output);
} catch (IOException e) {
//logger.warn("生成验证码错误.", e);
}
return checkCode;
}
//获取验证码写入ByteArrayOutputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
String rand = this.getValidateImage(RANGE, 70, 21, 4, 1, os);
req.getSession().setAttribute(KEY, rand);
return new ByteArrayInputStream(os.toByteArray());
} finally {
if (os != null) os.close();
}
//生成验证码方法
public static String getValidateImage(String str, int width, int height, int show, int lineNum, OutputStream output)
{
Random random = new Random();
BufferedImage image = new BufferedImage(width, height, 5);
Font font = new Font("Arial", 0, height - 1);
int distance = 18;
Graphics2D d = (Graphics2D)image.getGraphics();
d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
d.setColor(new Color(0X8FB9EB));
d.fillRect(0, 0, image.getWidth(), image.getHeight());
d.setColor(new Color(random.nextInt(100) + 100, random.nextInt(100) + 100, random.nextInt(100) + 100));
for (int i = 0; i < lineNum; i++) {
d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()),
random.nextInt(image.getHeight()));
}
d.setColor(new Color(0X8FB9EB));
d.setFont(font);
String checkCode = "";
int x = -distance;
for (int i = 0; i < show; i++) {
char tmp = str.charAt(random.nextInt(str.length()));
checkCode = checkCode + tmp;
x += distance;
d.setColor(new Color(random.nextInt(100) + 50, random.nextInt(100) + 50, random.nextInt(100) + 50));
d.drawString(String.valueOf(tmp), x, random.nextInt(image.getHeight() - font.getSize()) + font.getSize());
}
d.dispose();
try {
ImageIO.write(image, "jpg", output);
} catch (IOException e) {
//logger.warn("生成验证码错误.", e);
}
return checkCode;
}
相关文章推荐
- Java Web开发之图形验证码的生成与使用方法
- Java Web开发之图形验证码的生成与使用方法
- 使用JAVA代码生成验证码
- 【Java】使用Servlet生成随机验证码图片
- Java使用patchca生成验证码
- Java使用patchca生成验证码
- 使用Java注解开发自动生成SQL
- java web 生成及使用验证码验证 简单例子
- JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术
- 【超级详细】使用java生成带干扰线颜色不一的验证码
- [置顶] 【Android】java生成炫酷验证码,不区分大小写。登陆,发送手机验证码,防止注册机,android开发
- 毛超帅 使用 jquery.qrcode.js 生成网站地址url二维码图片______软件开发-JAVA
- Web开发中的验证码图片的生成-基于Java的实现
- MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping 分类: MyBatis JAVA开发杂谈 2013-06-04 16:30 17467人阅读 评论(
- 使用Java生成四位不重复的验证码
- 使用java代码生成随机验证码
- Javaweb开发中通过Servlet生成验证码图片
- 【Android】java生成炫酷验证码,不区分大小写。登陆,发送手机验证码,防止注册机,android开发
- [置顶] java生成四位随机数,包含数字和字母 区分大小写,特别适合做验证码,android开发
- Java中使用patchca生成超炫的验证码