社区
Web 开发
帖子详情
请问如何用数字和字母生成验证码
wzx7934003
2006-02-20 04:15:42
请问如何用数字和字母混合生成验证码
...全文
291
3
打赏
收藏
请问如何用数字和字母生成验证码
请问如何用数字和字母混合生成验证码
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
bitou
2006-02-21
打赏
举报
回复
建议引用apache的随机类,很方便
commons-lang.jar
import org.apache.commons.lang.RandomStringUtils;
RandomStringUtils.random(6,true,true);
此方法将产生6位包括字母和数字的随机数
uupo
2006-02-20
打赏
举报
回复
package uuposystem.Web;
/**
*
* <p>Title:石翮JAVA平台</p>
* <p>Description:创建图形验证码</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author 石翮
* @version 1.0
*/
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.LinkedList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
//import java.awt.Graphics;
public class CreateValidImage {
public String sRand="";
public CreateValidImage(){
}
public 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);
}
/**
* 通过文件创建图像
* 格式为jpg类型
* */
public void creatImage(String fileName,String content){
//在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics2D g = image.createGraphics();
//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));
g.setColor(Color.black);//黑色文字
g.drawString(content,10,15);
g.dispose();
try{
File f=new File(fileName);
if(!f.exists()){
f.createNewFile();
}else{
Thread.sleep(200);
f.delete();
f.createNewFile();
}
ImageIO.write(image, "jpg", f);
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 创建图像
* 格式为jpg类型
* @param content - String 图片输出内容
* @return java.awt.image.BufferedImage
* @since 2005-7-19
* */
public BufferedImage getBufferedImage(String content){
int width=60, height=20;
return getBufferedImage(content,width,height);
}
/**
* 创建图像
* 格式为jpg类型
* @param content - String 图片输出内容
* @param width - int 图片宽度
* @param height - int 图片高度
* @return java.awt.image.BufferedImage
* @since 2005-7-19
* */
public BufferedImage getBufferedImage(String content,int width,int height){
//在内存中创建图象
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics2D g = image.createGraphics();
//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));
g.setColor(Color.black);//黑色文字
g.drawString(content,10,15);
g.dispose();
return image;
}
/**
* 将现有BufferedImage融合进Response
* @param response - javax.servlet.http.ServletResponse 将使用的response对象
* @param img - java.awt.image.BufferedImage
* @since 2005-7-19
* */
public void response(HttpServletResponse response,BufferedImage img){
try{
response.setContentType("image/jpg;charset=GB2312");//设定输出的类型
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream());
encoder.encode(img);//对图片进行输出编码
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 返回一个4位的验证码
* */
public String getContent() throws InterruptedException{
String content="";
for(int i=0;i<4;i++){
content+=getChar();
Thread.sleep(new Random().nextInt(10)+10);//休眠以控制字符的重复问题
}
return content;
}
/**
* 获取随机字符
* */
public char getChar(){
Random random=new Random();
char ch='0';
LinkedList ls=new LinkedList();
for(int i=0;i<10;i++){//0-9
ls.add(String.valueOf(48+i));
}
for(int i=0;i<26;i++){//A-Z
ls.add(String.valueOf(65+i));
}
for(int i=0;i<26;i++){//a-z
ls.add(String.valueOf(97+i));
}
int index=random.nextInt(ls.size());
if(index>(ls.size()-1)){
index=ls.size()-1;
}
ch=(char)Integer.parseInt(String.valueOf(ls.get(index)));
return ch;
}
/**
测试
*/
public static void main(String []args) throws Exception{
CreateValidImage c=new CreateValidImage();
c.creatImage("me.jpg",c.getContent());
}
}
interpb
2006-02-20
打赏
举报
回复
用servlet 很容易实现
自己找找代码
!
含
字母
数字
验证码
生成
含
字母
数字
验证码
生成
Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码
Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码Python 如何
生成
包含
字母
和
数字
的图片
验证码
Python源码
【附源码】教你一行代码不写--完成手写
数字
字母
分类训练
掌握了这个,很多分类任务都是换汤不换药了 一行代码不写就可以完成手写
数字
和
字母
共计36类图片的分类,按照此方法,理论上可以一行代码不写完成大多数常见图像的分类工作。 从数据整理->模型设计->GUI界面...
安卓
验证码
相关-Android随机
生成
验证码
View支持
数字
字母
数字
字母
混合汉子
验证码
可以设置
验证码
位数等.rar
Android随机
生成
验证码
View,支持
数字
,
字母
,
数字
字母
混合,汉子
验证码
,可以设置
验证码
位数等.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
用java编写的四位随机的
验证码
,包括
数字
跟
字母
用java编写的四位随机的
验证码
,包括
数字
跟
字母
简单易懂
Web 开发
81,094
社区成员
341,717
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章