mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 20:58:00 +08:00
防止设置色差过大,导致循环次数过多,增加默认色差方法
This commit is contained in:
parent
4c00f6adb2
commit
a6f0193e19
@ -26,17 +26,26 @@ public class CaptchaUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void drawStringColourfulTest() {
|
public void drawStringColourfulColorDistanceTest() {
|
||||||
for(int i = 0; i < 10; i++) {
|
for(int i = 0; i < 10; i++) {
|
||||||
AbstractCaptcha lineCaptcha = new TestLineCaptcha(200, 100, 5, 10);
|
AbstractCaptcha lineCaptcha = new TestLineCaptchaColorDistance(200, 100, 5, 10);
|
||||||
lineCaptcha.write("d:/captcha/line"+i+".png");
|
lineCaptcha.write("d:/captcha/line1-"+i+".png");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class TestLineCaptcha extends AbstractCaptcha{
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void drawStringColourfulDefaultColorDistanceTest() {
|
||||||
|
for(int i = 0; i < 10; i++) {
|
||||||
|
AbstractCaptcha lineCaptcha = new TestLineCaptchaColorDistanceDefaultColorDistance(200, 100, 5, 10);
|
||||||
|
lineCaptcha.write("d:/captcha/line2-"+i+".png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TestLineCaptchaColorDistance extends AbstractCaptcha{
|
||||||
private static final long serialVersionUID = -558846929114465692L;
|
private static final long serialVersionUID = -558846929114465692L;
|
||||||
|
|
||||||
public TestLineCaptcha(int width, int height, int codeCount, int interfereCount) {
|
public TestLineCaptchaColorDistance(int width, int height, int codeCount, int interfereCount) {
|
||||||
super(width, height, codeCount, interfereCount);
|
super(width, height, codeCount, interfereCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,11 +71,12 @@ public class CaptchaUtilTest {
|
|||||||
* @param g {@link Graphics}画笔
|
* @param g {@link Graphics}画笔
|
||||||
* @param code 验证码
|
* @param code 验证码
|
||||||
*/
|
*/
|
||||||
private void drawString(Graphics2D g, String code) {
|
protected void drawString(Graphics2D g, String code) {
|
||||||
// 指定透明度
|
// 指定透明度
|
||||||
if (null != this.textAlpha) {
|
if (null != this.textAlpha) {
|
||||||
g.setComposite(this.textAlpha);
|
g.setComposite(this.textAlpha);
|
||||||
}
|
}
|
||||||
|
// 自定义与背景颜色的色差值,200是基于Color.WHITE较为居中的值
|
||||||
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height,Color.WHITE,200);
|
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height,Color.WHITE,200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,4 +99,22 @@ public class CaptchaUtilTest {
|
|||||||
}
|
}
|
||||||
// ----------------------------------------------------------------------------------------------------- Private method start
|
// ----------------------------------------------------------------------------------------------------- Private method start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class TestLineCaptchaColorDistanceDefaultColorDistance extends TestLineCaptchaColorDistance {
|
||||||
|
|
||||||
|
|
||||||
|
public TestLineCaptchaColorDistanceDefaultColorDistance(int width, int height, int codeCount, int interfereCount) {
|
||||||
|
super(width, height, codeCount, interfereCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void drawString(Graphics2D g, String code) {
|
||||||
|
// 指定透明度
|
||||||
|
if (null != this.textAlpha) {
|
||||||
|
g.setComposite(this.textAlpha);
|
||||||
|
}
|
||||||
|
// 使用默认色差设置
|
||||||
|
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height,Color.WHITE);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package cn.hutool.core.img;
|
package cn.hutool.core.img;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.RandomUtil;
|
import cn.hutool.core.util.RandomUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
@ -178,6 +179,9 @@ public class ColorUtil {
|
|||||||
* @since 5.8.30
|
* @since 5.8.30
|
||||||
*/
|
*/
|
||||||
public static Color randomColor(Color compareColor,int minDistance) {
|
public static Color randomColor(Color compareColor,int minDistance) {
|
||||||
|
// 注意minDistance太大会增加循环次数,保证至少1/3的概率生成成功
|
||||||
|
Assert.isTrue(minDistance < maxDistance(compareColor) / 3 * 2,
|
||||||
|
"minDistance is too large, there are too few remaining colors!");
|
||||||
Color color = randomColor();
|
Color color = randomColor();
|
||||||
while (computeColorDistance(compareColor,color) < minDistance) {
|
while (computeColorDistance(compareColor,color) < minDistance) {
|
||||||
color = randomColor();
|
color = randomColor();
|
||||||
@ -195,6 +199,20 @@ public class ColorUtil {
|
|||||||
return randomColor(null);
|
return randomColor(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 计算给定点与其他点之间的最大可能距离。
|
||||||
|
*
|
||||||
|
* @param color 指定颜色
|
||||||
|
* @return 其余颜色与color的最大距离
|
||||||
|
* @since 6.0.0-M16
|
||||||
|
*/
|
||||||
|
public static int maxDistance(final Color color) {
|
||||||
|
final int maxX = RGB_COLOR_BOUND - 2 * color.getRed();
|
||||||
|
final int maxY = RGB_COLOR_BOUND - 2 * color.getGreen();
|
||||||
|
final int maxZ = RGB_COLOR_BOUND - 2 * color.getBlue();
|
||||||
|
return (int)Math.sqrt(maxX * maxX + maxY * maxY + maxZ * maxZ);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算两个颜色之间的色差,按三维坐标距离计算
|
* 计算两个颜色之间的色差,按三维坐标距离计算
|
||||||
*
|
*
|
||||||
|
@ -101,6 +101,23 @@ public class GraphicsUtil {
|
|||||||
return drawString(g, str, font, null, width, height, compareColor, minColorDistance);
|
return drawString(g, str, font, null, width, height, compareColor, minColorDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绘制字符串,使用随机颜色,并且与背景颜色保持一定色差,默认抗锯齿
|
||||||
|
*
|
||||||
|
* @param g {@link Graphics}画笔
|
||||||
|
* @param str 字符串
|
||||||
|
* @param font 字体
|
||||||
|
* @param width 字符串总宽度
|
||||||
|
* @param height 字符串背景高度
|
||||||
|
* @param backgroundColor 背景颜色
|
||||||
|
* @return 画笔对象
|
||||||
|
* @since 4.5.10
|
||||||
|
*/
|
||||||
|
public static Graphics drawStringColourful(Graphics g, String str, Font font, int width, int height, Color backgroundColor) {
|
||||||
|
// 默认色差为最大色差的1/2
|
||||||
|
return drawString(g, str, font, null, width, height, backgroundColor, ColorUtil.maxDistance(backgroundColor) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 绘制字符串,默认抗锯齿
|
* 绘制字符串,默认抗锯齿
|
||||||
*
|
*
|
||||||
|
@ -2237,6 +2237,17 @@ public class ImgUtil {
|
|||||||
return ColorUtil.randomColor(compareColor,minDistance);
|
return ColorUtil.randomColor(compareColor,minDistance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机颜色,与指定颜色有一定的区分度
|
||||||
|
*
|
||||||
|
* @param compareColor 比较颜色
|
||||||
|
* @return 与指定颜色有一定的区分度的随机颜色,默认是最大可能的三维距离的一半
|
||||||
|
* @since 5.8.30
|
||||||
|
*/
|
||||||
|
public static Color randomColor(Color compareColor) {
|
||||||
|
return ColorUtil.randomColor(compareColor,ColorUtil.maxDistance(compareColor) / 2);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成随机颜色
|
* 生成随机颜色
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user