package com.echo.autorent.comm.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.apache.commons.lang.RandomStringUtils;
/**
* <p>Title: Auto project</p>
*
* <p>Description: auto project for Hongma</p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: echo</p>
*
* @author echo
* @version 1.0
*/
public class RandomImageGenerator {
//随即生成包含验证码的字符串
public static String random() {
//20060320 add by wyx
//因为o和0,l和1很难区分,所以,去掉大小写的o和l
String str = "";
str = "2345689ABCDEFGHJKLMNPQRSUVWXYZabcdefhikmnrstuvwxz"; //初始化种子
return RandomStringUtils.random(6, str); //返回6为的字符串
}
//随即生成包含验证码的字符串
public static String shortRandom() {
//20060320 add by wyx
//因为o和0,l和1很难区分,所以,去掉大小写的o和l
String str = "";
str = "2345689ABCDEFGHJKLMNPQRSUVWXYZabcdefhikmnrstuvwxz"; //初始化种子
return RandomStringUtils.random(4, str); //返回6为的字符串
}
/**
* 根据要求的数字生成图片,背景为白色,字体大小16,字体颜色黑色粗体
* @param num 要生成的数字
* @param out 输出流
* @throws IOException
*/
public static void render(String num, OutputStream out) throws IOException {
if (num.getBytes().length > 6) {
throw new IllegalArgumentException(
"The length of param num cannot exceed 6.");
}
//设定宽度和高度
int width = 130;
int height = 18;
// 在内存中创建图象
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics2D g = (Graphics2D) bi.getGraphics();
//画边框
java.util.Random random = new java.util.Random();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
//设置字体
Font mFont = new Font("Arial", Font.PLAIN, 18);
g.setFont(mFont);
g.setColor(Color.BLACK); //设置字体颜色
//画认证码,每个认证码在不同的水平位置
String str1[] = new String[6];
for (int i = 0; i < str1.length; i++) {
str1[i] = num.substring(i, i + 1);
int w = 0;
int x = (i + 1) % 3;
//随即生成验证码字符的水平偏移量
if (x == random.nextInt(3)) {
w = 14 - random.nextInt(2);
} else {
w = 14 + random.nextInt(2);
}
//随即生成颜色
Color color1 = new Color(random.nextInt(60), random.nextInt(60),
random.nextInt(60));
g.setColor(color1);
g.drawString(str1[i], 20 * i + 10,w);
}
随机产生干扰点,并用不同的颜色表示,使图象中的认证码不易被其它程序探测到
for (int i = 0; i < 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
Color color1 = new Color(random.nextInt(55) + 200, random.nextInt(55) + 200,
random.nextInt(55) + 200);
g.setColor(color1); //随即画各种颜色的点
g.drawOval(x, y, 0, 0);
}
//画干扰线
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
Color color1 = new Color(random.nextInt(55) + 200, random.nextInt(55) + 200,
random.nextInt(55) + 200);
g.setColor(color1); //随即画各种颜色的线
g.drawLine(x, y, x1, y1);
}
//图像生效
g.dispose();
//输出页面
ImageIO.write(bi, "jpg", out);
}
/**
* 根据要求的数字生成图片,背景为白色,字体大小16,字体颜色黑色粗体
* @param num 要生成的数字
* @param out 输出流
* @throws IOException
*/
public static void shortRender(String num, OutputStream out) throws IOException {
if (num.getBytes().length > 4) {
throw new IllegalArgumentException(
"The length of param num cannot exceed 4.");
}
//设定宽度和高度
int width = 64;
int height = 18;
// 在内存中创建图象
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics2D g = (Graphics2D) bi.getGraphics();
//画边框
java.util.Random random = new java.util.Random();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
//设置字体
Font mFont = new Font("Arial", Font.PLAIN, 18);
g.setFont(mFont);
g.setColor(Color.BLACK); //设置字体颜色
//画认证码,每个认证码在不同的水平位置
String str1[] = new String[4];
for (int i = 0; i < str1.length; i++) {
str1[i] = num.substring(i, i + 1);
int w = 0;
int x = (i + 1) % 3;
//随即生成验证码字符的水平偏移量
if (x == random.nextInt(3)) {
w = 14 - random.nextInt(2);
} else {
w = 14 + random.nextInt(2);
}
//随即生成颜色
Color color1 = new Color(random.nextInt(60), random.nextInt(60),
random.nextInt(60));
g.setColor(color1);
g.drawString(str1[i], 16 * i + 4,w);
}
// 随机产生干扰点,并用不同的颜色表示,使图象中的认证码不易被其它程序探测到
// for (int i = 0; i < 100; i++) {
// int x = random.nextInt(width);
// int y = random.nextInt(height);
// Color color1 = new Color(random.nextInt(55) + 200, random.nextInt(55) + 200,
// random.nextInt(55) + 200);
// g.setColor(color1); //随即画各种颜色的点
// g.drawOval(x, y, 0, 0);
// }
// //画干扰线
// for (int i = 0; i < 5; i++) {
// int x = random.nextInt(width);
// int y = random.nextInt(height);
// int x1 = random.nextInt(width);
// int y1 = random.nextInt(height);
// Color color1 = new Color(random.nextInt(55) + 200, random.nextInt(55) + 200,
// random.nextInt(55) + 200);
//
// g.setColor(color1); //随即画各种颜色的线
// g.drawLine(x, y, x1, y1);
// }
//图像生效
g.dispose();
//输出页面
ImageIO.write(bi, "jpg", out);
}
public static void main(String[] args) throws IOException {
String num = shortRandom();
System.out.println(num);
shortRender(num, new FileOutputStream("D:\\test.jpg"));
System.out.println("Image generated.");
}
}
jsp 登陆页面验证码的实现
5星 · 超过95%的资源 需积分: 25 83 浏览量
2010-06-08
11:47:47
上传
评论
收藏 60KB RAR 举报
liuyangzheyufeixiang
- 粉丝: 0
- 资源: 3
最新资源
- IMG_20240424_183805.jpg
- DatabaseWatermarkingBasedonTextFormat
- -移动通信-网络课程设计与研究
- 基于Python的PCA人脸识别算法的原理及实现代码+文档详解.zip
- 甘肃移动全业务工程建设项目管理流程优化研究
- 549springboot + vue 民宿管理平台.zip (可运行源码+数据库文件+文档)
- ZArchiver.Pro_0.9.5.apk
- vmware环境配置.mp4
- 548springboot + vue 大学生社团活动平台.zip(可运行源码+数据库文件+文档)
- 微信小程序 辩论倒计时小程序源码 作业设计demo 计算机专业参考
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
前往页