我想用satik1.7将svg文件转换为png格式,我的代码如下:
public static void convertSvgToPng(InputStream in, FileOutputStream fos) {
try {
PNGTranscoder t = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(in);
TranscoderOutput output = new TranscoderOutput(fos);
t.transcode(input, output);
fos.flush();
fos.close();
} catch (IOException ex) {
Logger.getLogger(CreateFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (TranscoderException ex) {
Logger.getLogger(CreateFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
但是有些图片无法转换,并且错了:
org.apache.batik.bridge.BridgeException: null:-1
The URI "#Unnamed_Pattern"
specified on the element <pattern> is invalidat org.apache.batik.bridge.SVGPatternElementBridge.extractPatternContent(SVGPatternElementBridge.java:260)
at org.apache.batik.bridge.SVGPatternElementBridge.createPaint(SVGPatternElementBridge.java:86)
at org.apache.batik.bridge.PaintServer.convertURIPaint(PaintServer.java:359)
at org.apache.batik.bridge.PaintServer.convertPaint(PaintServer.java:259)
at org.apache.batik.bridge.PaintServer.convertFillPaint(PaintServer.java:228)
at org.apache.batik.bridge.PaintServer.convertFillAndStroke(PaintServer.java:146)
at org.apache.batik.bridge.SVGShapeElementBridge.createShapePainter(SVGShapeElementBridge.java:117)
at org.apache.batik.bridge.SVGDecoratedShapeElementBridge.createFillStrokePainter(SVGDecoratedShapeElementBridge.java:58) ....
当我用TXT打开svg时,有一个mang“pattern”标签。在我用程序删除所有“模式”后,转换成功,但失去一些detall。我认为这不是一个解决这个问题的好方法。还有什么方法可以解决这个问题吗?请帮帮我。
问题文件:
jsfiddle.net/asdzheng/BtFbX/1/
答案 0 :(得分:1)
在@BigBadaboom建议使用batik-rasterizer.jar替换batik-transcoder.jar之后,我已经让它工作了。我的代码如下:
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.batik.apps.rasterizer.DestinationType;
import org.apache.batik.apps.rasterizer.SVGConverter;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
public class DOMRasterizer {
public Document createDocument(InputStream in) {
Document doc = null ;
try {
// Create a new svg document.
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
doc = f.createSVGDocument(null, in);
} catch (IOException ex) {
Logger.getLogger(DOMRasterizer.class.getName()).log(Level.SEVERE, null, ex);
}
return doc;
}
public static void main(String[] args) throws Exception {
DOMRasterizer rasterizer = new DOMRasterizer();
InputStream in = new FileInputStream(new File("your svg path"));
Document svgXmlDoc = rasterizer.createDocument(in);
// Save this SVG into a file (required by SVG -> PNG transformation process)
File svgFile = File.createTempFile("graphic-", ".svg");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(svgXmlDoc);
FileOutputStream fos = new FileOutputStream(svgFile);
try {
transformer.transform(source, new StreamResult(fos));
} finally {
fos.close();
}
// Convert the SVG into PNG
File outputFile =new File("output path");
SVGConverter converter = new SVGConverter();
converter.setDestinationType(DestinationType.PNG);
converter.setSources(new String[]{svgFile.toString()});
converter.setDst(outputFile);
converter.execute();
}
}
我有以下JAR:
蜡染所有-1.7.jar
公地-IO-1.3.1.jar
共享记录-1.0.4.jar
XML的API-ext.jar
答案 1 :(得分:0)
从错误中听起来该文件引用了一个不存在的模式(“Unnamed_Pattern”)。
您可以尝试搜索文件以获取参考(“Unnamed_Pattern”)。当你找到它时,它可能是这样的:
fill="url(#Unnamed_Pattern)"
然后删除该属性,或将其更改为fill="none"
。请注意,文件中可能存在多个不存在的模式引用。
但是要绝对肯定,我们需要看到原始文件,正如罗伯特所说。