记一次linux中文乱码

/**
	 * 二维码添加文字
	 * @param args
	 * @throws WriterException
	 */
	public static BufferedImage addBottomText(BufferedImage image,String note){
		BufferedImage outImage = new BufferedImage(430, 480, BufferedImage.TYPE_INT_BGR);
		int width = image.getWidth();
		int height = image.getHeight();
		if (!MyStringUtil.isNullOrEmpty(note)) {
			// 新的图片,把带logo的二维码下面加上文字
			Graphics2D outg = outImage.createGraphics();
			// 画二维码到新的面板
			outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
			// 画文字到新的面板
			outg.setColor(Color.white);
			outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
			int strWidth = outg.getFontMetrics().stringWidth(note);
			if (strWidth > 399) {
				// //长度过长就截取前面部分
				// 长度过长就换行
				String note1 = note.substring(0, note.length() / 2);
				String note2 = note.substring(note.length() / 2, note.length());
				int strWidth1 = outg.getFontMetrics().stringWidth(note1);
				int strWidth2 = outg.getFontMetrics().stringWidth(note2);
				outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
				BufferedImage outImage2 = new BufferedImage(400, 480, BufferedImage.TYPE_INT_BGR);
				Graphics2D outg2 = outImage2.createGraphics();
				outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
				outg2.setColor(Color.BLACK);
				outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
				outg2.drawString(note2, 200 - strWidth2 / 2,
						outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
				outg2.dispose();
				outImage2.flush();
				outImage = outImage2;
			} else {
				outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
			}
			outg.dispose();
		}
		return outImage;
	}

这是一个二维码底部添加中文的方法,本地运行没问题,到linux上就乱码了

 

解决方案:

1.找到本地windows下对应的中文字体,比如宋体,楷体

C:\Windows\Fonts

2.把中文字体文件复制到linux系统/usr/share/fonts下,随便新建一个文件夹来装windows中的字体文件

3.先执行

yum install mkfontscale

yum install fontconfig

4.在新建字体文件夹下执行命令mkfontscale && mkfontdir && fc-cache -fv

5.刷新字体缓存

fc-cache -fv

6.查看字体列表

fc-list

 

7.重启你的java程序

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值