最近使用Java将字体写到图片上时遇到了一个问题,写文字的时候英文字母可以正常写上,而中文却消失了,由于我本地代码是在Windows环境下运行的,而上传到服务器是在Linux环境下运行的,在本地的图片和文字可以正常合成,而上传到服务器后就凉了。。最后发现原来是Java找不到相应字体类型导致的,具体代码如下:
/**
* 图片上写文字
* @param backgroundImg 图片
* @param markContent 文字内容
* @param fontX 文字距离图片左侧像素
* @param fontY 文字距离图片上方像素
* @param fontSize 文字大小
* @param markContentColor 文字颜色
* @return
*/
public static BufferedImage mergeImage(BufferedImage backgroundImg,String markContent,int fontX, int fontY, int fontSize,
Color markContentColor){
int width = backgroundImg.getWidth(null) == -1 ? 200 : backgroundImg.getWidth(null);
int height = backgroundImg.getHeight(null) == -1 ? 200 : backgroundImg.getHeight(null);
BufferedImage bufferedImage = new BufferedImage(wi