package org.verify.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
public class RandomNumUtil {
public static final char[] CHARS = {'0','1','2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private ByteArrayInputStream image;//图像
private String str;//验证码
//构造函数
private RandomNumUtil(){
init(); //初始化属性
}
/**
* 获得RandomNumUtil实例
*/
public static RandomNumUtil Instance() {
return new RandomNumUtil();
}
/**
* 获得图片验证图片
*/
public ByteArrayInputStream getImage() {
return image;
}
public void setImage(ByteArrayInputStream image) {
this.image = image;
}
/**
* 获得图片验证码
* @return
*/
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
/**
* 初始化
*/
private void init() {
//在内存中创建图像
int width=65,height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//生产随机类
Random random = new Random();
//设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(6位数字)
StringBuffer sRand = new StringBuffer();
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(CHARS[random.nextInt(CHARS.length)]);
sRand.append(rand);
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 赋值验证码
this.str = sRand.toString();
// 图象生效
g.dispose();
ByteArrayInputStream input = null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
ImageOutputStream imageOut = ImageIO
.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
input = new ByteArrayInputStream(output.toByteArray());
} catch (Exception e) {
System.out.println("验证码图片产生出现错误:" + e.toString());
}
this.image = input;/* 赋值图像 */
}
/*
* 给定范围获得随机颜色
*/
private 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);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
loginVerify.rar (75个子文件)
loginVerify
.project 1KB
db
eternal.sql 2KB
.mymetadata 301B
src
struts.xml 941B
applicationContext.xml 3KB
org
verify
service
Impl
UserServiceImpl.java 791B
action
UserAction.java 2KB
RandomAction.java 942B
Dao
Impl
UserDaoImpl.java 2KB
util
RandomNumUtil.java 4KB
entity
User.java 1KB
WebRoot
WEB-INF
classes
struts.xml 941B
applicationContext.xml 3KB
org
verify
service
Impl
UserServiceImpl.class 1KB
action
RandomAction.class 1KB
UserAction.class 2KB
Dao
Impl
UserDaoImpl.class 2KB
util
RandomNumUtil.class 4KB
entity
User.class 1KB
lib
commons-lang3-3.1.jar 308KB
asm-commons-3.3.jar 37KB
org.springframework.aspects-3.1.2.RELEASE.jar 49KB
org.springframework.jdbc-3.1.2.RELEASE.jar 395KB
org.springframework.core-3.1.2.RELEASE.jar 439KB
jboss-transaction-api_1.1_spec-1.0.0.Final.jar 11KB
org.springframework.jms-3.1.2.RELEASE.jar 194KB
dom4j-1.6.1.jar 307KB
commons-lang-2.4.jar 256KB
org.springframework.web-3.1.2.RELEASE.jar 536KB
org.springframework.test-3.1.2.RELEASE.jar 224KB
hibernate-core-4.1.7.Final.jar 4.26MB
ezmorph-1.0.6.jar 84KB
c3p0-0.9.1.jar 594KB
hibernate-c3p0-4.1.7.Final.jar 41KB
org.springframework.web.struts-3.1.2.RELEASE.jar 30KB
struts2-core-2.3.4.1.jar 765KB
javassist-3.15.0-GA.jar 633KB
commons-logging-1.1.1.jar 59KB
org.springframework.expression-3.1.2.RELEASE.jar 172KB
commons-beanutils-1.7.0.jar 184KB
freemarker-2.3.19.jar 909KB
hibernate-commons-annotations-4.0.1.Final.jar 79KB
commons-io-2.0.1.jar 156KB
struts2-spring-plugin-2.3.4.1.jar 21KB
asm-tree-3.3.jar 21KB
org.springframework.aop-3.1.2.RELEASE.jar 325KB
hibernate-jpa-2.0-api-1.0.1.Final.jar 100KB
commons-fileupload-1.2.2.jar 58KB
antlr-2.7.7.jar 435KB
aspectjweaver-1.6.9.jar 1.59MB
commons-logging.jar 37KB
json-lib-2.4-jdk15.jar 155KB
mysql-connector-java-5.1.22-bin.jar 813KB
aopalliance-1.0.jar 4KB
org.springframework.transaction-3.1.2.RELEASE.jar 239KB
asm-3.3.jar 43KB
jboss-logging-3.1.0.GA.jar 59KB
ognl-3.0.5.jar 222KB
struts2-convention-plugin-2.3.4.1.jar 65KB
org.springframework.context-3.1.2.RELEASE.jar 813KB
org.springframework.beans-3.1.2.RELEASE.jar 578KB
commons-collections-3.2.1.jar 562KB
org.springframework.asm-3.1.2.RELEASE.jar 52KB
jta.jar 9KB
xwork-core-2.3.4.1.jar 614KB
org.springframework.orm-3.1.2.RELEASE.jar 371KB
web.xml 926B
index.jsp 2KB
success.jsp 829B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.jdt.core.prefs 330B
org.eclipse.wst.jsdt.ui.superType.name 6B
.jsdtscope 500B
.classpath 4KB
共 75 条
- 1
资源评论
宇宙最强皮皮虾
- 粉丝: 50
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功