CaptchaUtil主要讲述如何生成验证码图片,并指定设置图片大小,验证码大小及干扰项等
无
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
/**
*
* @模块名:taf
* @包名:pers.cc.taf.io.img
* @类名称: CaptchaUtil
* @类描述:【类描述】图片验证码工具类
* @版本:1.0
* @创建人:cc
* @创建时间:2018年9月11日上午10:06:40
*/
public class CaptchaUtil {
/**
* 图片的宽度
*/
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;
/**
* 验证码范围,去掉0(数字)和O(拼音)容易混淆的(小写的1和L也可以去掉,大写不用了)
*/
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 CaptchaUtil() {
this.createCode();
}
/**
* 构造函数
*
* @param width 图片宽
* @param height 图片高
*/
public CaptchaUtil(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
/**
* 构造函数
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
public CaptchaUtil(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode();
}
/**
*
* @方法名:createCode
* @方法描述【方法功能描述】生成验证码
* @修改描述【修改描述】
* @版本:1.0
* @创建人:cc
* @创建时间:2018年9月11日 上午10:10:35
* @修改人:cc
* @修改时间:2018年9月11日 上午10:10:35
*/
public void createCode() {
int x = 0, fontHeight = 0, codeY = 0;
int red = 0, green = 0, blue = 0;
x = width / (codeCount + 2);// 每个字符的宽度(左右各空出一个字符)
fontHeight = height - 2;// 字体的高度
codeY = height - 4;
// 图像buffer
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);
for (int i = 0; i < lineCount; i++) {
// 设置随机开始和结束坐标
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);
}
// randomCode记录随机产生的验证码
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();
}
/**
*
* @方法名:write
* @方法描述【方法功能描述】将验证码图片写入指定路径
* @param path
* @throws IOException
* @修改描述【修改描述】
* @版本:1.0
* @创建人:cc
* @创建时间:2018年9月11日 上午10:10:55
* @修改人:cc
* @修改时间:2018年9月11日 上午10:10:55
*/
public void write(String path) throws IOException {
OutputStream sos = new FileOutputStream(path);
this.write(sos);
}
/**
*
* @方法名:write
* @方法描述【方法功能描述】将验证码图片写入指定输出流
* @param sos
* @throws IOException
* @修改描述【修改描述】
* @版本:1.0
* @创建人:cc
* @创建时间:2018年9月11日 上午10:11:30
* @修改人:cc
* @修改时间:2018年9月11日 上午10:11:30
*/
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
/**
*
* @方法名:getBuffImg
* @方法描述【方法功能描述】获取验证码图片
* @return 验证码图片BufferedImage
* @修改描述【修改描述】
* @版本:1.0
* @创建人:cc
* @创建时间:2018年9月11日 上午10:12:08
* @修改人:cc
* @修改时间:2018年9月11日 上午10:12:08
*/
public BufferedImage getBuffImg() {
return buffImg;
}
/**
*
* @方法名:getCode
* @方法描述【方法功能描述】获取验证码
* @return 验证码
* @修改描述【修改描述】
* @版本:1.0
* @创建人:cc
* @创建时间:2018年9月11日 上午10:12:44
* @修改人:cc
* @修改时间:2018年9月11日 上午10:12:44
*/
public String getCode() {
return code;
}
public static void main(String[] args) {
CaptchaUtil vCode = new CaptchaUtil(160, 40, 5, 150);
try {
String path = "D://a/" + new Date().getTime() + ".png";
System.out.println(vCode.getCode() + " >" + path);
vCode.write(path);
// ValidateCode vCode = new ValidateCode(120,40,5,100);
// session.setAttribute("code", vCode.getCode());
// vCode.write(response.getOutputStream());
}
catch (IOException e) {
e.printStackTrace();
}
}
}
NLTK 和jieba官方网址: http://www.nltk.org/python上著名的自然语言处理库,自带语料库,词性分类库,自带分类,分词,等等功能,还有N多的简单版wrapper安装NLTK和jieba:sudo pip install -U nltksudo pip install -U numpysudo pip install jieba下载语料库:import...
周末跟曾经的一位程序员同事聊天,他说他貌似遇到了技术的瓶颈了,最近一直在刻苦攻读,希望可以突破自己取得进步。有时候想想自己还挺惭愧的,高手们况且都还在不断的努力,自己的日子过的总是有点太安逸。我们总是在清醒的时候,喜欢为自己制定计划,每天要完成多少行代码,多少天要看完一本技术方面的书,但是计划总是没有办法实现,因为我们有一个口头禅叫"没时间"。 编程其实没有捷径可走,虽然你可以
查看Oracle执行计划的几种方法一、通过PL/SQL Dev工具 1、直接File->New->Explain Plan Window,在窗口中执行sql可以查看计划结果。其中,Cost表示cpu的消耗,单位为n%,Cardinality表示执行的行数,等价Rows。 2、先执行 EXPLAIN PLAN FOR select * f
用来复习大学期末应该是足够了,考研还有知识点没有写进去
越来越多H5页面需要获取用户微信信息,具体怎么操作,下面是封装好的类直接调用即可。class WxService{ protected $appid; protected $appKey; public $data = null; public function __construct($appid, $appKey) { $this->appid = $appid; //微信支付申请对应的公众号的APPID $t
方式1:通过请求查询接口获取ip归属地,比较慢,因为要一个一个去发送请求。方式2:通过第三方库qqwry来请求接口,推荐,速度超快,需要将纯真ip数据库下载到本地。
用例的官方定义是:用例定义了一组用例实例,其中每个用例实例都是系统所执行的一系列操作,这些操作生成特定主角可以观察的值。具体的说,用例是一件事,完成这件事需要一系列的活动,做这件事可以用不同的方法和步骤,可会遇到各种情况,因此这件事就是由很多不同的情况集合构成的,这些情况在UML称为用例场景,一个场景就是用例的实例。 一个系统的功能性是由一些对系统有愿望的主角要做的一些事构成的,当全部主角的
一、Pyecharts 简介Echarts是一个由百度开源的数据可视化工具,凭借着良好的交互性,精巧的图表设计,得到了众多开发者的认可。而 Python 是一门富有表达力的语言,很适合用于数据处理。当数据分析遇上数据可视化时,Pyecharts诞生了。Pyecharts最早只适用于工程领域的可视化开发,但是随着其对Jupyter notebook、Jupyter lab等交互式开发工具的支持不断加强,现在也开始被许多数据分析师应用到数据探索中。注:V1.x之前的版本已不再维护,建议直接学习最新V 1
展开全部给你个做好了的Java的源程序的记事本32313133353236313431303231363533e4b893e5b19e31333339663935,自己看看就行了的,不怎么难的···import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class MyNotepa...
过去10年间最成功的社交网络是这样产生的:我们需要有一个真实的资料页面认识其他人,并且分享自己的生活,于是有了Facebook;我们需要一个代表职业身份的资料页面,描绘自己的职业生涯,于是有了LinkedIn;进入移动互联网时代,写博客交流文字费时费力,我们需要快速即时的沟通,于是有了Twitter;手机摄像头进步了,人们想拍摄更美的照片并且分享给别人,...
在tomcat启动时报invalid LOC header (bad signature)错误 Caused by: java.lang.IllegalArgumentException: java.util.zip.ZipException: invalid LOC header (bad signature) at org.apache.catalina.webresource...
下面介绍了 iOS 优化 ipa 安装包大小的几种方法。