//设置响应头
response.setCharacterEncoding("image/jpeg");
int width=160;
int height=40;
BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setFont(new Font("宋体", Font.BOLD, 18));
Random random=new Random();
for(int i=0;i<4;i++)//获取随机四个汉字
{
Color c=new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
String s=GetStr(); AffineTransform aff = new AffineTransform();
aff.rotate(Math.random(),i*10,height-20);//旋转
aff.scale(0.2+Math.random(), 0.2+Math.random());//缩放
g2d.setTransform(aff);
System.err.println(">:"+s); g2d.setColor(c);
g2d.drawString(s, i*10, height- 5);
} for(int i=0;i<3;i++) //干扰线
{
Color c=new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
g2d.setColor(c);
g2d.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height)); }
g2d.dispose();//图片生效
ImageIO.write(image, "jpeg", response.getOutputStream());//输出
System.out.println("输出");
} //获取汉字
private static String GetStr()
{
String str = null;
try { int hightPos, lowPos; // 定义高低位
Random random = new Random();
hightPos = (176 + Math.abs(random.nextInt(39)));//获取高位值
lowPos = (161 + Math.abs(random.nextInt(93)));//获取低位值
byte[] b = new byte[2];
b[0] = (new Integer(hightPos).byteValue());
b[1] = (new Integer(lowPos).byteValue());
str = new String(b, "GBK");//转成中文 } catch (Exception e) {
e.printStackTrace();
}
return str; }

PS:写验证码不能把所有的都用上,这样就无法识别了,可以选择其中的几种进行,如写汉字只加干扰线,写字母加旋转等。

验证码 Demo的更多相关文章

  1. PHP生成图片验证码demo【OOP面向对象版本】

    下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考. 这个demo总共分为4个文件,具体代码如下: 1.code.html中的代码: <!doctype html> < ...

  2. 图片验证码demo示例

    1.首先我们需要一个生成图片验证码图片的一个工具类(下方会有代码示例) 代码如下: package com.util; import java.awt.BasicStroke; import java ...

  3. springboot 集成kaptcha验证码Demo

    验证码(CAPTCHA)是“Completely Automated Public Turing test to tell Computers and Humans Apart”(全自动区分计算机和人 ...

  4. python 之验证码

    验证码原理在于后台自动创建一张带有随机内容的图片,然后将内容通过img标签输出到页面. 安装图像处理模块: pip3 install pillow

  5. 前后端分离djangorestframework—— 接入第三方的验证码平台

    关于验证码部分,在我这篇文章里说的挺详细的了:Python高级应用(3)—— 为你的项目添加验证码 这里还是再给一个前后端分离的实例,因为极验官网给的是用session作为验证的,而我们做前后端分离的 ...

  6. web框架详解之 tornado 四 模板引擎、session、验证码、xss

    一.模板引擎 基本使用 继承,extends 页面整体布局用继承 导入,include 如果是小组件等重复的那么就用导入 下面是目录 首先在controllers里面创建一个文件,文件里面是页面类 # ...

  7. Python(九)Tornado web 框架

    一.简介 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...

  8. 第一个web框架tornado

    简介 tornado,是我学到的第一个web框架是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google ...

  9. Python开发【第十五篇】:Web框架之Tornado

    概述 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了 ...

随机推荐

  1. wdcp/wdlinux 常用工具及命令集

    强制修改Mysql的root密码,在忘记mysql密码时有用 sh /www/Wdlinux/tools/mysql_root_chg.sh ftp配置文件检查,在FTP不能连接或登录时有用sh /w ...

  2. PHP iconv()编码转换函数用法示例

    PHP iconv()字符编码转换函数的用法,iconv()函数,在php5中是内置的,语法格式:iconv("UTF- 8","GB2312//IGNORE" ...

  3. NGUI类之间的关系和架构

    NGUI Drawcall 1.使用同一个altals的元素尽量放在同一个UIPanel下面,在NGUI中,它消耗的drawcall是以每个Panel为独立计算单位进行计算的. 2.如果一个UIPan ...

  4. Unity3d中的Awake()、OnEnable()、Start()等默认函数的执行顺序和生命周期

    Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数 ...

  5. Android Activity设置为全屏的方法

    1:代码方式: 放到setContentView之前 //隐藏标题栏 this.requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏状态栏 this. ...

  6. CSS浮动属性Float详解

    什么是CSS Float? float 是 css 的定位属性.在传统的印刷布局中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在 ...

  7. City Tour

    Description Alice想要从城市A出发到城市B,由于Alice最近比较穷(不像集训队陈兴老师是个rich second),所以只能选择做火车从A到B.不过Alice很讨厌坐火车,火车上人比 ...

  8. HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)

    Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...

  9. codevs1044:dilworth定理

    http://www.cnblogs.com/submarine/archive/2011/08/03/2126423.html dilworth定理的介绍 题目大意:求一个序列的lds 同时找出这个 ...

  10. sql语句相关操作

    create user test identified by test default tablespace users temporary tablespace temp quota 3M on u ...