验证码的实现类ValidateCode
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
* 验证码生成器
*
* @author
*/
public class ValidateCode {
// 图片的宽度。
private int width = 160;
// 图片的高度。
private int height = 40;
// 验证码字符个数
private int codeCount = 5;
// 验证码干扰线数
private int lineCount = 150;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null;
private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
* 默认构造函数,设置默认参数
*/
public ValidateCode() {
this.createCode();
}
* @param width 图片宽
* @param height 图片高
*/
public ValidateCode(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
public ValidateCode(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode();
}
int x = 0, fontHeight = 0, codeY = 0;
int red = 0, green = 0, blue = 0;
fontHeight = height - 2;//字体的高度
codeY = height - 4;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 生成随机数
Random random = new Random();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体,可以修改为其它的
Font font = new Font("Fixedsys", Font.PLAIN, fontHeight);
// Font font = new Font("Times New Roman", Font.ROMAN_BASELINE, fontHeight);
g.setFont(font);
// 设置随机开始和结束坐标
int xs = random.nextInt(width);//x坐标开始
int ys = random.nextInt(height);//y坐标开始
int xe = xs + random.nextInt(width / 8);//x坐标结束
int ye = ys + random.nextInt(height / 8);//y坐标结束
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawLine(xs, ys, xe, ye);
}
StringBuffer randomCode = new StringBuffer();
// 随机产生codeCount个字符的验证码。
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
// 产生随机的颜色值,让输出的每个字符的颜色值都将不同。
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i + 1) * x, codeY);
// 将产生的四个随机数组合在一起。
randomCode.append(strRand);
}
// 将四位数字的验证码保存到Session中。
code = randomCode.toString();
}
OutputStream sos = new FileOutputStream(path);
this.write(sos);
}
ImageIO.write(buffImg, "png", sos);
sos.close();
}
return buffImg;
}
return code.toLowerCase();
}
}
验证码的实现类ValidateCode的更多相关文章
- 生成随机数验证码的工具类(from韩顺平)
生成随机数验证码的工具类 package com.cx; //生成随机数的图片 import java.awt.Color; import java.awt.Font; import java.awt ...
- 验证码类validateCode
PHP验证码类,代码如下: <?php //验证码类 class ValidateCode { private $charset = 'abcdefghkmnprstuvwxyzABCDEFGH ...
- C#生成漂亮验证码完整代码类
using System;using System.Web;using System.Drawing;using System.Security.Cryptography; namespace Dot ...
- c# 验证码图片生成类
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...
- 【Android工具类】验证码倒计时帮助类CountDownButtonHelper的实现
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 我们在做有关短信验证码功能的时候.为了防止用户无休止的获取短信验证码,或者是误操作.造成验证码混乱的情况.我 ...
- java生成图片验证码(转)--封装生成图片验证码的工具类
博客部分内容转载自 LonlySnow的博客:后台java 实现验证码生成 1.controller方法 @RequestMapping(value = "/verifycode/img&q ...
- java图形验证码生成工具类及web页面校验验证码
最近做验证码,参考网上案例,发现有不少问题,特意进行了修改和完善. 验证码生成器: import javax.imageio.ImageIO; import java.awt.*; import ja ...
- java 验证码图片处理类,为验证码识别做准备
/* * To change this template, choose Tools | Templates * and open the template in the editor. */pack ...
- 从.Net到Java学习第十一篇——SpringBoot登录实现
从.Net到Java学习系列目录 通过前面10篇文章的学习,相信我们对SpringBoot已经有了一些了解,那么如何来验证我们的学习成果呢?当然是通过做项目来证明啦!所以从这一篇开始我将会对之前自己做 ...
随机推荐
- .NET Core api部署到IIS上405的问题
今天部署到iis 服务器上,api的put请求一直报405.其他像get post都没问题. google了半天,找到两种解决方案,亲测都可以.但我个人认为不是最理想的解决方案. 1.IIS拒绝PUT ...
- buffer 与 cache 的区别
Buffer 和 Cache buffer 和 cache 同样作为缓存,他们之间有什么区别呢? 简单来说,buffer 是即将要写入磁盘的缓存,而 cache 是从磁盘中读出来放到缓存的 参考来自: ...
- 使用shared memory 计算矩阵乘法 (其实并没有加速多少)
#include "cuda_runtime.h" #include "device_launch_parameters.h" #include "d ...
- 简单实现app使用PC图片
提一个很人性化的需求: 在自己的app里使用PC里的图片. 关键点:传输.怎么把图片从PC导入自己的APP. 因为iOS的封闭性,一般用户不能很方便把图片导入手机相册.笔者稍微想了下,实现功能其实也有 ...
- LINUX档案权限
一.ls命令显示目录详细信息 列表显示目录内容 : ls drwxr-xr-x. 4 root root 4096 Jun 26 02:07 acpi -rw-r--r--. 1 ...
- MySQL之日期时间函数
1.NOW() 用法:显示当前日期和时间 举例: mysql> select NOW(); +---------------------+ | NOW() | +-------------- ...
- 使用url_for()时,会自动调用转换器的to_url()方法
视图反推url,在动态url(转换器)反推中的应用 # -*- coding: utf-8 -*- from flask import Flask, url_for, redirect from we ...
- 完整的ELK+filebeat+kafka笔记
之前有写过elasticsearch集群和elk集群的博客, 都是基于docker的,使用docker-compose进行编排(K8S暂未掌握) 三台服务器搭建es集群:https://www.cnb ...
- Linux命令——mkdir、rmdir
mkdir -p:递归创建目录 -v:每次创建新目录都显示信息 -m:直接指定权限(不使用默认umask演算的那个) [root@51cto /]# mkdir /a/b/c -pv mkdir: c ...
- 3.Hbase数据模型
3.1.Hbase数据模型: 概念视图: 物理视图 Hbase数据在存储系统中是以列族来体现的[Column Family],任何时候可以随意的添加一列到已经存在的列族中 空的单元格在表中不做存储也不 ...