Java生成图片中文乱码bug

文章描述了一个在CentOS7.5环境下资产管理项目中,二维码卡片生成时中文显示乱码的问题。通过分析,发现问题可能源于Linux系统的字体不匹配。解决方案包括查看系统字体、安装fontconfig并拷贝Windows中的微软雅黑字体到Linux系统中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目场景:

资产管理:对每个资产进行全生命周期的跟踪,每个资产生成对应的二维码卡片,扫码卡片中的二维码可以查询资产的详细信息。
部署环境:CentOS 7.5


问题描述

卡片中的中文乱码,英文正常显示。
资产管理卡片
通过ImageUtils生成图片的代码如下:

private void pressAction(Map<String, Object> data, String qrcodeurl, String cardurl, String savePath) throws Exception {
        // 生成设备卡片图片
        File file = new File(savePath + "model/cardModel.png");
        File toFile = new File(cardurl);
        try {
            ImageUtils.copy(file, toFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 生成二维码
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(data.get("hospitalmarker").toString(), BarcodeFormat.QR_CODE, 80, 80, hints);
        // 删除白边
        bitMatrix = ImageUtils.deleteWhite(bitMatrix);
        File outputFile = new File(qrcodeurl);
        MatrixToImageWriter.writeToFile(bitMatrix, "png", outputFile);
        // 图片缩放
        ImageUtils.resize(qrcodeurl, 90, 90, true);

        // 添加图片水印
        ImageUtils.pressImage(cardurl, qrcodeurl, 375, 250, 1f);
        // 添加文字水印
        ImageUtils.pressText(cardurl, "设备名称:", "微软雅黑", Font.PLAIN, 22, Color.BLACK, 10, 125, 1f);
        ImageUtils.pressText(cardurl, null == data.get("assetname") ? "" : data.get("assetname").toString(), "微软雅黑", Font.PLAIN, 22, Color.BLACK, 120, 125, 1f);
        ImageUtils.pressText(cardurl, "管理部门:", "微软雅黑", Font.PLAIN, 22, Color.BLACK, 260, 180, 1f);
        ImageUtils.pressText(cardurl, null == data.get("mangedeptname") ? "" : data.get("mangedeptname").toString(), "微软雅黑", Font.PLAIN, 22, Color.BLACK, 370, 180, 1f);
        ImageUtils.pressText(cardurl, "启用日期:", "微软雅黑", Font.PLAIN, 22, Color.BLACK, 10, 180, 1f);
        ImageUtils.pressText(cardurl, null == data.get("startusingtime") ? "" : data.get("startusingtime").toString(), "微软雅黑", Font.PLAIN, 22, Color.BLACK, 120, 180, 1f);
        ImageUtils.pressText(cardurl, "资产编号:", "微软雅黑", Font.PLAIN, 22, Color.BLACK, 10, 300, 1f);
        ImageUtils.pressText(cardurl, null == data.get("othercode") ? "" : data.get("othercode").toString(), "微软雅黑", Font.PLAIN, 22, Color.BLACK, 120, 300, 1f);
        ImageUtils.pressText(cardurl, "管理编号:", "微软雅黑", Font.PLAIN, 22, Color.BLACK, 10, 240, 1f);
        ImageUtils.pressText(cardurl, null == data.get("assetcode") ? "" : data.get("assetcode").toString(), "微软雅黑", Font.PLAIN, 22, Color.BLACK, 120, 240, 1f);

    }

原因分析:

项目部署在Windows Server上不会出现中文乱码的情况,考虑是Linux系统的字体不匹配的原因。


解决方案:

  1. 查看Linux系统是否安装了字体
 // 查看系统所有字体
 fc-list 
 // 查看系统支持的中文字体
 fc-list :lang=zh

PS:若出现提示-bash: fc-list: command not found,说明系统未安装字体。

  1. 安装字体
yum  install fontconfig
  1. 字体安装后,在/usr/share/路径下会出现fontsfontconfig目录
    字体安装后生成的两个文件夹

  2. 代码中使用的中文字体是微软雅黑,从Windows中将字体拷贝至Linuxfonts目录中
    windows中的字体库
    Linux中的字体

  3. 重启项目,查看卡片
    正常显示的卡片

  4. bug修复完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值