shadow text: dynamic kernel size

This commit is contained in:
hrj 2022-04-03 23:09:45 +05:30
parent 832053f6e9
commit 83b0eb069e

View File

@ -37,10 +37,12 @@ public class ShadowTextCaptcha implements ChallengeProvider {
return answer.toLowerCase().equals(secret); return answer.toLowerCase().equals(secret);
} }
private float[] kernel = { private float[] makeKernel(int size) {
1f / 9f, 1f / 9f, 1f / 9f, final int N = size * size;
1f / 9f, 1f / 9f, 1f / 9f, final float weight = 1.0f / (N);
1f / 9f, 1f / 9f, 1f / 9f 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) { private byte[] shadowText(final int width, final int height, String text) {
@ -54,7 +56,8 @@ public class ShadowTextCaptcha implements ChallengeProvider {
graphics2D.setFont(font); graphics2D.setFont(font);
graphics2D.drawString(text, 15, 50); graphics2D.drawString(text, 15, 50);
graphics2D.dispose(); 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); BufferedImage img2 = op.filter(img, null);
Graphics2D g2d = img2.createGraphics(); Graphics2D g2d = img2.createGraphics();
HelperFunctions.setRenderingHints(g2d); HelperFunctions.setRenderingHints(g2d);