为什么蜡染无法使用OutputStream保存.svg文件?

时间:2019-04-10 17:30:27

标签: svg batik

我想使用Apache的Batik库处理文件系统中现有的.svg文件。我的目标是加载.svg文件,在其上绘制一些形状,然后将最终结果保存在文件系统上。

现在我有两个课。头等舱可以加载文件.svg并在其上绘制形状,但无法保存结果。第二类能够在新画布上绘制形状并将结果保存在文件系统中。

这是头等舱。我尝试使用OutputStream保存最终结果,但是没有用。


import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.svg.GVTTreeBuilderAdapter;
import org.apache.batik.swing.svg.GVTTreeBuilderEvent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.util.concurrent.atomic.AtomicBoolean;

public class RedrawingSVG extends JFrame {

    protected AtomicBoolean shown = new AtomicBoolean(false);

    public static void main(String[] args) throws MalformedURLException, InterruptedException, FileNotFoundException, UnsupportedEncodingException, SVGGraphics2DIOException {
        RedrawingSVG redrawingSVG = new RedrawingSVG();
        redrawingSVG.drawSvg();
    }

    public void drawSvg() throws MalformedURLException, InterruptedException, FileNotFoundException, UnsupportedEncodingException, SVGGraphics2DIOException {
        final JSVGCanvas canvas = new JSVGCanvas();
        canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC); // to update it
        canvas.setURI(new File("/home/ekuntsevich/Downloads/img.svg").toURI().toURL().toString());

        canvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                synchronized (shown) {
                    shown.set(true); // No modifications be fore!!
                    shown.notifyAll();
                }
            }
        });
        getContentPane().add(canvas);

        setSize(800, 400);
        setVisible(true);

        synchronized (shown) { // Strictly required to wait
            while (shown.get() == false) {
                try {
                    shown.wait(0);
                } catch (Exception e) {
                }
            }
        }
        Document doc = canvas.getSVGDocument();

        SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
        svgGenerator.setPaint(Color.red);
        svgGenerator.fill(new Rectangle(100, 100, 1000, 1000));

        Element root = doc.getDocumentElement();
        svgGenerator.getRoot(root);

        Writer out;
        try {
            OutputStream outputStream = new FileOutputStream(new File("img2.svg"));
            out = new OutputStreamWriter(outputStream, "UTF-8");
            svgGenerator.stream(out, true);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SVGGraphics2DIOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

第二堂课。


import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.IOException;

import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;

import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;

public class TestSVGGen {

    public void paint(Graphics2D g2d) {
        g2d.setPaint(Color.red);
        g2d.fill(new Rectangle(10, 10, 100, 100));
    }

    public static void main(String[] args) throws IOException {

        // Get a DOMImplementation.
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

        // Create an instance of org.w3c.dom.Document.
        String svgNS = "http://www.w3.org/2000/svg";
        Document document = domImpl.createDocument(svgNS, "svg", null);

        // Create an instance of the SVG Generator.
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

        // Ask the test to render into the SVG Graphics2D implementation.
        TestSVGGen test = new TestSVGGen();
        test.paint(svgGenerator);

        // Finally, stream out SVG to the standard output using
        // UTF-8 encoding.
        boolean useCSS = true; // we want to use CSS style attributes
        Writer out;
        try {
            OutputStream outputStream = new FileOutputStream(new File("img3.svg"));
            out = new OutputStreamWriter(outputStream, "UTF-8");
            svgGenerator.stream(out, useCSS);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SVGGraphics2DIOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

最后,我要结合这两个类的功能。我想使用以下代码:加载.svg图像->在此图像上绘制一些东西->将结果另存为.svg图像在文件系统上。

2 个答案:

答案 0 :(得分:0)

我解决了我的问题。我查看了stream类中方法SVGGraphics2D的不同签名,发现有一个方法带有适合我的情况的参数。我使用下一个方法stream(Element svgRoot, Writer writer)保存.svg图像。最后,我改为使用svgGenerator.stream(out, true); svgGenerator.stream(root, out);。它对我有用。

答案 1 :(得分:0)

将SVGDOcument保存到文件的最快方法[为后代:)]

public static void saveSvgDocumentToFile(SVGDocument document, File file)
        throws FileNotFoundException, IOException {
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    try (Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) {
        svgGenerator.stream(document.getDocumentElement(), out);
    }
}