优化验证码效果

This commit is contained in:
synchronized
2018-07-30 13:10:51 +08:00
parent 74a1991352
commit 92af7138a9
9 changed files with 160 additions and 31 deletions

View File

@@ -68,8 +68,8 @@ public class ChineseGifCaptcha extends ChineseCaptchaAbstract {
Graphics2D g2d = (Graphics2D) image.getGraphics();
g2d.setColor(Color.WHITE); // 填充背景颜色
g2d.fillRect(0, 0, width, height);
AlphaComposite ac3;
// 抗锯齿
AlphaComposite ac3;
g2d.setColor(fontcolor);
g2d.setFont(font);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
@@ -82,7 +82,7 @@ public class ChineseGifCaptcha extends ChineseCaptchaAbstract {
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i));
g2d.setComposite(ac3);
// 计算坐标
int x = i * w + sp + num(-Math.abs(sp), Math.abs(sp));
int x = i * w + sp + num(-3, 3);
int y = h + num(-3, 3);
if (x < 0) {
x = 0;
@@ -99,17 +99,19 @@ public class ChineseGifCaptcha extends ChineseCaptchaAbstract {
g2d.drawString(String.valueOf(strs[i]), x, y);
}
// 随机画干扰线
g2d.setStroke(new BasicStroke(1.2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
g2d.setStroke(new BasicStroke(1.25f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.45f);
g2d.setComposite(ac3);
for (int i = 0; i < 4; i++) {
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.55f);
g2d.setComposite(ac3);
int x1 = num(-10, width - 10);
int y1 = num(5, height - 5);
int x2 = num(10, width + 10);
int y2 = num(2, height - 2);
g2d.drawLine(x1, y1, x2, y2);
// 画干扰圆圈
g2d.drawOval(num(width), num(height), 5 + num(10), 5 + num(10));
}
// 画干扰圆圈
for (int i = 0; i < 8; i++) {
g2d.drawOval(num(width), num(height), 5 + num(50), 5 + num(50));
}
g2d.dispose();
return image;
@@ -124,7 +126,8 @@ public class ChineseGifCaptcha extends ChineseCaptchaAbstract {
*/
private float getAlpha(int i, int j) {
int num = i + j;
float r = (float) 1 / len, s = (len + 1) * r;
return num > len ? (num * r - s) : num * r;
float r = (float) 1 / (len - 1);
float s = len * r;
return num >= len ? (num * r - s) : num * r;
}
}