为什么蜡染不能使用OutputStream保存.svg文件?

问题描述 投票:0回答:1

我想使用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();
        }
    }
}

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

svg batik
1个回答
0
投票

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


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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.