Linux环境下"Font font = new Font("黑体", Font.BOLD, 18)"[BufferedImage~Graphics~drawString]中文乱码

Linux环境下"Font font = new Font("黑体", Font.BOLD, 18)"[BufferedImage~Graphics~drawString]中文乱码,windows下不会乱码,但在linux下nginx和tomcat部署后出现乱码、方框、问号等等,不能认清楚!!!


先说说系统:

Microsoft Windows [版本 10.0.10586]
(c) 2015 Microsoft Corporation。保留所有权利。

----------------------------------------------------------------------

CentOS release 6.5 (Final)
Kernel \r on an \m

----------------------------------------------------tomcat 8.0.28

好了,说怎么解决的吧步骤:

1:查看CentOS下”/usr/share/fonts“目录,查看新的系统则为空目录;无目录可手动创建fonts。

2:打开”C:\Windows\WinSxS\amd64_microsoft-windows-font-truetype-simsunb_31bf3856ad364e35_10.0.10586.0_none_1d80224ec6470747“等目录下,找到自己需要的字体的目录,那个文件夹也是见名知意了;

3:之后把挑选的字体,可以建个文件夹,一次传到Xftp等上传到服务器的”/usr/share/fonts“的目录下;

4:进入"cd /usr/share/fonts/windowsFonts"文件夹,注:windowsFonts为我挑选字体目录:

注:如果没有fonts目录可以手动创建,“windowsFonts”为自定义文件夹名称。

有楷体,黑体,简宋体,微软雅黑等,最后附上!

5:最后在此目录下,执行"#fc-cache -fv",扫描字体目录并生成字体信息的缓存,然后应用程序就可以"立即"使用这些新安装的字体;

6:最终还是要重启web sever,例如:tomcat,字体生效!

注:在ubuntu中需要安装:apt-get install fontconfig,侯执行以下命令!

执行命令 fc-cache -fv

[root@timespacexstar windowsFonts]# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/windowsFonts: caching, new cache contents: 8 fonts, 0 dirs
/usr/share/X11/fonts/Type1: skipping, no such directory
/usr/share/X11/fonts/TTF: skipping, no such directory
/usr/local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

root@iZ234iuy39uZ:/usr/share/fonts/windowsFonts# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 2 dirs
/usr/share/fonts/truetype: caching, new cache contents: 0 fonts, 1 dirs
/usr/share/fonts/truetype/dejavu: caching, new cache contents: 6 fonts, 0 dirs
/usr/share/fonts/windowsFonts: caching, new cache contents: 8 fonts, 0 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/usr/share/fonts: caching, new cache contents: 0 fonts, 2 dirs
/usr/local/share/fonts: caching, new cache contents: 0 fonts, 0 dirs
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/var/cache/fontconfig: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
fc-cache: succeeded

包涵字体:

[root@timespacexstar windowsFonts]# ll
total 77796
-rw-r--r-- 1 root root 16299048 Jan 31 17:43 msyhbd.ttc
-rw-r--r-- 1 root root 23608184 Jan 31 17:43 msyh.ttc
-rw-r--r-- 1 root root  9751720 Jan 31 17:45 simhei.ttf
-rw-r--r-- 1 root root 11785184 Jan 31 17:46 simkai.ttf
-rw-r--r-- 1 root root 18209004 Jan 31 17:47 simsun.ttc

