Servlet实现图片验证码
2015-07-30 15:51
393 查看
public static final char[] CHARS = { '0', '1', '2', '3', '4', '5', 'A',
'B', 'C', 'D' };
public static Random random = new Random();
public static String getRandomString() {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 4; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
public static Color getRandomColor() {
return new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
}
public static Color getReverseColor(Color c) {
return new Color((255 - c.getRed()), (255 - c.getGreen()),
(255 - c.getBlue()));
}
String randomString = getRandomString();
request.getSession(true).setAttribute("randomString", randomString);
int width = 100;
int height = 30;
Color color = getRandomColor();
Color reverse = getReverseColor(color);
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
Graphics2D g = bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(reverse);
g.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++) {
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
<img alt="abc" src="servlet/IdentityServlet" id="identity" onload="btn.disable = false;" />
'B', 'C', 'D' };
public static Random random = new Random();
public static String getRandomString() {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 4; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
public static Color getRandomColor() {
return new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
}
public static Color getReverseColor(Color c) {
return new Color((255 - c.getRed()), (255 - c.getGreen()),
(255 - c.getBlue()));
}
String randomString = getRandomString();
request.getSession(true).setAttribute("randomString", randomString);
int width = 100;
int height = 30;
Color color = getRandomColor();
Color reverse = getReverseColor(color);
BufferedImage bi = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
Graphics2D g = bi.createGraphics();
g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
g.setColor(color);
g.fillRect(0, 0, width, height);
g.setColor(reverse);
g.drawString(randomString, 18, 20);
for (int i = 0, n = random.nextInt(100); i < n; i++) {
g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
ServletOutputStream out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(bi);
out.flush();
<img alt="abc" src="servlet/IdentityServlet" id="identity" onload="btn.disable = false;" />
相关文章推荐
- [Linux] - CentOS 安装nginx
- 如何过上简单的生活
- with: __enter__ and __exit__
- C#生成Code39条形码【非条形码字体】
- SSH配置拦截器
- neutron router-update如何增加路由
- 黑马程序员--C语言-指针函数及应用
- mongodb的用户认证
- java string 中文转换UTF-8
- 面向对象链接整理
- android:gravity="right" 没有居右侧
- Android 客户端登陆超时退出处理方案
- Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案
- mysql处理上百万条的数据库如何优化语句来提高处理查询效率
- linux下vsftp软件正常启动了,也能正常连接,但是就是没办法正常浏览文件夹
- C#生成Code39条形码【非条形码字体】
- USB的枚举过程--自己写的(转载)
- Eclipse/MyEclipse自动补全的设置(自动提示)
- SetUnhandledExceptionFilter让程序优雅的崩溃
- FreeMarker使用说明