Search in sources :

Example 21 with FontFormatException

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();
}
Also used : BasicStroke(java.awt.BasicStroke) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HongsExpedient(app.hongs.HongsExpedient) IOException(java.io.IOException) FontFormatException(java.awt.FontFormatException) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) FileInputStream(java.io.FileInputStream) Graphics2D(java.awt.Graphics2D) Random(java.util.Random) File(java.io.File)

Aggregations

FontFormatException (java.awt.FontFormatException)21 IOException (java.io.IOException)19 FileNotFoundException (java.io.FileNotFoundException)9 File (java.io.File)8 ClosedChannelException (java.nio.channels.ClosedChannelException)7 Font (java.awt.Font)6 InputStream (java.io.InputStream)5 ByteBuffer (java.nio.ByteBuffer)5 BridgeAssetManager (android.content.res.BridgeAssetManager)4 AssetRepository (com.android.ide.common.rendering.api.AssetRepository)4 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 MappedByteBuffer (java.nio.MappedByteBuffer)3 RandomAccessFile (java.io.RandomAccessFile)2 BufferUnderflowException (java.nio.BufferUnderflowException)2 HongsExpedient (app.hongs.HongsExpedient)1 BasicStroke (java.awt.BasicStroke)1 Graphics2D (java.awt.Graphics2D)1 GraphicsEnvironment (java.awt.GraphicsEnvironment)1 BufferedImage (java.awt.image.BufferedImage)1