项目需求上传多个SVG图片将其中的汉字翻译后再下载成zip包,自己技术很菜于是在网上找了很久大佬们的方法集中起来,自己记录一下,方便以后学习!
首先上POM依赖
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-codec</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>batik</groupId>
<artifactId>batik-transcoder</artifactId>
<version>1.6-1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jxpath</artifactId>
<version>1.4.0</version>
</dependency>
SVG转换代码:
/**
* 解析svg
* @param file 当前文件
* @param vehicleCode 车型号
* @throws Exception
*/
public File analysisSVG(File file,String vehicleCode) throws Exception {
//当前登录人
SysUserEntity su = sysUserService.getById(getUserId());
List<OnlineTranslationEntity> list = new ArrayList<>();
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
Document doc = f.createDocument(file.toURI().toString());
//转换成XML格式
/*Element element1 = doc.getDocumentElement();
String elementStr1 = convertElemToSVG(element1);
System.out.println("XML内容是:"+elementStr1);*/
//存放翻译后的集合
List<String> yingyu = new ArrayList<>();
//拿到所有text标签的内容
NodeList text = doc.getDocumentElement().getElementsByTagName("text");
//遍历text里面的所有内容
for (int i = 0; i < text.getLength(); i++) {
//拿到当前text标签里面的内容
String textContent = text.item(i).getTextContent();
//进行汉字判断
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher isNum = p.matcher(textContent);
if (isNum.find()) {
ElectricApplianceEntity chinese = dataService.getOne(new QueryWrapper<ElectricApplianceEntity>().eq("parts_chinese", textContent));
if(chinese == null){
OnlineTranslationEntity onlineTranslationEntity = new OnlineTranslationEntity();
onlineTranslationEntity.setCreateUser(su.getUsername());
onlineTranslationEntity.setCreateDate(new Date());
onlineTranslationEntity.setTranslationWords(textContent);
onlineTranslationEntity.setVehicleNumber(vehicleCode);
list.add(onlineTranslationEntity);
ElectricApplianceEntity chinese1 = new ElectricApplianceEntity();
chinese1.setPartsEnglish("暂无该词汇");
chinese = chinese1;
}
text.item(i).setTextContent(chinese.getPartsEnglish());
yingyu.add(chinese.getPartsEnglish());
}
}
//去重
list = list.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(o -> o.getTranslationWords()))), ArrayList::new)
);
this.saveBatch(list);
//输出成SVG文件
Transformer t = TransformerFactory.newInstance().newTransformer();
FileOutputStream stream = new FileOutputStream(file);
t.transform(new DOMSource(doc), new StreamResult(stream));
//关闭流
stream.close();
return file;
}
/**
* 将element转换成XML字符串
* @param element
* @return
*/
public String convertElemToSVG(Element element) {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transFactory.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
try {
transformer.transform(new DOMSource(element), new StreamResult(buffer));
} catch (TransformerException e) {
e.printStackTrace();
}
String elementStr = buffer.toString();
return elementStr;
}