private int width =80;
private int height=30;
private Random r=new Random();
private String fontnames[]= {"宋体","华文楷体","黑体","微软雅黑","楷体_GBK2312"};
private String Codes="23456789QWERTYUOPASDFGHJKLZXCVBNMqwertyuopasdfghjklzxcvbnm";
private Color color=new Color(255,255,255);
private String text;
//随机颜色
public Color setColor(){
int red = r.nextInt(150);
int grreen = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red,grreen,blue);

}

//随机字体
private Font randomFont() {
int nextInt = r.nextInt(fontnames.length);
String fontnsmr= fontnames[nextInt];
int style=r.nextInt(4);
int size=r.nextInt(5)+24;
return new Font(fontnsmr, style, size);
}
//创建图片
private BufferedImage creatrimg() {
BufferedImage img=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D)img.getGraphics();//得到画笔
graphics.setColor(this.color);
graphics.fillRect(0, 0, width, height);//fillRect(坐标,坐标,width,height)
return img;
}
//生成随机字符
private char randomChar() {
int r=this.Codes.length();
int index=this.r.nextInt(r);
char c=this.Codes.charAt(index);
return c;
}
//添加干扰线
private void drawLine(BufferedImage bfimg) {
Graphics2D graphics = (Graphics2D)bfimg.getGraphics();
for (int i = 0; i <3; i++) {
graphics.setStroke(new BasicStroke(1f));
graphics.setColor(Color.BLUE);
graphics.drawLine(r.nextInt(width), r.nextInt(height),r.nextInt(width),r.nextInt(height));
}
}

//得到验证码
public BufferedImage getimg() {
//创建图片缓冲区
BufferedImage bfimg=creatrimg();
//得到画笔
Graphics2D graphics = (Graphics2D)bfimg.getGraphics();
//装载生成的验证码
StringBuffer sb=new StringBuffer();
//向图片绘制四个字符
for (int i = 1; i < 5; i++) {
String randomChar = randomChar()+"";
sb.append(randomChar);
//设置当前字符的x 轴
float x=i*1.0f*width/5;
//设置随机字体
graphics.setFont(randomFont());
graphics.setColor(randomColor());
graphics.drawString(randomChar, x, height-=2);
}
this.text=sb.toString(http://www.my516.com);
//添加干扰线
drawLine(bfimg);
return bfimg;
}
---------------------

最新文章

  1. .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)
  2. iOS ARC 下的单例模式
  3. 为川师大女生支招 15年如何还200W
  4. geoserver使用curl发布 imagemosaic
  5. SED入门
  6. matlab 在代码中,显示错误,退出程序
  7. case,cast
  8. Android Camera 使用一例,视频聊天app
  9. (转)requirejs:杏仁的优化(almond)
  10. python正则表达式re模块详细介绍
  11. win10系统开机输入密码黑屏解决方法
  12. Redis深入学习笔记(四)主从数据复制流程
  13. 分布式配置 SSH 免密登陆
  14. 同步调用异步方法how-would-i-run-an-async-taskt-method-synchronously
  15. 【杂谈】对IO与NIO的认识
  16. 函数和常用模块【day04】:高阶函数(七)
  17. jenkins或ansible启动应用不成功日志又不报错
  18. Ubuntu 添加,删除ppa
  19. 【uoj#315/bzoj4943】[NOI2017]蚯蚓排队 Hash
  20. JavaScript 简单吗

热门文章

  1. jdbc 数据库批处理insert操作
  2. jdbc blob插入及查询操作
  3. 初学Linux基本的命令操作应当记牢
  4. paper 135:关于C#泛型的一些讲解
  5. &lt;Jmeter入门不放弃&gt;之&lt;2.常用功能&gt;
  6. mysql启动脚本-my
  7. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】03、创建RESTful API,并统一处理返回值
  8. java并发编程笔记(三)——线程安全性
  9. 数据批量导入HBase
  10. linux下vnstat查看服务器带宽流量统计