JAVA-简单实用生成图片验证码工具类
如何快速生成图片验证码
生成图片验证码是每个B/S架构项目的必备工具,该博文介绍如果快速、简单、生成实用的图片验证码;
该工具类已经在多个项目中使用。
第三方依赖关系
<dependency> <groupId>com.bladejava</groupId> <artifactId>blade-patchca</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.0.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency>
核心代码
- 生成图片验证码参数POJO类
import lombok.Data;
/**
* @author huangrusheng
* @version 1.0
* @date 2021/5/20 9:55
*/
@Data
public class ImageCaptchaParams {
/**
* 默认图片验证码的文字内容
*/
public static final String DEFAULT_WORDS = "123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
public static final String DEFAULT_FORMAT = "png";
/**
* 图片验证码宽度
*/
private int width;
/**
* 图片验证码高度
*/
private int height;
/**
* 最多生成几个文字
*/
private int maxWordAmount;
/**
* 最小生成几个文字
*/
private int minWordAmount;
/**
* 字体最大尺寸
*/
private int maxFontSize;
/**
* 文字最小尺寸
*/
private int minFontSize;
/**
* 生成图片验证码内容的字体
*/
private String words;
/**
* 图片类型
*/
private String format;
public ImageCaptchaParams(){
this.width = 200;
this.height = 60;
this.maxWordAmount = 5;
this.minWordAmount = 4;
this.minFontSize = 40;
this.maxFontSize = 50;
this.words = DEFAULT_WORDS;
this.format = DEFAULT_FORMAT;
}
}
2.以IO流、Base64、Web的img标签识别的Base64方式输出图片验证码类
import cn.hutool.core.codec.Base64;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.font.RandomFontFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author huangrusheng
* @version 1.0
* @date 2021/5/20 9:54
*/
public final class ImageCaptchaUtil {
private ImageCaptchaUtil(){}
/**
* 流方式输出
* @param params
* @param outputStream
* @return 图片文字内容
* @throws IOException
*/
public static String create(ImageCaptchaParams params, OutputStream outputStream) throws IOException{
ConfigurableCaptchaService captchaService = new ConfigurableCaptchaService();
captchaService.setColorFactory(new SingleColorFactory(Color.DARK_GRAY));
captchaService.setFilterFactory(new CurvesRippleFilterFactory(captchaService.getColorFactory()));
captchaService.setHeight(params.getHeight());
captchaService.setWidth(params.getWidth());
RandomWordFactory wordFactory = new RandomWordFactory();
wordFactory.setCharacters(params.getWords());
wordFactory.setMaxLength(params.getMaxWordAmount());
wordFactory.setMinLength(params.getMinWordAmount());
captchaService.setWordFactory(wordFactory);
RandomFontFactory fontFactory = new RandomFontFactory();
fontFactory.setMaxSize(params.getMaxFontSize());
fontFactory.setMinSize(params.getMinFontSize());
captchaService.setFontFactory(fontFactory);
return EncoderHelper.getChallangeAndWriteImage(captchaService,params.getFormat(),outputStream);
}
/**
* 以base64方式返回
* @param params
* @return 图片文字内容+base64格式的图片
* @throws IOException
*/
public static ImageCaptchaResult createBase64(ImageCaptchaParams params) throws IOException{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024 * 3);
String words = create(params,outputStream);
return new ImageCaptchaResult(words, Base64.encode(outputStream.toByteArray()));
}
/**
* 以Web的img识别的base64返回
* @param params
* @return 图片文字内容+base64格式的img标签识别的图片
* @throws IOException
*/
public static ImageCaptchaResult createBase64ForWeb(ImageCaptchaParams params) throws IOException{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024 * 3);
String words = create(params,outputStream);
StringBuilder webImageSrc = new StringBuilder();
webImageSrc.append("data:image/").append(params.getFormat().toLowerCase()).append(";base64,").append( Base64.encode(outputStream.toByteArray()));
return new ImageCaptchaResult(words,webImageSrc.toString());
}
}
3.生成图片验证码的返回结果
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author huangrusheng
* @version 1.0
* @date 2021/5/20 10:27
*/
@Data
@AllArgsConstructor
public class ImageCaptchaResult {
/**
* 图片验证码文字内容
*/
private String words;
/**
* 返回内容,例如正常的Base64或以Web的image标签识别的base64格式
*/
private String returnContent;
}
单元测试用例
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.io.FileOutputStream;
/**
* @author huangrusheng
* @version 1.0
* @date 2021/5/20 10:13
*/
@RunWith(JUnit4.class)
public class ImageCaptchaTest {
@Test
public void testCreate() throws Exception{
FileOutputStream fileOutputStream = new FileOutputStream("D:\\image_captcha.png");
System.out.println(ImageCaptchaUtil.create(new ImageCaptchaParams(),fileOutputStream));
fileOutputStream.close();
}
@Test
public void testCreateBase64() throws Exception{
ImageCaptchaResult result = ImageCaptchaUtil.createBase64(new ImageCaptchaParams());
System.out.println(result.getWords());
System.out.println(result.getReturnContent());
}
@Test
public void testCreateBase64ForWeb() throws Exception{
ImageCaptchaResult result = ImageCaptchaUtil.createBase64ForWeb(new ImageCaptchaParams());
System.out.println(result.getWords());
System.out.println(result.getReturnContent());
}
}
版权声明:本文为m0_38138879原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。