Graphics2D 打包在Linux运行时中文乱码,展示成方格

项目打jar包在服务器运行时,生成的图片中文全是方格,因为Linux上没有对应的字体文件,

安装字体文件

1、在本地Windows电脑上C:\Windows\Fonts 下面的字体文件全部复制,也可以单独复制需要的字体,复制后要删除所有的不是ttc、ttf的文件,不然无法执行安装命令

2、在服务器新建文件夹来存放字体文件 例如 /usr/local/share/fonts

cd  /usr/local/share
mkdir fonts

3、然后进入该目录,依次执行以下命令

mkfontscale // 安装字体
mkfontdir 
fc-cache // 更新字体

执行完后可通过 fc-list命令查看当前系统具有的字体,接下来重启jar包就可以了!

执行命令如果报错 

-bash: mkfontscale: command not found  ,

 -bash: fc-cache: command not found

需要安装mkfontscale、fontconfig

yum install mkfontscale 

yum install fontconfig

5、在安装mkfontscale时报错

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
To address this issue please refer to the below wiki article 

https://wiki.centos.org/yum-errors

If above article doesn't help to resolve this issue please use https://bugs.centos.org/.



 One of the configured repositories failed (CentOS-7 - Base),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=base ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable base
        or
            subscription-manager repos --disable=base

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=base.skip_if_unavailable=true

failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try.
https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/repodata/repomd.xml: [Errno 14] HTTPS Error 404 - Not Found

这个时候就不要想起他的了,反正我是搞了一天,最后直接rm了/etc/yum.repos.d 下面的所有文件,也就是按照文档说的换yum源,不过换源前建议还是备份一下比较好

cp -r /etc/yum.repos.d/ /etc/yum.repos.d.bf

3、通过wget进行下载在线yum源

cd /etc/yum.repos.d/

阿里云地址

wget http://mirrors.aliyun.com/repo/Centos-7.repo 

下载完成后会看到 Centos-7.repo 文件,然后再次执行mkfontscale的安装,继续上面的字体安装步骤

Linux系统上使用Graphics2D进行图形绘制时遇到中文乱码的问题,通常是因为编码设置不匹配导致的。Graphics2D默认处理的是字节流,如果处理的字符串不是UTF-8编码,就可能出现乱码现象。解决这个问题的一般步骤包括: 1. **检查字体**:确保你的字体支持中文字符。Linux系统自带了一些支持中文的字体,如华文宋体、SimHei等。 2. **设置字体资源**:在绘图前,需要明确设置当前使用的字体,并确保它包含中文字符。例如,在Java中可以这样做: ```java Graphics2D g = (Graphics2D) yourDrawingCanvas.getGraphics(); g.setFont(new Font("SimHei", Font.PLAIN, 16)); ``` 3. **设置文本编码**:当使用String绘制文本时,确保转换正确的编码,如UTF-8。例如: ```java String chineseText = "你好"; byte[] bytes = chineseText.getBytes(StandardCharsets.UTF_8); // 然后使用bytes绘制到Graphics2D ``` 4. **使用`setRenderingHint`**:有些情况下,可能需要设置渲染提示来控制文字的绘制,比如`FontRenderContext`: ```java g.setRenderingHint(RenderingHints.KEY_FONTPROPERTIES, RenderingHints.VALUE_FONTPROPERTIES_DEFAULT); ``` 如果你已经尝试了以上步骤还是出现问题,可能需要查看具体的代码环境,或者检查应用程序的配置文件,确保整个流程都设置了正确的字符编码。如果涉及到跨平台操作,可能还需要考虑使用专门处理字符集的库,如BMPFont或FreeType。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值