XVcode动态 Web 校验码图片生成
XVcode,Web 动态验证码图片生成工具,用于生成随机验证码图片及返回验证码。以随机圆圈为干扰背景,增加随机前后景透明度,并提供3种gif动态图。
PngGenerator :
GifGenerator:
Gif2Generator:
Gif3Generator:
该项目用于生成gif图片编码器使用了 gifencoder 项目
快速上手
class Test { //生成验证码图片到本地磁盘 draw image and save to disk public void main(String args[]) throws IOException { String path = ".";//图片存储路径 path for image save Integer height = 40;//image 高度。 image height. count as pixel Integer width = 200;//image 宽度。 image width. count as pixel Integer count = 5; // validation code length. String validCode = null; //验证码 Generator generator = new PngVCGenerator(width, height, count); generator.write2out(new FileOutputStream(path + "/1.png")).close(); validCode = generator.text(); //get the validation code as 'String' System.out.println(validCode); generator = new GifVCGenerator(width, height, count);// gif generator.write2out(new FileOutputStream(path + "/1.gif")).close(); validCode = generator.text(); System.out.println(validCode); generator = new Gif2VCGenerator(width, height, count);// gif generator.write2out(new FileOutputStream(path + "/2.gif")).close(); validCode = generator.text(); System.out.println(validCode); generator = new Gif3VCGenerator(width, height, count);// gif generator.write2out(new FileOutputStream(path + "/3.gif")).close(); validCode = generator.text(); System.out.println(validCode); }
如果要将验证码图片以流的方式传到前端,可以直接使用generator.write2out()
方法
Use Under Command(require ${JAVA_HOME} set)
可以使用jar包直接生成本地图片。命令:
java -jar xvcode-1.0-SNAPSHOT-cl
usage :
支持参数如下:
usage: -p dir path for the image, default generate in current dir -h image height, between 50 to 500, default 200 -w image width, between 30 to 300, default 40 -cl validation code length, between 2 to 10, default 5
示例:
java -jar xvcode-1.0-SNAPSHOT-cl -p test/ -h 300 -w 60 -cl 7
评论