增加内置字体,增加贝赛尔曲线干扰线

This commit is contained in:
whvcse@foxmail.com
2019-08-22 17:14:16 +08:00
parent b390fe3f3f
commit 366f54ea80
22 changed files with 194 additions and 44 deletions

View File

@@ -1,5 +1,7 @@
package com.wf.captcha;
import com.wf.captcha.base.Captcha;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
@@ -40,7 +42,6 @@ public class SpecCaptcha extends Captcha {
*/
@Override
public boolean out(OutputStream out) {
checkAlpha();
return graphicsImage(textChar(), out);
}
@@ -52,51 +53,51 @@ public class SpecCaptcha extends Captcha {
* @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();
// 填充背景
g.setColor(Color.WHITE);
g.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);
// drawLine(3, g);
// 随机画干扰圆
drawOval(8, g);
// drawOval(8, g);
drawBesselLine(2, g);
// 画字符串
AlphaComposite ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f);// 指定透明度
g.setComposite(ac3);
int hp = (height - font.getSize()) >> 1;
int h = height - hp;
int w = width / strs.length;
int sp = (w - font.getSize()) / 2;
// 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; // 字符的左右边距
for (int i = 0; i < strs.length; i++) {
g.setColor(new Color(20 + num(110), 20 + num(110), 20 + num(110)));
g.setColor(color());
// 计算坐标
int x = i * w + sp + num(3);
int y = h - num(3, 6);
if (x < 8) {
int x = i * fW + fSp;
int y = fY/* - num(3, 6)*/;
/*if (x < 8) {
x = 8;
}
if (x + font.getSize() > width) {
x = width - font.getSize();
}
if (y > height) {
if (x + fontSize > width) {
x = width - fontSize;
}*/
/*if (y > height) {
y = height;
}
if (y - font.getSize() < 0) {
y = font.getSize();
}
g.setFont(font.deriveFont(num(2) == 0 ? Font.PLAIN : Font.ITALIC));
if (y - fontSize < 0) {
y = fontSize;
}*/
g.drawString(String.valueOf(strs[i]), x, y);
}
ImageIO.write(bi, "png", out);
out.flush();
ok = true;
return true;
} catch (IOException e) {
ok = false;
e.printStackTrace();
} finally {
try {
@@ -105,6 +106,6 @@ public class SpecCaptcha extends Captcha {
e.printStackTrace();
}
}
return ok;
return false;
}
}