mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
6.x
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package cn.hutool.swing.captcha;
|
||||
|
||||
import cn.hutool.swing.img.GraphicsUtil;
|
||||
import cn.hutool.swing.img.ImgUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Image;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
/**
|
||||
* 圆圈干扰验证码
|
||||
*
|
||||
* @author looly
|
||||
* @since 3.2.3
|
||||
*
|
||||
*/
|
||||
public class CircleCaptcha extends AbstractCaptcha {
|
||||
private static final long serialVersionUID = -7096627300356535494L;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
*/
|
||||
public CircleCaptcha(int width, int height) {
|
||||
this(width, height, 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
*/
|
||||
public CircleCaptcha(int width, int height, int codeCount) {
|
||||
this(width, height, codeCount, 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param width 图片宽
|
||||
* @param height 图片高
|
||||
* @param codeCount 字符个数
|
||||
* @param interfereCount 验证码干扰元素个数
|
||||
*/
|
||||
public CircleCaptcha(int width, int height, int codeCount, int interfereCount) {
|
||||
super(width, height, codeCount, interfereCount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image createImage(String code) {
|
||||
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
final Graphics2D g = ImgUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE));
|
||||
|
||||
// 随机画干扰圈圈
|
||||
drawInterfere(g);
|
||||
|
||||
// 画字符串
|
||||
drawString(g, code);
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------- Private method start
|
||||
/**
|
||||
* 绘制字符串
|
||||
*
|
||||
* @param g {@link Graphics2D}画笔
|
||||
* @param code 验证码
|
||||
*/
|
||||
private void drawString(Graphics2D g, String code) {
|
||||
// 指定透明度
|
||||
if (null != this.textAlpha) {
|
||||
g.setComposite(this.textAlpha);
|
||||
}
|
||||
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height);
|
||||
}
|
||||
|
||||
/**
|
||||
* 画随机干扰
|
||||
*
|
||||
* @param g {@link Graphics2D}
|
||||
*/
|
||||
private void drawInterfere(Graphics2D g) {
|
||||
final ThreadLocalRandom random = RandomUtil.getRandom();
|
||||
|
||||
for (int i = 0; i < this.interfereCount; i++) {
|
||||
g.setColor(ImgUtil.randomColor(random));
|
||||
g.drawOval(random.nextInt(width), random.nextInt(height), random.nextInt(height >> 1), random.nextInt(height >> 1));
|
||||
}
|
||||
}
|
||||
// ----------------------------------------------------------------------------------------------------- Private method end
|
||||
}
|
||||
Reference in New Issue
Block a user