From 83b0eb069e35b5dc9af5c4af16c44415d1255c2c Mon Sep 17 00:00:00 2001 From: hrj Date: Sun, 3 Apr 2022 23:09:45 +0530 Subject: [PATCH] shadow text: dynamic kernel size --- src/main/java/lc/captchas/ShadowTextCaptcha.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/lc/captchas/ShadowTextCaptcha.java b/src/main/java/lc/captchas/ShadowTextCaptcha.java index ebc35bf..16bbdc1 100644 --- a/src/main/java/lc/captchas/ShadowTextCaptcha.java +++ b/src/main/java/lc/captchas/ShadowTextCaptcha.java @@ -37,10 +37,12 @@ public class ShadowTextCaptcha implements ChallengeProvider { return answer.toLowerCase().equals(secret); } - private float[] kernel = { - 1f / 9f, 1f / 9f, 1f / 9f, - 1f / 9f, 1f / 9f, 1f / 9f, - 1f / 9f, 1f / 9f, 1f / 9f + private float[] makeKernel(int size) { + final int N = size * size; + final float weight = 1.0f / (N); + final float[] kernel = new float[N]; + java.util.Arrays.fill(kernel, weight); + return kernel; }; private byte[] shadowText(final int width, final int height, String text) { @@ -54,7 +56,8 @@ public class ShadowTextCaptcha implements ChallengeProvider { graphics2D.setFont(font); graphics2D.drawString(text, 15, 50); graphics2D.dispose(); - ConvolveOp op = new ConvolveOp(new Kernel(3, 3, kernel), ConvolveOp.EDGE_NO_OP, null); + final int kernelSize = (int) Math.ceil((Math.min(width, height) / 50.0)); + ConvolveOp op = new ConvolveOp(new Kernel(kernelSize, kernelSize, makeKernel(kernelSize)), ConvolveOp.EDGE_NO_OP, null); BufferedImage img2 = op.filter(img, null); Graphics2D g2d = img2.createGraphics(); HelperFunctions.setRenderingHints(g2d);