增加设置验证码大小和针对alias注释

This commit is contained in:
Looly
2024-04-11 21:28:50 +08:00
parent b2dbd55c1e
commit b859cf4913
8 changed files with 192 additions and 51 deletions

View File

@@ -352,4 +352,11 @@ public class JSONUtilTest {
final String jsonStr = JSONUtil.toJsonStr(true);
Assertions.assertEquals("true", jsonStr);
}
@Test
public void issue3540Test() {
final Long userId=10101010L;
final String jsonStr = JSONUtil.toJsonStr(userId);
Assertions.assertEquals("10101010", jsonStr);
}
}

View File

@@ -81,6 +81,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
private final AtomicInteger currentRow;
// region Constructors
/**
* 构造默认生成xls格式的Excel文件<br>
* 此构造不传入写出的Excel文件路径只能调用{@link #flush(OutputStream)}方法写出到流<br>
@@ -449,6 +450,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
/**
* 设置是否只保留别名中的字段值如果为true则不设置alias的字段将不被输出false表示原样输出
* Bean中设置@Alias时setOnlyAlias是无效的这个参数只和addHeaderAlias配合使用原因是注解是Bean内部的操作而addHeaderAlias是Writer的操作不互通。
*
* @param isOnlyAlias 是否只保留别名中的字段值
* @return this
@@ -799,6 +801,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
}
// region ----- writeImg
/**
* 写出数据本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
* 添加图片到当前sheet中 / 默认图片类型png / 默认的起始坐标和结束坐标都为0
@@ -899,6 +902,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
// endregion
// region ----- writeRow
/**
* 写出一行标题数据<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
@@ -1070,6 +1074,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
// endregion
// region ----- writeCol
/**
* 从第1列开始按列写入数据(index 从0开始)<br>
* 本方法只是将数据写入Workbook中的Sheet并不写出到文件<br>
@@ -1080,7 +1085,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
* @return this
*/
public ExcelWriter writeCol(final Map<?,? extends Iterable<?>> colMap, final boolean isWriteKeyAsHead){
public ExcelWriter writeCol(final Map<?, ? extends Iterable<?>> colMap, final boolean isWriteKeyAsHead) {
return writeCol(colMap, 0, isWriteKeyAsHead);
}
@@ -1095,12 +1100,12 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isWriteKeyAsHead 是否将Map的Key作为表头输出如果为True第一行为表头紧接着为values
* @return this
*/
public ExcelWriter writeCol(final Map<?,? extends Iterable<?>> colMap, int startColIndex, final boolean isWriteKeyAsHead){
public ExcelWriter writeCol(final Map<?, ? extends Iterable<?>> colMap, int startColIndex, final boolean isWriteKeyAsHead) {
for (final Object k : colMap.keySet()) {
final Iterable<?> v = colMap.get(k);
if(v != null){
writeCol(isWriteKeyAsHead?k:null,startColIndex, v, startColIndex != colMap.size() - 1);
startColIndex ++;
if (v != null) {
writeCol(isWriteKeyAsHead ? k : null, startColIndex, v, startColIndex != colMap.size() - 1);
startColIndex++;
}
}
return this;
@@ -1118,8 +1123,8 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方
* @return this
*/
public ExcelWriter writeCol(final Object headerVal, final Iterable<?> colData, final boolean isResetRowIndex){
return writeCol(headerVal,0,colData,isResetRowIndex);
public ExcelWriter writeCol(final Object headerVal, final Iterable<?> colData, final boolean isResetRowIndex) {
return writeCol(headerVal, 0, colData, isResetRowIndex);
}
/**
@@ -1134,18 +1139,18 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
* @param isResetRowIndex 如果为true写入完毕后Row index 将会重置为写入之前的未知如果为false写入完毕后Row index将会在写完的数据下方
* @return this
*/
public ExcelWriter writeCol(final Object headerVal, final int colIndex, final Iterable<?> colData, final boolean isResetRowIndex){
public ExcelWriter writeCol(final Object headerVal, final int colIndex, final Iterable<?> colData, final boolean isResetRowIndex) {
Assert.isFalse(this.isClosed, "ExcelWriter has been closed!");
int currentRowIndex = currentRow.get();
if(null != headerVal){
writeCellValue(colIndex, currentRowIndex, headerVal,true);
if (null != headerVal) {
writeCellValue(colIndex, currentRowIndex, headerVal, true);
currentRowIndex++;
}
for (final Object colDatum : colData) {
writeCellValue(colIndex, currentRowIndex, colDatum);
currentRowIndex++;
}
if(!isResetRowIndex){
if (!isResetRowIndex) {
currentRow.set(currentRowIndex);
}
return this;
@@ -1153,6 +1158,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
// endregion
// region ----- writeCellValue
/**
* 给指定单元格赋值,使用默认单元格样式
*
@@ -1196,6 +1202,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
// endregion
// region ----- setStyle
/**
* 设置某个单元格的样式<br>
* 此方法用于多个单元格共享样式的情况<br>
@@ -1313,6 +1320,7 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
// endregion
// region ----- flush
/**
* 将Excel Workbook刷出到预定义的文件<br>
* 如果用户未自定义输出的文件,将抛出{@link NullPointerException}<br>

View File

@@ -99,12 +99,25 @@ public abstract class AbstractCaptcha implements ICaptcha {
* @param interfereCount 验证码干扰元素个数
*/
public AbstractCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount) {
this(width, height, generator, interfereCount, 0.75f);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param generator 验证码生成器
* @param interfereCount 验证码干扰元素个数
* @param sizeBaseHeight 字体的大小 高度的倍数
*/
public AbstractCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount, final float sizeBaseHeight) {
this.width = width;
this.height = height;
this.generator = generator;
this.interfereCount = interfereCount;
// 字体高度设为验证码高度-2留边距
this.font = new Font(Font.SANS_SERIF, Font.PLAIN, (int) (this.height * 0.75));
this.font = new Font(Font.SANS_SERIF, Font.PLAIN, (int) (this.height * sizeBaseHeight));
}
@Override
@@ -114,7 +127,7 @@ public abstract class AbstractCaptcha implements ICaptcha {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Image image = null;
try{
try {
image = createImage(this.code);
ImgUtil.writePng(image, out);
} finally {
@@ -222,7 +235,7 @@ public abstract class AbstractCaptcha implements ICaptcha {
* @return 图片带文件格式的 Base64
* @since 5.3.11
*/
public String getImageBase64Data(){
public String getImageBase64Data() {
return UrlUtil.getDataUriBase64("image/png", getImageBase64());
}

View File

@@ -44,6 +44,21 @@ public class CaptchaUtil {
return new LineCaptcha(width, height, codeCount, lineCount);
}
/**
* 创建线干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
* @param sizeBaseHeight 字体的大小 高度的倍数
* @return {@link LineCaptcha}
*/
public static LineCaptcha ofLineCaptcha(final int width, final int height, final int codeCount,
final int lineCount, final float sizeBaseHeight) {
return new LineCaptcha(width, height, codeCount, lineCount, sizeBaseHeight);
}
/**
* 创建圆圈干扰的验证码默认5位验证码15个干扰圈
*
@@ -70,6 +85,21 @@ public class CaptchaUtil {
return new CircleCaptcha(width, height, codeCount, circleCount);
}
/**
* 创建圆圈干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param circleCount 干扰圆圈条数
* @param size 字体的大小 高度的倍数
* @return {@link CircleCaptcha}
*/
public static CircleCaptcha ofCircleCaptcha(final int width, final int height, final int codeCount,
final int circleCount, final float size) {
return new CircleCaptcha(width, height, codeCount, circleCount, size);
}
/**
* 创建扭曲干扰的验证码默认5位验证码
*
@@ -96,6 +126,20 @@ public class CaptchaUtil {
return new ShearCaptcha(width, height, codeCount, thickness);
}
/**
* 创建扭曲干扰的验证码默认5位验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param thickness 干扰线宽度
* @param sizeBaseHeight 字体的大小 高度的倍数
* @return {@link ShearCaptcha}
*/
public static ShearCaptcha ofShearCaptcha(final int width, final int height, final int codeCount, final int thickness, final float sizeBaseHeight) {
return new ShearCaptcha(width, height, codeCount, thickness, sizeBaseHeight);
}
/**
* 创建GIF验证码
*
@@ -118,4 +162,19 @@ public class CaptchaUtil {
public static GifCaptcha ofGifCaptcha(final int width, final int height, final int codeCount) {
return new GifCaptcha(width, height, codeCount);
}
/**
* 创建圆圈干扰的验证码
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param thickness 验证码干扰元素个数
* @param sizeBaseHeight 字体的大小 高度的倍数
* @return {@link GifCaptcha}
*/
public static GifCaptcha ofGifCaptcha(final int width, final int height, final int codeCount,
final int thickness, final float sizeBaseHeight) {
return new GifCaptcha(width, height, codeCount, thickness, sizeBaseHeight);
}
}

View File

@@ -30,7 +30,6 @@ import java.util.concurrent.ThreadLocalRandom;
*
* @author looly
* @since 3.2.3
*
*/
public class CircleCaptcha extends AbstractCaptcha {
private static final long serialVersionUID = -7096627300356535494L;
@@ -38,7 +37,7 @@ public class CircleCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param width 图片宽
* @param height 图片高
*/
public CircleCaptcha(final int width, final int height) {
@@ -48,8 +47,8 @@ public class CircleCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
*/
public CircleCaptcha(final int width, final int height, final int codeCount) {
@@ -59,9 +58,9 @@ public class CircleCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param interfereCount 验证码干扰元素个数
*/
public CircleCaptcha(final int width, final int height, final int codeCount, final int interfereCount) {
@@ -80,12 +79,25 @@ public class CircleCaptcha extends AbstractCaptcha {
super(width, height, generator, interfereCount);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param interfereCount 验证码干扰元素个数
* @param sizeBaseHeight 字体的大小 高度的倍数
*/
public CircleCaptcha(final int width, final int height, final int codeCount, final int interfereCount, final float sizeBaseHeight) {
super(width, height, new RandomGenerator(codeCount), interfereCount, sizeBaseHeight);
}
@Override
public Image createImage(final String code) {
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
try{
try {
// 随机画干扰圈圈
drawInterfere(g);
@@ -99,10 +111,11 @@ public class CircleCaptcha extends AbstractCaptcha {
}
// ----------------------------------------------------------------------------------------------------- Private method start
/**
* 绘制字符串
*
* @param g {@link Graphics2D}画笔
* @param g {@link Graphics2D}画笔
* @param code 验证码
*/
private void drawString(final Graphics2D g, final String code) {

View File

@@ -86,6 +86,19 @@ public class GifCaptcha extends AbstractCaptcha {
super(width, height, generator, interfereCount);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 验证码个数
* @param interfereCount 验证码干扰元素个数
* @param sizeBaseHeight 字体的大小 高度的倍数
*/
public GifCaptcha(final int width, final int height, final int codeCount, final int interfereCount, final float sizeBaseHeight) {
super(width, height, new RandomGenerator(codeCount), interfereCount, sizeBaseHeight);
}
/**
* 设置图像的颜色量化(转换质量 由GIF规范允许的最大256种颜色)。
* 低的值(最小值= 1)产生更好的颜色,但处理显著缓慢。

View File

@@ -36,10 +36,11 @@ public class LineCaptcha extends AbstractCaptcha {
private static final long serialVersionUID = 8691294460763091089L;
// -------------------------------------------------------------------- Constructor start
/**
* 构造默认5位验证码150条干扰线
*
* @param width 图片宽
* @param width 图片宽
* @param height 图片高
*/
public LineCaptcha(final int width, final int height) {
@@ -49,8 +50,8 @@ public class LineCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
@@ -69,6 +70,19 @@ public class LineCaptcha extends AbstractCaptcha {
public LineCaptcha(final int width, final int height, final CodeGenerator generator, final int interfereCount) {
super(width, height, generator, interfereCount);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param interfereCount 验证码干扰元素个数
* @param sizeBaseHeight 字体的大小 高度的倍数
*/
public LineCaptcha(final int width, final int height, final int codeCount, final int interfereCount, final float sizeBaseHeight) {
super(width, height, new RandomGenerator(codeCount), interfereCount, sizeBaseHeight);
}
// -------------------------------------------------------------------- Constructor end
@Override
@@ -77,7 +91,7 @@ public class LineCaptcha extends AbstractCaptcha {
final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
try{
try {
// 干扰线
drawInterfere(g);
@@ -91,10 +105,11 @@ public class LineCaptcha extends AbstractCaptcha {
}
// ----------------------------------------------------------------------------------------------------- Private method start
/**
* 绘制字符串
*
* @param g {@link Graphics}画笔
* @param g {@link Graphics}画笔
* @param code 验证码
*/
private void drawString(final Graphics2D g, final String code) {

View File

@@ -30,7 +30,6 @@ import java.awt.image.BufferedImage;
*
* @author looly
* @since 3.2.3
*
*/
public class ShearCaptcha extends AbstractCaptcha {
private static final long serialVersionUID = -7096627300356535494L;
@@ -38,7 +37,7 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param width 图片宽
* @param height 图片高
*/
public ShearCaptcha(final int width, final int height) {
@@ -48,8 +47,8 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
*/
public ShearCaptcha(final int width, final int height, final int codeCount) {
@@ -59,8 +58,8 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param thickness 干扰线宽度
*/
@@ -80,12 +79,25 @@ public class ShearCaptcha extends AbstractCaptcha {
super(width, height, generator, interfereCount);
}
/**
* 构造
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param interfereCount 验证码干扰元素个数
* @param sizeBaseHeight 字体的大小 高度的倍数
*/
public ShearCaptcha(final int width, final int height, final int codeCount, final int interfereCount, final float sizeBaseHeight) {
super(width, height, new RandomGenerator(codeCount), interfereCount, sizeBaseHeight);
}
@Override
public Image createImage(final String code) {
final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
final Graphics2D g = GraphicsUtil.createGraphics(image, ObjUtil.defaultIfNull(this.background, Color.WHITE));
try{
try {
// 画字符串
drawString(g, code);
// 扭曲
@@ -100,10 +112,11 @@ public class ShearCaptcha extends AbstractCaptcha {
}
// ----------------------------------------------------------------------------------------------------- Private method start
/**
* 绘制字符串
*
* @param g {@link Graphics}画笔
* @param g {@link Graphics}画笔
* @param code 验证码
*/
private void drawString(final Graphics2D g, final String code) {
@@ -117,9 +130,9 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* 扭曲
*
* @param g {@link Graphics}
* @param w1 w1
* @param h1 h1
* @param g {@link Graphics}
* @param w1 w1
* @param h1 h1
* @param color 颜色
*/
private void shear(final Graphics g, final int w1, final int h1, final Color color) {
@@ -130,9 +143,9 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* X坐标扭曲
*
* @param g {@link Graphics}
* @param w1 宽
* @param h1 高
* @param g {@link Graphics}
* @param w1
* @param h1
* @param color 颜色
*/
private void shearX(final Graphics g, final int w1, final int h1, final Color color) {
@@ -155,9 +168,9 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* Y坐标扭曲
*
* @param g {@link Graphics}
* @param w1 宽
* @param h1 高
* @param g {@link Graphics}
* @param w1
* @param h1
* @param color 颜色
*/
private void shearY(final Graphics g, final int w1, final int h1, final Color color) {
@@ -180,13 +193,13 @@ public class ShearCaptcha extends AbstractCaptcha {
/**
* 干扰线
*
* @param g {@link Graphics}
* @param x1 x1
* @param y1 y1
* @param x2 x2
* @param y2 y2
* @param g {@link Graphics}
* @param x1 x1
* @param y1 y1
* @param x2 x2
* @param y2 y2
* @param thickness 粗细
* @param c 颜色
* @param c 颜色
*/
@SuppressWarnings("SameParameterValue")
private void drawInterfere(final Graphics g, final int x1, final int y1, final int x2, final int y2, final int thickness, final Color c) {