1

我想使用 batik1.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” 标签。在我用程序删除所有“模式”后,转换成功,但失去了一些细节。我认为这不是解决这个问题的好方法。它还有其他方法可以解决这个问题吗?请帮我。

问题文件:</p>

jsfiddle.net/asdzheng/BtFbX/1/
4

2 回答 2

2

在@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:

  • batik-all-1.7.jar

  • commons-io-1.3.1.jar

  • commons-logging-1.0.4.jar

  • xml-apis-ext.jar

于 2013-11-14T02:53:01.173 回答
0

从错误中,听起来文件引用了不存在的模式(“Unnamed_Pattern”)。

您可以尝试在文件中搜索参考(“Unnamed_Pattern”)。当你找到它时,它可能是这样的:

fill="url(#Unnamed_Pattern)"

然后删除该属性,或将其更改为fill="none". 请注意,文件中可能有多个引用该不存在的模式。

但要绝对确定,正如罗伯特所说,我们需要查看原始文件。

于 2013-11-06T21:57:36.593 回答