动态 Web 校验码图片生成 XVcode https://github.com/botaruibo/xvcode
主要用于生成web动态验证码图片。主要应用场景为web 页面上需要用户输入验证码才能进行操作的地方。 开发原因: 目前网络广为流传的代码所生成的验证码图片太简单,并易于破解。所以我们自己开发了一个简单的验证码生成包,增加了背景的干扰性。 features:
- 提供1中png格式的图片生成器,3种gif格式图片生成器。
- 随机码由生成器自身生成。
- 可一定程度自定义背景干扰图形参数
图片示例 mapping from generator to image style
Add Dependency(maven)
add dependency to your pom.xml:
1 2 3 4 5 |
<dependency> <groupId>com.github.botaruibo</groupId> <artifactId>xvcode</artifactId> <version>1.0</version> </dependency> |
####### Code Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.pandy.test;/** * Created by pandy on 17-5-3. */ import com.github.botaruibo.xvcode.generator.*; import java.io.FileOutputStream; import java.io.IOException; /** * 项目名称: idea * 功能说明: * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 17-5-3. * 创建时间: 下午10:40. * 修改历史: * ----------------------------------------------- */ public class XvcodeTest { //生成验证码图片到本地磁盘 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); } } |
the generator.write2out() method proved the ability to write the image binary to any OutputStream object. this especially convenience for servlet request
如果要将验证码图片以流的方式穿到前端,可以直接使用*generator.write2out()*方法