增加中文验证码功能

This commit is contained in:
synchronized
2018-07-28 10:26:10 +08:00
parent bd976c7d25
commit c168c09fc9
9 changed files with 141 additions and 66 deletions

View File

@@ -69,23 +69,39 @@ public class ChineseGifCaptcha extends ChineseCaptchaAbstract {
g2d.setColor(Color.WHITE); // 填充背景颜色
g2d.fillRect(0, 0, width, height);
AlphaComposite ac3;
int h = height - ((height - font.getSize()) >> 1);
int w = width / len;
g2d.setFont(font);
g2d.setColor(fontcolor);
// 抗锯齿
g2d.setColor(fontcolor);
g2d.setFont(font);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 画验证码
int hp = (height - font.getSize()) >> 1;
int h = height - hp;
int w = width / strs.length;
int sp = (w - font.getSize()) / 2;
for (int i = 0; i < len; i++) {
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i));
g2d.setComposite(ac3);
int x = i * w + num(2, 6);
g2d.drawString(String.valueOf(strs[i]), x, h - num(2, 6));
// 计算坐标
int x = i * w + sp + num(-Math.abs(sp), Math.abs(sp));
int y = h + num(-3, 3);
if (x < 0) {
x = 0;
}
if (x + font.getSize() > width) {
x = width - font.getSize();
}
if (y > height) {
y = height;
}
if (y - font.getSize() < 0) {
y = font.getSize();
}
g2d.drawString(String.valueOf(strs[i]), x, y);
}
// 随机画干扰线
g2d.setStroke(new BasicStroke(1.2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
for (int i = 0; i < 5; i++) {
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.65f);
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);