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

 

目录

缺少中文字符集

缺少微软雅黑字体


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

本文链接:https://blog.csdn.net/qq_31183727/article/details/102678965


缺少中文字符集

//生成svg
DOMImplementation 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 element
svgRoot.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 output
ByteArrayOutputStream 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";
//上传华为云OBS
String 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 版权协议,转载请附上原文出处链接及本声明。
原文链接: https://blog.csdn.net/xu622/article/details/81270130

缺少微软雅黑字体

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

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

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

解决:
windows C:\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 版权协议,转载请附上原文出处链接及本声明。
原文链接: https://blog.csdn.net/m0_37924754/article/details/85060093

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值