windowsFonts字体

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
优化这段代码能产生不同的单元格宽度:public void myGraphicsGeneration(String name,String cellsValue[][], String path) { // 字体大小 int fontTitileSize = 15; // 横线的行数 int totalrow = cellsValue.length+1; // 竖线的行数 int totalcol = 0; if (cellsValue[0] != null) { totalcol = cellsValue[0].length; } // 图片宽度 int imageWidth = 1024; // 行高 int rowheight = 40; // 图片高度 int imageHeight = totalrow*rowheight+50; // 起始高度 int startHeight = 10; // 起始宽度 int startWidth = 10; // 单元格宽度 int colwidth = (int)((imageWidth-20)/totalcol); BufferedImage image = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0,0, imageWidth, imageHeight); graphics.setColor(new Color(220,240,240)); //画横线 for(int j=0;j<totalrow; j++){ graphics.setColor(Color.black); graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, startWidth + colwidth * totalcol, startHeight + (j + 1) * rowheight); } //画竖线 for(int k=0;k<totalcol+1;k++){ graphics.setColor(Color.black); graphics.drawLine(startWidth + k * colwidth, startHeight + rowheight, startWidth + k * colwidth, startHeight + rowheight * totalrow); } //设置字体 Font font = new Font("微软雅黑",Font.BOLD,fontTitileSize); graphics.setFont(font); //写标题 String title = "【"+ name +"】"; graphics.drawString(title, startWidth, startHeight+rowheight-10); //写入内容 for(int n=0;n<cellsValue.length;n++){ for(int l=0;l<cellsValue[n].length;l++){ if (n == 0) { font = new Font("微软雅黑",Font.BOLD,fontTitileSize); graphics.setFont(font); }else if (n > 0 && l >0) { font = new Font("微软雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.setColor(Color.RED); } else { font = new Font("微软雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.setColor(Color.BLACK); } if(cellsValue[n][l]==null){ cellsValue[n][l] = ""; } graphics.drawString(cellsValue[n][l].toString(), startWidth + colwidth * l + 5, startHeight + rowheight * (n + 2) - 10); } } // 保存图片 createImage(image, path); }
06-06
可以考虑使用自适应单元格宽度的方法来优化这段代码,使得不同单元格的宽度可以根据内容长度来动态调整。具体实现方法可以参考以下步骤: 1. 计算每列的最大字符个数,即获取每列中字符长度的最大值。 2. 根据最大字符个数和字体大小计算单元格宽度,可以采用 FontMetrics 类的 stringWidth 方法来计算字符串的宽度。 3. 在画竖线时,根据计算出的单元格宽度动态调整每列的宽度。 4. 在画内容时,根据每个单元格中字符的长度和计算出的单元格宽度动态调整每个单元格的宽度,以保证内容能够完整显示。 优化后的代码如下: ``` public void myGraphicsGeneration(String name,String cellsValue[][], String path) { // 字体大小 int fontTitileSize = 15; // 横线的行数 int totalrow = cellsValue.length+1; // 竖线的行数 int totalcol = 0; if (cellsValue[0] != null) { totalcol = cellsValue[0].length; } // 图片宽度 int imageWidth = 1024; // 行高 int rowheight = 40; // 图片高度 int imageHeight = totalrow*rowheight+50; // 起始高度 int startHeight = 10; // 起始宽度 int startWidth = 10; // 每列的最大字符个数 int[] maxChars = new int[totalcol]; for (int i = 0; i < totalcol; i++) { for (int j = 0; j < totalrow; j++) { if (cellsValue[j][i] != null && cellsValue[j][i].length() > maxChars[i]) { maxChars[i] = cellsValue[j][i].length(); } } } // 计算单元格宽度 int[] colWidths = new int[totalcol]; Font font = new Font("微软雅黑", Font.PLAIN, fontTitileSize); FontMetrics metrics = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics().getFontMetrics(font); for (int i = 0; i < totalcol; i++) { colWidths[i] = metrics.stringWidth(String.format("%" + maxChars[i] + "s", "")) + 10; } // 重新计算图片宽度 imageWidth = Arrays.stream(colWidths).sum() + 20; BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0,0, imageWidth, imageHeight); graphics.setColor(new Color(220,240,240)); // 画横线 for(int j=0;j<totalrow; j++){ graphics.setColor(Color.black); graphics.drawLine(startWidth, startHeight + (j + 1) * rowheight, startWidth + Arrays.stream(colWidths).sum(), startHeight + (j + 1) * rowheight); } // 画竖线 int x = startWidth; for(int k=0;k<totalcol+1;k++){ graphics.setColor(Color.black); graphics.drawLine(x, startHeight + rowheight, x, startHeight + rowheight * totalrow); x += (k == totalcol) ? 0 : colWidths[k]; } // 设置字体 font = new Font("微软雅黑",Font.BOLD,fontTitileSize); graphics.setFont(font); // 写标题 String title = "【"+ name +"】"; graphics.drawString(title, startWidth, startHeight+rowheight-10); // 写入内容 for(int n=0;n<cellsValue.length;n++){ for(int l=0;l<cellsValue[n].length;l++){ if (n == 0) { font = new Font("微软雅黑",Font.BOLD,fontTitileSize); graphics.setFont(font); } else if (n > 0 && l >0) { font = new Font("微软雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.setColor(Color.RED); } else { font = new Font("微软雅黑",Font.PLAIN,fontTitileSize); graphics.setFont(font); graphics.setColor(Color.BLACK); } if(cellsValue[n][l]==null){ cellsValue[n][l] = ""; } graphics.drawString(cellsValue[n][l].toString(), startWidth + Arrays.stream(colWidths).limit(l).sum() + 5, startHeight + rowheight * (n + 2) - 10); } } // 保存图片 createImage(image, path); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值