100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java使用 Batik svg代码转换成png图片 JPEGTranscoder/PNGTranscoder 转图片 linux 中文乱码

java使用 Batik svg代码转换成png图片 JPEGTranscoder/PNGTranscoder 转图片 linux 中文乱码

时间:2020-01-12 08:37:41

相关推荐

java使用 Batik svg代码转换成png图片 JPEGTranscoder/PNGTranscoder 转图片 linux 中文乱码

目录

缺少中文字符集

缺少微软雅黑字体

两种情况:缺少中文字符集,缺少对应中文字体(windows一般默认微软雅黑)。

本文链接:/qq_31183727/article/details/102678965

缺少中文字符集

//生成svgDOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;SVGDocument doc = (SVGDocument) domImpl.createDocument(svgNS, "svg", null);// Create a converter for this document.SVGGraphics2D g2d = new SVGGraphics2D(doc);// get the root element (the svg element)Element svgRoot = doc.getDocumentElement();// set the width and height attribute on the svg root elementsvgRoot.setAttributeNS(null, "width", "500");svgRoot.setAttributeNS(null, "height", "600");Element group = doc.createElementNS(svgNS, "g");Element rect = doc.createElementNS(svgNS, "rect");rect.setAttributeNS(null, "x", "0");rect.setAttributeNS(null, "y", "0");rect.setAttributeNS(null, "width", "100%");rect.setAttributeNS(null, "height", "100%");group.appendChild(rect);//插入背景图Element image = doc.createElementNS(svgNS, "image");image.setAttributeNS(null, "xlink:href", ImageToBase64Util.getPrefix(back) + base64str);image.setAttributeNS(null, "x", "0");image.setAttributeNS(null, "y", "0");image.setAttributeNS(null, "width", String.valueOf(styleInnerPO.getWidth()));image.setAttributeNS(null, "height", String.valueOf(styleInnerPO.getHeight()));group.appendChild(image);//插入文本Element textE = doc.createElementNS(svgNS, "text");textE.setAttributeNS(null, "style", "fill:#000000;text-anchor: middle;font-size:50px;font-family:SimHei;");textE.setAttributeNS(null, "x", 100);textE.setAttributeNS(null, "y", 200);textE.setTextContent(text);//text 是传进来的中文字符group.appendChild(textE);ImageTranscoder transcoder = new JPEGTranscoder();TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(baos.toString().getBytes("utf-8")));// Create the transcoder outputByteArrayOutputStream baos_1 = new ByteArrayOutputStream();TranscoderOutput output = new TranscoderOutput(baos_1);//转换transcoder.transcode(input, output);DateFormat format = new SimpleDateFormat("yyyyMMdd");String date = format.format(new Date());String fileName = UUID.randomUUID().toString() + ".jpg";//上传华为云OBSString url = huaweiObsService.pushObject(date, fileName, new ByteArrayInputStream(baos_1.toByteArray()));

这段程序在本地(ubuntu系统)跑起来转换正常,在华为云服务器上转换图片显示正常,但是图片上的文字乱码。

tomcat远程debug了,发现text是中文,转换成svg xml code也是正常的中文。

猜测是jpg的生成代码跟系统的字符集有关。是不是华为云的系统字符集没有中文字体导致这个问题的?

设置ubuntu中文支持

安装中文支持包language-pack-zh-hans:

sudo apt-get install language-pack-zh-hans

修改/etc/environment(在文件的末尾追加):

LANG="zh_CN.UTF-8"

LANGUAGE="zh_CN:zh:en_US:en"

再修改/var/lib/locales/supported.d/local(没有这个文件就新建,同样在末尾追加):

en_US.UTF-8 UTF-8

zh_CN.UTF-8 UTF-8

zh_CN.GBK GBK

zh_CN GB2312

执行命令:

sudo locale-gen

对于中文乱码是空格的情况,安装中文字体解决。

sudo apt-get install fonts-droid-fallback ttf-wqy-zenhei ttf-wqy-microhei fonts-arphic-ukai fonts-arphic-uming

再次请求转换正常了

————————————————

版权声明:本文为CSDN博主「渔蜜之湘」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接: /xu622/article/details/81270130

缺少微软雅黑字体

描述:

现需求是将highcharts图表生成图片,highcharts提供获取svg代码方法,

使用javabatik将svg代码转换成png图片,功能测试都正常,将项目打包到linux服务器后测试,中文乱码了

原因:

经过一番折腾发现是字体的问题,linux系统上没有微软雅黑字体

解决:

在windowsC:\Windows\Fonts目录下找到微软雅黑 .ttc文件上传到linux系统的/usr/share/fonts下

若字体安装后还不正常重启下服务器

Linux中安装字体

查看系统中的字体

fc-list

查看系统中的中文字体

fc-list :lang=zh

将然后将字体文件拷贝到/usr/share/fonts/中

cp aa.ttl /usr/share/fonts

cd /usr/share/fonts

mkfontscale

mkfontdir

————————————————

版权声明:本文为CSDN博主「It-小白」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接: /m0_37924754/article/details/85060093

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。