java生成图形验证码
效果图

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 生成图形验证码
*/
public class AuthImg extends HttpServlet {
private static final long serialVersionUID = 1L;
// 定义图形验证码中绘制的字符的字体
private final Font mFont = new Font("Arial Black", Font.PLAIN, 23);
// 图形验证码的大小
private final int IMG_WIDTH = 72;
private final int IMG_HEIGTH = 27;
// 获取随机颜色的方法
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);
}
// 获取随机字符串
private String getRandomChar() {
int rand = (int) Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand) {
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char) itmp;
return String.valueOf(ctmp);
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char) itmp;
return String.valueOf(ctmp);
default:
itmp = Math.round(Math.random() * 9);
return itmp + "";
}
}
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置禁止缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGTH, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
// 填充背景色
g.fillRect(1, 1, IMG_WIDTH - 1, IMG_HEIGTH - 1);
// 为图形验证码绘制边框
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, IMG_WIDTH, IMG_HEIGTH);
g.setColor(getRandColor(160, 200));
// 生成随机干扰线
for (int i = 0; i < 80; i++) {
int x = random.nextInt(IMG_WIDTH - 1);
int y = random.nextInt(IMG_HEIGTH - 1);
int x1 = random.nextInt(6) + 1;
int y1 = random.nextInt(12) + 1;
g.drawLine(x, y, x + x1, y + y1);
}
g.setColor(getRandColor(160, 200));
// 生成随机干扰线
for (int i = 0; i < 80; i++) {
int x = random.nextInt(IMG_WIDTH - 1);
int y = random.nextInt(IMG_HEIGTH - 1);
int x1 = random.nextInt(12) + 1;
int y1 = random.nextInt(6) + 1;
g.drawLine(x, y, x - x1, y - y1);
}
// 设置绘制字符的字体
g.setFont(mFont);
// 用于保存系统生成的随机字符串
String sRand = "";
for (int i = 0; i < 4; i++) {
String tmp = getRandomChar();
sRand += tmp;
g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
g.drawString(tmp, 15 * i + 10, 20);
}
// 获取HttpSession对象
HttpSession session = request.getSession(true);
session.removeAttribute("rand");
session.setAttribute("rand", sRand);
g.dispose();
// 向输出流中输出图片
ImageIO.write(image, "JPEG", response.getOutputStream());
}
}
java生成图形验证码的更多相关文章
- C#生成图形验证码
先看效果: 再上代码 public class CaptchaHelper { private static Random rand = new Random(); private static in ...
- PHP5 GD库生成图形验证码(汉字)
PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...
- PHP5生成图形验证码(有汉字)
利用PHP5中GD库生成图形验证码 类似于下面这样 1.利用GD库函数生成图片,并在图片上写指定字符 imagecreatetruecolor 新建一个真彩色图像 imagecolora ...
- 【Java】生成图形验证码
本章介绍一个能生成比较好看的图形验证码类 生成验证码工具类 package com.util; import java.awt.Color; import java.awt.Font; import ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- (转)Android 之生成图形验证码
import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; impor ...
- Java 之图形验证码
图形验证码作用 防止恶意注册 防暴力破解 Java 与图片相关的类: Image, ImageIO, BufferedImage, Icon, ImageIcon public static void ...
- ASP.NET中如何生成图形验证码
通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3 ...
- Net Core 生成图形验证码
1. NetCore ZKweb 在我第一次绘制图形验证码时是采用的ZKweb的绘制库,奉上代码参考 public byte[] GetVerifyCode(out string ...
随机推荐
- 腾讯云Windows2016数据中文版环境搭建
最近忙活了好几天,在腾讯云上买了台服务器,系统是Windows2016数据中文版,用于个人的学习,下面说一下整个流程吧. 遇到的问题: 一开始是按照腾讯云的指南文档去搞环境配置的,但它上面都是以Win ...
- python --- excel文件处理
1.安装第三方库:openpyxl 2.操作示例 from openpyxl import load_workbook #.打开文件 file = load_workbook("test.x ...
- POJ 1320:Street Numbers
Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2753 Accepted: 1530 De ...
- OpenCV3 Ref SVM : cv::ml::SVM Class Reference
OpenCV3 Ref SVM : cv::ml::SVM Class Reference OpenCV2: #include <opencv2/core/core.hpp>#inclu ...
- Java算法练习——整数转罗马数字
题目链接 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 ...
- hostapd 热点设置
一. 需要安装的服务:hostapd dhcp(isc-dhcp-server) dns(dnsmasq或者bind9或者都不装设成8.8.8.8 ) 二.建hostapd.conf文件 ,su ...
- 标准库模块——json模块
将Python数据类型转换为其他代码格式叫做(序列化),而json就是在各个代码实现转换的中间件. 序列化要求: 1. 只能有int,str,bool,list,dict,tuple的类型支持序列化. ...
- Day 12:枚举值、枚举类
jdk1.5新特性之-----枚举 问题:某些方法所接收的数据必须是在固定范围之内的, 解决方案: 这时候我们的解决方案就是自定义一个类,然后是私有化构造函数,在自定义类中创建本类的对象对外使用. ...
- HDU 5311:Hidden String
Hidden String Accepts: 437 Submissions: 2174 Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- c++ 字母排序
char a[123] = {'Z', 's', 'p', 'l', 'j', 'r', 'q', 'v', 'n', 'm', 'C', 'F', 'D', 'B', 'A', '2', '0', ...