|
|
@ -73,32 +73,50 @@ public class GifCaptcha extends Captcha {
|
|
|
|
private BufferedImage graphicsImage(Color[] fontcolor, char[] strs, int flag) {
|
|
|
|
private BufferedImage graphicsImage(Color[] fontcolor, char[] strs, int flag) {
|
|
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
Graphics2D g2d = (Graphics2D) image.getGraphics();
|
|
|
|
Graphics2D g2d = (Graphics2D) image.getGraphics();
|
|
|
|
g2d.setColor(Color.WHITE); // 填充背景颜色
|
|
|
|
// 填充背景颜色
|
|
|
|
|
|
|
|
g2d.setColor(Color.WHITE);
|
|
|
|
g2d.fillRect(0, 0, width, height);
|
|
|
|
g2d.fillRect(0, 0, width, height);
|
|
|
|
AlphaComposite ac3;
|
|
|
|
// 抗锯齿
|
|
|
|
int h = height - ((height - font.getSize()) >> 1);
|
|
|
|
|
|
|
|
int w = width / len;
|
|
|
|
|
|
|
|
g2d.setFont(font);
|
|
|
|
g2d.setFont(font);
|
|
|
|
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
|
|
|
// 随机画干扰线
|
|
|
|
// 随机画干扰线
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
for (int i = 0; i < num(5, 12); i++) {
|
|
|
|
|
|
|
|
g2d.setStroke(new BasicStroke(1.1f + RANDOM.nextFloat() / 2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
|
|
|
|
|
|
|
g2d.setColor(color(150, 250));
|
|
|
|
int x1 = num(-10, width - 10);
|
|
|
|
int x1 = num(-10, width - 10);
|
|
|
|
int y1 = num(5, height - 5);
|
|
|
|
int y1 = num(5, height - 5);
|
|
|
|
int x2 = num(10, width + 10);
|
|
|
|
int x2 = num(10, width + 10);
|
|
|
|
int y2 = num(2, height - 2);
|
|
|
|
int y2 = num(2, height - 2);
|
|
|
|
g2d.setColor(color(150, 250));
|
|
|
|
|
|
|
|
g2d.setStroke(new BasicStroke(1.3f));
|
|
|
|
|
|
|
|
g2d.drawLine(x1, y1, x2, y2);
|
|
|
|
g2d.drawLine(x1, y1, x2, y2);
|
|
|
|
// 画干扰圆圈
|
|
|
|
// 画干扰圆圈
|
|
|
|
g2d.setColor(color(100, 250));
|
|
|
|
g2d.setColor(color(100, 250));
|
|
|
|
g2d.setStroke(new BasicStroke(1.0f));
|
|
|
|
g2d.drawOval(num(width), num(height), 5 + num(25), 5 + num(25));
|
|
|
|
g2d.drawOval(num(width), num(height), 5 + num(10), 5 + num(10));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 画验证码
|
|
|
|
// 画验证码
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
int hp = (height - font.getSize()) >> 1;
|
|
|
|
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i));
|
|
|
|
int h = height - hp;
|
|
|
|
|
|
|
|
int w = width / strs.length;
|
|
|
|
|
|
|
|
int sp = (w - font.getSize()) / 2;
|
|
|
|
|
|
|
|
for (int i = 0; i < strs.length; i++) {
|
|
|
|
|
|
|
|
AlphaComposite ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(flag, i));
|
|
|
|
g2d.setComposite(ac3);
|
|
|
|
g2d.setComposite(ac3);
|
|
|
|
g2d.setColor(fontcolor[i]);
|
|
|
|
g2d.setColor(fontcolor[i]);
|
|
|
|
g2d.drawString(String.valueOf(strs[i]), (width - (len - i) * w) + (w - font.getSize()) + num(7, 11), h - num(2, 6));
|
|
|
|
// 计算坐标
|
|
|
|
|
|
|
|
int x = i * w + sp + num(-3, 3);
|
|
|
|
|
|
|
|
int y = h + num(-6, 0);
|
|
|
|
|
|
|
|
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.dispose();
|
|
|
|
g2d.dispose();
|
|
|
|
return image;
|
|
|
|
return image;
|
|
|
|