mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add GifCaptcha
This commit is contained in:
parent
4d1bb996fc
commit
ba2a8156d5
@ -30,6 +30,7 @@
|
||||
* 【all 】 增加Hutool.getAllUtils和printAllUtils方法
|
||||
* 【core 】 增加PunyCode(issue#1268@Gitee)
|
||||
* 【core 】 ArrayUtil增加isSorted方法(pr#1271@Github)
|
||||
* 【captcha】 增加GifCaptcha(pr#1273@Github)
|
||||
|
||||
### Bug修复
|
||||
* 【cron 】 修复CronTimer可能死循环的问题(issue#1224@Github)
|
||||
|
@ -84,11 +84,26 @@ public class CaptchaUtil {
|
||||
return new ShearCaptcha(width, height, codeCount, thickness);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建GIF验证码
|
||||
*
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
public static GifCaptcha createGifCaptcha(int width, int height) {
|
||||
return new GifCaptcha(width, height);
|
||||
}
|
||||
|
||||
public static GifCaptcha createGifCaptcha(int width, int height, int delay) {
|
||||
return new GifCaptcha(width, height, delay);
|
||||
/**
|
||||
* 创建GIF验证码
|
||||
*
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @param codeCount 字符个数
|
||||
* @return {@link GifCaptcha}
|
||||
*/
|
||||
public static GifCaptcha createGifCaptcha(int width, int height, int codeCount) {
|
||||
return new GifCaptcha(width, height, codeCount);
|
||||
}
|
||||
}
|
||||
|
@ -3,21 +3,24 @@ package cn.hutool.captcha;
|
||||
|
||||
import cn.hutool.core.img.gif.AnimatedGifEncoder;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Gif验证码类
|
||||
*
|
||||
* @author hsoftxl
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public class GifCaptcha extends AbstractCaptcha {
|
||||
private static final long serialVersionUID = 7091627304326538464L;
|
||||
|
||||
private final Random RANDOM = new Random();
|
||||
// 帧延迟 (默认100)
|
||||
private int delay = 100;
|
||||
//量化器取样间隔 - 默认是10ms
|
||||
private int quality = 10;
|
||||
// 帧循环次数
|
||||
@ -31,17 +34,17 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
/**
|
||||
* 可以设置验证码宽度,高度的构造函数
|
||||
*
|
||||
* @param width -验证码宽度
|
||||
* @param height -验证码高度
|
||||
* @param width 验证码宽度
|
||||
* @param height 验证码高度
|
||||
*/
|
||||
public GifCaptcha(int width, int height) {
|
||||
this(width, height, 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param width -验证码宽度
|
||||
* @param height -验证码高度
|
||||
* @param codeCount -验证码个数
|
||||
* @param width 验证码宽度
|
||||
* @param height 验证码高度
|
||||
* @param codeCount 验证码个数
|
||||
*/
|
||||
public GifCaptcha(int width, int height, int codeCount) {
|
||||
super(width, height, codeCount, 10);
|
||||
@ -54,12 +57,14 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
* 值更大(大于20)不产生显著的改善速度
|
||||
*
|
||||
* @param quality 大于1
|
||||
* @return this
|
||||
*/
|
||||
public void setQuality(int quality) {
|
||||
public GifCaptcha setQuality(int quality) {
|
||||
if (quality < 1) {
|
||||
quality = 1;
|
||||
}
|
||||
this.quality = quality;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,29 +73,35 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
* 必须在添加的第一个图像之前被调用。
|
||||
*
|
||||
* @param repeat 必须大于等于0
|
||||
* @return this
|
||||
*/
|
||||
public void setRepeat(int repeat) {
|
||||
public GifCaptcha setRepeat(int repeat) {
|
||||
if (repeat >= 0) {
|
||||
this.repeat = repeat;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置验证码字符颜色
|
||||
*
|
||||
* @param maxColor 颜色
|
||||
* @return this
|
||||
*/
|
||||
public void setMaxColor(int maxColor) {
|
||||
public GifCaptcha setMaxColor(int maxColor) {
|
||||
this.maxColor = maxColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置验证码字符颜色
|
||||
*
|
||||
* @param minColor 颜色
|
||||
* @return this
|
||||
*/
|
||||
public void setMinColor(int minColor) {
|
||||
public GifCaptcha setMinColor(int minColor) {
|
||||
this.minColor = minColor;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -102,6 +113,8 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
//生成字符
|
||||
gifEncoder.start(out);
|
||||
gifEncoder.setQuality(quality);//设置量化器取样间隔
|
||||
// 帧延迟 (默认100)
|
||||
int delay = 100;
|
||||
gifEncoder.setDelay(delay);//设置帧延迟
|
||||
gifEncoder.setRepeat(repeat);//帧循环次数
|
||||
BufferedImage frame;
|
||||
@ -149,10 +162,14 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
g2d.setComposite(this.textAlpha);
|
||||
}
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getPellucidity(chars.length, flag, i));
|
||||
ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(chars.length, flag, i));
|
||||
g2d.setComposite(ac);
|
||||
g2d.setColor(fontColor[i]);
|
||||
g2d.drawOval(num(width), num(height), num(5, 30), 5 + num(5, 30));//绘制椭圆边框
|
||||
g2d.drawOval(
|
||||
RandomUtil.randomInt(width),
|
||||
RandomUtil.randomInt(height),
|
||||
RandomUtil.randomInt(5, 30), 5 + RandomUtil.randomInt(5, 30)
|
||||
);//绘制椭圆边框
|
||||
g2d.drawString(words[i] + "", x + (font.getSize() + m) * i, y);
|
||||
}
|
||||
g2d.dispose();
|
||||
@ -164,7 +181,7 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
*
|
||||
* @return float 透明度
|
||||
*/
|
||||
private float getPellucidity(int v, int i, int j) {
|
||||
private float getAlpha(int v, int i, int j) {
|
||||
int num = i + j;
|
||||
float r = (float) 1 / v;
|
||||
float s = (v + 1) * r;
|
||||
@ -193,29 +210,9 @@ public class GifCaptcha extends AbstractCaptcha {
|
||||
min = 0;
|
||||
max = 255;
|
||||
}
|
||||
return new Color(min + num(max - min), min + num(max - min), min + num(max - min));
|
||||
return new Color(
|
||||
RandomUtil.randomInt(min, max),
|
||||
RandomUtil.randomInt(min, max),
|
||||
RandomUtil.randomInt(min, max));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 产生两个数之间的随机数
|
||||
*
|
||||
* @param min 小数
|
||||
* @param max 比min大的数
|
||||
* @return int 随机数字
|
||||
*/
|
||||
private int num(int min, int max) {
|
||||
return min + RANDOM.nextInt(max - min);
|
||||
}
|
||||
|
||||
/**
|
||||
* 产生0--num的随机数,不包括num
|
||||
*
|
||||
* @param num 数字
|
||||
* @return int 随机数字
|
||||
*/
|
||||
private int num(int num) {
|
||||
return RANDOM.nextInt(num);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ public class CaptchaTest {
|
||||
@Ignore
|
||||
public void GifCaptchaTest() {
|
||||
GifCaptcha captcha = CaptchaUtil.createGifCaptcha(200, 100, 4);
|
||||
captcha.write("f:/gif_captcha.gif");
|
||||
captcha.write("d:/test/gif_captcha.gif");
|
||||
assert captcha.verify(captcha.getCode());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user