2018-02-11 14:53:59 +08:00
|
|
|
package com.wf.captcha;
|
|
|
|
|
2019-08-22 17:14:16 +08:00
|
|
|
import com.wf.captcha.base.Captcha;
|
|
|
|
|
2018-07-27 15:11:34 +08:00
|
|
|
import java.awt.*;
|
2018-02-11 14:53:59 +08:00
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
2018-07-27 12:02:33 +08:00
|
|
|
|
2018-02-11 14:53:59 +08:00
|
|
|
/**
|
2018-07-27 12:02:33 +08:00
|
|
|
* png格式验证码
|
|
|
|
* Created by 王帆 on 2018-07-27 上午 10:08.
|
2018-02-11 14:53:59 +08:00
|
|
|
*/
|
2018-07-27 12:02:33 +08:00
|
|
|
public class SpecCaptcha extends Captcha {
|
|
|
|
|
|
|
|
public SpecCaptcha() {
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
2018-07-27 12:02:33 +08:00
|
|
|
|
|
|
|
public SpecCaptcha(int width, int height) {
|
2018-07-27 16:30:33 +08:00
|
|
|
this();
|
2018-07-27 12:02:33 +08:00
|
|
|
setWidth(width);
|
|
|
|
setHeight(height);
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
2018-07-27 12:02:33 +08:00
|
|
|
|
|
|
|
public SpecCaptcha(int width, int height, int len) {
|
|
|
|
this(width, height);
|
|
|
|
setLen(len);
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
2018-07-27 12:02:33 +08:00
|
|
|
|
|
|
|
public SpecCaptcha(int width, int height, int len, Font font) {
|
|
|
|
this(width, height, len);
|
|
|
|
setFont(font);
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
2018-07-27 12:02:33 +08:00
|
|
|
|
2018-02-11 14:53:59 +08:00
|
|
|
/**
|
|
|
|
* 生成验证码
|
2018-07-27 12:02:33 +08:00
|
|
|
*
|
|
|
|
* @param out 输出流
|
|
|
|
* @return 是否成功
|
2018-02-11 14:53:59 +08:00
|
|
|
*/
|
|
|
|
@Override
|
2018-07-27 12:02:33 +08:00
|
|
|
public boolean out(OutputStream out) {
|
|
|
|
return graphicsImage(textChar(), out);
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-27 12:02:33 +08:00
|
|
|
* 生成验证码图形
|
|
|
|
*
|
|
|
|
* @param strs 验证码
|
|
|
|
* @param out 输出流
|
|
|
|
* @return boolean
|
2018-02-11 14:53:59 +08:00
|
|
|
*/
|
2018-07-27 12:02:33 +08:00
|
|
|
private boolean graphicsImage(char[] strs, OutputStream out) {
|
|
|
|
try {
|
|
|
|
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
|
|
|
Graphics2D g = (Graphics2D) bi.getGraphics();
|
2019-08-22 17:14:16 +08:00
|
|
|
// 填充背景
|
2018-02-11 14:53:59 +08:00
|
|
|
g.setColor(Color.WHITE);
|
2018-07-27 12:02:33 +08:00
|
|
|
g.fillRect(0, 0, width, height);
|
2018-07-28 10:26:10 +08:00
|
|
|
// 抗锯齿
|
|
|
|
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
2018-08-09 15:50:40 +08:00
|
|
|
g.setStroke(new BasicStroke(1.3f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
2018-07-27 15:11:34 +08:00
|
|
|
// 随机画干扰线
|
2019-08-22 17:14:16 +08:00
|
|
|
// drawLine(3, g);
|
2018-08-09 15:50:40 +08:00
|
|
|
// 随机画干扰圆
|
2019-08-22 17:14:16 +08:00
|
|
|
// drawOval(8, g);
|
|
|
|
drawBesselLine(2, g);
|
2018-07-27 12:02:33 +08:00
|
|
|
// 画字符串
|
2019-08-22 17:14:16 +08:00
|
|
|
// AlphaComposite ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f);// 指定透明度
|
|
|
|
// g.setComposite(ac3);
|
|
|
|
g.setFont(getFont());
|
|
|
|
int fontSize = getFont().getSize();
|
|
|
|
int fY = height - ((height - fontSize) >> 1); // 文字的纵坐标
|
|
|
|
int fW = width / strs.length; // 每一个字符所占的宽度
|
|
|
|
int fSp = (fW - fontSize) / 2; // 字符的左右边距
|
2018-07-28 10:26:10 +08:00
|
|
|
for (int i = 0; i < strs.length; i++) {
|
2019-08-22 17:14:16 +08:00
|
|
|
g.setColor(color());
|
2018-07-28 10:26:10 +08:00
|
|
|
// 计算坐标
|
2019-08-22 17:14:16 +08:00
|
|
|
int x = i * fW + fSp;
|
|
|
|
int y = fY/* - num(3, 6)*/;
|
|
|
|
/*if (x < 8) {
|
2018-08-09 15:50:40 +08:00
|
|
|
x = 8;
|
2018-07-28 10:26:10 +08:00
|
|
|
}
|
2019-08-22 17:14:16 +08:00
|
|
|
if (x + fontSize > width) {
|
|
|
|
x = width - fontSize;
|
|
|
|
}*/
|
|
|
|
/*if (y > height) {
|
2018-07-28 10:26:10 +08:00
|
|
|
y = height;
|
|
|
|
}
|
2019-08-22 17:14:16 +08:00
|
|
|
if (y - fontSize < 0) {
|
|
|
|
y = fontSize;
|
|
|
|
}*/
|
2018-07-28 10:26:10 +08:00
|
|
|
g.drawString(String.valueOf(strs[i]), x, y);
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
|
|
|
ImageIO.write(bi, "png", out);
|
|
|
|
out.flush();
|
2019-08-22 17:14:16 +08:00
|
|
|
return true;
|
2018-07-27 12:02:33 +08:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
out.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
2019-08-22 17:14:16 +08:00
|
|
|
return false;
|
2018-02-11 14:53:59 +08:00
|
|
|
}
|
|
|
|
}
|