【Linux】linux下Kaptcha生成的验证码图片文字不显示

开发环境:win10+springboot1.5+Kaptcha2.3.x

问题:验证码图片在windows下正常显示,在Linux下能显示图片,但不显示文字

相关代码:

@Bean
    public DefaultKaptcha captchaProducer() {
        DefaultKaptcha captchaProducer = new DefaultKaptcha();
        Properties properties = new Properties();
        // 图片边框
        properties.setProperty("kaptcha.border", "yes");
        // 边框颜色
        properties.setProperty("kaptcha.border.color", "105,179,90");
        // 字体颜色
        properties.setProperty("kaptcha.textproducer.font.color", "red");
        // 图片宽
        properties.setProperty("kaptcha.image.width", "110");
        // 图片高
        properties.setProperty("kaptcha.image.height", "40");
        // 字体大小
        properties.setProperty("kaptcha.textproducer.font.size", "30");
        // session key
        properties.setProperty("kaptcha.session.key", "code");
        // 验证码长度
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        // 字体
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        captchaProducer.setConfig(config);
        return captchaProducer;
    }

原因及解决方法:由于Linux环境不存在相关字体,需先在Linux下安装字体

安装字体:系统 centos7

1、yum -y install fontconfig

2、进入 /usr/share 可以看到 fontconfig、fonts目录生成

3、在Windows系统上,拷贝需要的字体

4、将所需的字体上传的到Linux环境的 /usr/share/fonts/chinese目录:

如果没有  /usr/share/fonts/chinese 目录,执行如下命令:

mkdir -p /usr/share/fonts/chinese
chmod -R 755 /usr/share/fonts/chinese

上传字体如图:

5、安装ttmkfdir来搜索目录中所有的字体信息,并汇总生成fonts.scale文件

yum -y install ttmkfdir

ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

 

6、修改字体配置文件了

 vi /etc/fonts/fonts.conf

7、刷新内存中的字体缓存,这样就不用reboot重启了

fc-cache

fc-list看一下字体列表。

 

posted @ 2019-01-16 10:14  二月无雨  阅读(2077)  评论(0编辑  收藏  举报