• 大小: 86KB
    文件类型: .java
    金币: 2
    下载: 1 次
    发布日期: 2022-09-20
  • 语言: Java
  • 标签:

资源简介

生成验证码时,当设置的字体服务器上没有,生成的验证码会乱码,此为终极解决方案,永不担心字体不存在而导致验证码乱码问题

资源截图

代码片段和文件信息

package com.xysz.common.security;

import java.io.ByteArrayInputStream;
import java.awt.*;

/**
 * ttf字体文件
 *
 * @author Caisin
 */
public class ImgFontByte {
    public Font getFont(int fontHeight) {
        try {
            //创建一个新字体
            Font baseFont = Font.createFont(Font.TRUETYPE_FONT new ByteArrayInputStream(hex2byte(getFontByteStr())));
            return baseFont.deriveFont(Font.PLAIN fontHeight);
        } catch (Exception e) {
            return new Font(“Arial“ Font.PLAIN fontHeight);
        }
    }

    private byte[] hex2byte(String str) {
        if (str == null) {
            return null;
        }
        str = str.trim();
        int len = str.length();
        if (len == 0 || len % 2 == 1) {
            return null;
        }

        byte[] b = new byte[len / 2];
        try {
            for (int i = 0; i < str.length(); i += 2) {
                b[i / 2] = (byte) Integer
                        .decode(“0x“ + str.substring(i i + 2)).intValue();
            }
            return b;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * ttf字体文件的十六进制字符串
     *
     * @return
     */
    private String getFontByteStr() {
        StringBuilder sb = new StringBuilder();
        sb.append(“0001000000100040000400c04f532f327d8175d4000087740000005650434c5461e3d9fb000087“);
        sb.append(“cc00000036636d61709cbc69ab00007a64000005e863767420bb32bf1600000f24000000326“);
        sb.append(“670676d8333c24f00000f1000000014676c7966302665d800000fc40000663c68646d7806bee“);
        sb.append(“f530000806c0000070868656164c6469a91000088040000003668686561068002f40000883c0“);
        sb.append(“0000024686d7478e8bd09b4000077b0000001ac6b65726efffe00650000804c0000001e6c6f6“);
        sb.append(“361001a319600007600000001b06d617870013e049f00008860000000206e616d65a927f70000“);
        sb.append(“00010c00000e04706f737469df66ea0000795c0000010670726570eacfd8a800000f580000006b“);
        sb.append(“0000001f017a000000000000000001de00000000000000000001001c00520000000000000002000“);
        sb.append(“e01de0000000000000003003201ec0000000000000004001c005200000000000000050024021e000“);
        sb.append(“0000000000006001a02420000000000000007005c0052000100000000000000ef025c000100000000“);
        sb.append(“0001000e028500010000000000020007034b0001000000000003001903520001000000000004000e“);
        sb.append(“028500010000000000050012036b0001000000000006000d037d0001000000000007002e02850003“);
        sb.append(“00010409000001de00000003000104090001001c00520003000104090002000e01de0003000104090“);
        sb.append(“003003201ec0003000104090004001c005200030001040900050024021e0003000104090006001a02“);
        sb.append(“420003000104090007005c00520003000104090008002c038a000300010409000900180aa20003000“);
        sb.append(“10409000a01300b16000300010409000b004c0a12000300010409000c00440c46000300010409000d0“);
        sb.append(“76003b6000300010409000e00600a12006

评论

共有 条评论

相关资源