解决方式1:
windows上的字体拷贝到linux上。
解决方式2:
本地java读写字体。
网上时候还有这个问题,不过我还没遇到:
3.个人解决此问题的完整代码:
public InputStream createDistributorSharePosterInputstream(String bgOriginUrl, String wechatNickName, String wechatAvatarUrl, String wechatRecomQrcodeUrl) { try { BufferedImage bimg = getUrlByBufferedImage(bgOriginUrl); // String pathString = FontLoader.class.getResource("/font/msyhbd.ttf").getFile(); // Font font2 = Font.createFont(Font.TRUETYPE_FONT, new File(pathString)); Font font1 = null; GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); // if(localGraphicsEnvironment.getAvailableFontFamilyNames() == null || localGraphicsEnvironment.getAvailableFontFamilyNames().length == 0 ){ Font font = Font.createFont(Font.TRUETYPE_FONT, ImageGenerateUtil.class.getResourceAsStream("/font/msyhbd.ttf")); font1 = font.deriveFont(Font.BOLD,24f); localGraphicsEnvironment.registerFont(font); // }else{ // font1 = new Font(typeface, Font.BOLD, 24); // } // 开启画图 Graphics2D g2d = localGraphicsEnvironment.createGraphics(bimg); // 抗锯齿 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //用户头像 BufferedImage userLogo = getSque(getUrlByBufferedImage(wechatAvatarUrl)); g2d.drawImage(setRadius(userLogo, 3), 336, 1064, 80, 80, null); //用户昵称 // Font font1 = font.deriveFont(Font.BOLD,24f); g2d.setFont(font1); g2d.setColor(Color.WHITE); // 计算文字长度,计算居中的x点坐标 drawString(wechatNickName, bimg, g2d, font1, 1174); //推荐语 // font2 = new Font(typeface, Font.BOLD, 24); g2d.setFont(font1); g2d.setColor(Color.WHITE); drawString("邀请您成为飞碟推荐官", bimg, g2d, font1, 1202); //太阳码 Image sunCodeUrlImage = getUrlByBufferedImage(wechatRecomQrcodeUrl); g2d.drawImage(sunCodeUrlImage, 604, 1258, 100, 100, null); // 释放对象 g2d.dispose(); return upload(bimg); } catch (Exception e) { e.printStackTrace(); log.info("goodsCard:{}", e.getMessage()); } return null; }
完