mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
防止设置色差过大,导致循环次数过多,增加默认色差方法
This commit is contained in:
@@ -26,17 +26,26 @@ public class CaptchaUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void drawStringColourfulTest() {
|
||||
public void drawStringColourfulColorDistanceTest() {
|
||||
for(int i = 0; i < 10; i++) {
|
||||
AbstractCaptcha lineCaptcha = new TestLineCaptcha(200, 100, 5, 10);
|
||||
lineCaptcha.write("d:/captcha/line"+i+".png");
|
||||
AbstractCaptcha lineCaptcha = new TestLineCaptchaColorDistance(200, 100, 5, 10);
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -62,11 +71,12 @@ public class CaptchaUtilTest {
|
||||
* @param g {@link Graphics}画笔
|
||||
* @param code 验证码
|
||||
*/
|
||||
private void drawString(Graphics2D g, String code) {
|
||||
protected void drawString(Graphics2D g, String code) {
|
||||
// 指定透明度
|
||||
if (null != this.textAlpha) {
|
||||
g.setComposite(this.textAlpha);
|
||||
}
|
||||
// 自定义与背景颜色的色差值,200是基于Color.WHITE较为居中的值
|
||||
GraphicsUtil.drawStringColourful(g, code, this.font, this.width, this.height,Color.WHITE,200);
|
||||
}
|
||||
|
||||
@@ -89,4 +99,22 @@ public class CaptchaUtilTest {
|
||||
}
|
||||
// ----------------------------------------------------------------------------------------------------- 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user