增加内置字体、贝塞尔曲线、算术验证码、base64输出
This commit is contained in:
@@ -38,10 +38,14 @@ public class ChineseCaptcha extends ChineseCaptchaAbstract {
|
||||
*/
|
||||
@Override
|
||||
public boolean out(OutputStream out) {
|
||||
checkAlpha();
|
||||
return graphicsImage(textChar(), out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toBase64() {
|
||||
return toBase64("data:image/png;base64,");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码图形
|
||||
*
|
||||
@@ -50,54 +54,34 @@ public class ChineseCaptcha extends ChineseCaptchaAbstract {
|
||||
* @return boolean
|
||||
*/
|
||||
private boolean graphicsImage(char[] strs, OutputStream out) {
|
||||
boolean ok;
|
||||
try {
|
||||
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = (Graphics2D) bi.getGraphics();
|
||||
AlphaComposite ac3;
|
||||
int len = strs.length;
|
||||
g.setColor(Color.WHITE);
|
||||
g.fillRect(0, 0, width, height);
|
||||
Graphics2D g2d = (Graphics2D) bi.getGraphics();
|
||||
// 填充背景
|
||||
g2d.setColor(Color.WHITE);
|
||||
g2d.fillRect(0, 0, width, height);
|
||||
// 抗锯齿
|
||||
g.setColor(color());
|
||||
Font font = getFont();
|
||||
g.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;
|
||||
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
|
||||
// 画干扰圆
|
||||
drawOval(3, g2d);
|
||||
// 画干扰线
|
||||
g2d.setStroke(new BasicStroke(1.2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
||||
drawBesselLine(1, g2d);
|
||||
// 画字符串
|
||||
for (int i = 0; i < len; i++) {
|
||||
// 计算坐标
|
||||
int x = i * w + sp + num(-5, 5);
|
||||
int y = h + num(-5, 5);
|
||||
if (x < 5) {
|
||||
x = 5;
|
||||
}
|
||||
if (x + font.getSize() > width) {
|
||||
x = width - font.getSize();
|
||||
}
|
||||
if (y > height) {
|
||||
y = height;
|
||||
}
|
||||
if (y - font.getSize() < 0) {
|
||||
y = font.getSize();
|
||||
}
|
||||
g.setFont(font.deriveFont(num(2) == 0 ? Font.PLAIN : Font.ITALIC));
|
||||
g.drawString(String.valueOf(strs[i]), x, y);
|
||||
g2d.setFont(getFont());
|
||||
FontMetrics fontMetrics = g2d.getFontMetrics();
|
||||
int fW = width / strs.length; // 每一个字符所占的宽度
|
||||
int fSp = (fW - (int) fontMetrics.getStringBounds("王", g2d).getWidth()) / 2; // 字符的左右边距
|
||||
for (int i = 0; i < strs.length; i++) {
|
||||
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);
|
||||
}
|
||||
// 随机画干扰线
|
||||
g.setStroke(new BasicStroke(1.25f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
|
||||
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f); // 指定透明度
|
||||
g.setComposite(ac3);
|
||||
drawLine(2, g.getColor(), g);
|
||||
// 画干扰圆圈
|
||||
drawOval(5, g.getColor(), g);
|
||||
g2d.dispose();
|
||||
ImageIO.write(bi, "png", out);
|
||||
out.flush();
|
||||
ok = true;
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
ok = false;
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
@@ -106,6 +90,6 @@ public class ChineseCaptcha extends ChineseCaptchaAbstract {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user