use of java.awt.FontFormatException in project HongsCORE by ihongs.
the class Capts method newCapt.
public void newCapt() {
buff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 单位宽度
int fw = width / (1 + codeCount);
// 干扰线框
int ls = (int) ((float) height * maskRatio);
// 最大字号
int fs = (int) ((float) height * fontRatio);
// 最小字号
int fz = (int) ((float) fs * 0.80f);
Random rd = new Random();
StringBuilder sb = new StringBuilder();
Graphics2D gd = buff.createGraphics();
gd.setColor(backColor);
gd.fillRect(0, 0, width, height);
gd.setColor(fontColor);
gd.setStroke(new BasicStroke(ls, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font;
try {
InputStream is;
if (fontFile.startsWith("!")) {
is = Capts.class.getResourceAsStream(fontFile.substring(1));
} else {
is = new FileInputStream(new File(fontFile));
}
font = Font.createFont(Font.TRUETYPE_FONT, is);
} catch (FontFormatException ex) {
throw new HongsExpedient.Common(ex);
} catch (IOException ex) {
throw new HongsExpedient.Common(ex);
}
for (int i = 0; i < codeCount; i++) {
/**
* medRatio 是字号误差系数
* 实际可能偏大, 需要缩减尺寸,
* 修正高度误差, 避免超出画布.
*/
int fn = rd.nextInt(fs - fz) + fz;
int fe = (int) (fn * mendRatio);
int j = rd.nextInt(fontDict.length);
String ss = String.valueOf(fontDict[j]);
sb.append(ss);
gd.setFont(font.deriveFont(Font.CENTER_BASELINE, fn));
int fx = (fw - gd.getFontMetrics().stringWidth(ss)) / 2;
gd.drawString(ss, fw / 2 + fw * i + fx, (height - fn) / 2 + fn - fe);
}
for (int i = 0; i < maskCount; i++) {
int xs = rd.nextInt(width);
int ys = rd.nextInt(height);
int xe = xs + rd.nextInt(fw * 2);
int ye = ys + rd.nextInt(fs / 2);
if (i % 3 == 0) {
gd.setColor(fontColor);
} else {
gd.setColor(backColor);
}
gd.drawLine(xs, ys, xe, ye);
}
// 扭曲画面
shearX(width, height, gd, rd, backColor);
shearY(width, height, gd, rd, backColor);
code = sb.toString();
}
Aggregations