项目场景:
资产管理:对每个资产进行全生命周期的跟踪,每个资产生成对应的二维码卡片,扫码卡片中的二维码可以查询资产的详细信息。
部署环境: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系统的字体不匹配的原因。
解决方案:
- 查看Linux系统是否安装了字体
// 查看系统所有字体
fc-list
// 查看系统支持的中文字体
fc-list :lang=zh
PS:
若出现提示-bash: fc-list: command not found
,说明系统未安装字体。
- 安装字体
yum install fontconfig
-
字体安装后,在
/usr/share/
路径下会出现fonts
和fontconfig
目录
-
代码中使用的中文字体是
微软雅黑
,从Windows
中将字体拷贝至Linux
的fonts
目录中
-
重启项目,查看卡片
-
bug修复完成