JAVA:SVG转换器到PNG get';图案';错误的

JAVA:SVG转换器到PNG get';图案';错误的,java,svg,png,batik,Java,Svg,Png,Batik,我想使用batik1.7将svg文件转换为png格式,我的代码如下: public static void convertSvgToPng(InputStream in, FileOutputStream fos) { try { PNGTranscoder t = new PNGTranscoder(); TranscoderInput input = new TranscoderInput(in); TranscoderOutput o

我想使用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) ....

从错误中听起来,该文件似乎引用了一个不存在的模式(“未命名的_模式”)

您可以尝试在文件中搜索引用(“未命名的_模式”)。当你找到它时,它可能是这样的:

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


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

在@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();
}
}
我有以下罐子:

  • 蜡染全部1.7.jar

  • commons-io-1.3.1.jar

  • commons-logging-1.0.4.jar

  • xml-api-ext.jar


我们需要查看您试图转换的文件。@RobertLongson我已经介绍了我的svg的一些细节。这是我第一次在这里提问,请原谅我不擅长编辑问题。谢谢你的回答,我已经在上面列出了一些细节。通常,只有当一个答案解决了你的问题时,你才单击绿色复选标记。听起来你还不是这样。好吧,显然你的模式确实存在。而且似乎是有效的(我在一个测试文件中尝试过)。所以我建议的解释是不正确的。不幸的是,如果没有某种显示问题的测试文件,那么很难进行诊断。您是否可以将您的文件缩减为可以发布但仍会出现错误的文件?很抱歉,绿色复选框,我下次会注意到。我想发布图像,但网站无法上载svg的图像。每次我发布所有详细信息时,编辑框都会崩溃。如何把文件交给你或在这个问题上发表?再次感谢你。
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();
}
}