增加内置字体、贝塞尔曲线、算术验证码、base64输出
This commit is contained in:
@@ -45,6 +45,11 @@ public class SpecCaptcha extends Captcha {
|
||||
return graphicsImage(textChar(), out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toBase64() {
|
||||
return toBase64("data:image/png;base64,");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码图形
|
||||
*
|
||||
@@ -55,45 +60,28 @@ public class SpecCaptcha extends Captcha {
|
||||
private boolean graphicsImage(char[] strs, OutputStream out) {
|
||||
try {
|
||||
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = (Graphics2D) bi.getGraphics();
|
||||
Graphics2D g2d = (Graphics2D) bi.getGraphics();
|
||||
// 填充背景
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(0, 0, width, height);
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(0, 0, width, height);
|
||||
// 抗锯齿
|
||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
g.setStroke(new BasicStroke(1.3f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
||||
// 随机画干扰线
|
||||
// drawLine(3, g);
|
||||
// 随机画干扰圆
|
||||
// drawOval(8, g);
|
||||
drawBesselLine(2, g);
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
// 画干扰圆
|
||||
drawOval(2, g2d);
|
||||
// 画干扰线
|
||||
g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
||||
drawBesselLine(1, g2d);
|
||||
// 画字符串
|
||||
// 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); // 文字的纵坐标
|
||||
g2d.setFont(getFont());
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||
int fW = width / strs.length; // 每一个字符所占的宽度
|
||||
int fSp = (fW - fontSize) / 2; // 字符的左右边距
|
||||
int fSp = (fW - (int) fontMetrics.getStringBounds("W", g2d).getWidth()) / 2; // 字符的左右边距
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
g.setColor(color());
|
||||
// 计算坐标
|
||||
int x = i * fW + fSp;
|
||||
int y = fY/* - num(3, 6)*/;
|
||||
/*if (x < 8) {
|
||||
x = 8;
|
||||
}
|
||||
if (x + fontSize > width) {
|
||||
x = width - fontSize;
|
||||
}*/
|
||||
/*if (y > height) {
|
||||
y = height;
|
||||
}
|
||||
if (y - fontSize < 0) {
|
||||
y = fontSize;
|
||||
}*/
|
||||
g.drawString(String.valueOf(strs[i]), x, y);
|
||||
g2d.setColor(color());
|
||||
int fY = height - ((height - (int) fontMetrics.getStringBounds(String.valueOf(strs[i]), g2d).getHeight()) >> 1); // 文字的纵坐标
|
||||
g2d.drawString(String.valueOf(strs[i]), i * fW + fSp + 3, fY - 3);
|
||||
}
|
||||
g2d.dispose();
|
||||
ImageIO.write(bi, "png", out);
|
||||
out.flush();
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user