0

我有一个 .svg 文件来创建一个地图,该地图将具有与边界相关的侦听器(我猜它称为可伸缩矢量图形的“节点”)。但是,我正在尝试将此文件添加为 JPanel 上的 BufferedImage。(我是关于摇摆组件的新手,所以如果您有更好的想法,请分享)。此外,我使用了Batik库并阅读了一些有关它的信息,并使用此代码示例将这个 .svg 文件作为 BufferedImage

package org.pulpcore.tools.res;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.TranscodingHints;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.util.SVGConstants;
import org.apache.commons.io.FileUtils;

public class SVGRasterizer {

    /**
     Rasterizes an SVG file using Batik.

     @param svgFile The svg file.
     @return The rasterized image.
     @throws IOException if the svg file could not be rasterized.
     */
    public static BufferedImage rasterize(File svgFile) throws IOException {

        final BufferedImage[] imagePointer = new BufferedImage[1];

        // Rendering hints can't be set programatically, so
        // we override defaults with a temporary stylesheet.
        // These defaults emphasize quality and precision, and
        // are more similar to the defaults of other SVG viewers.
        // SVG documents can still override these defaults.
        String css = "svg {" +
                "shape-rendering: geometricPrecision;" +
                "text-rendering:  geometricPrecision;" +
                "color-rendering: optimizeQuality;" +
                "image-rendering: optimizeQuality;" +
                "}";
        File cssFile = File.createTempFile("batik-default-override-", ".css");
        FileUtils.writeStringToFile(cssFile, css);

        TranscodingHints transcoderHints = new TranscodingHints();
        transcoderHints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, Boolean.FALSE);
        transcoderHints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION,
                SVGDOMImplementation.getDOMImplementation());
        transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
                SVGConstants.SVG_NAMESPACE_URI);
        transcoderHints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, "svg");
        transcoderHints.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, cssFile.toURI().toString());

        try {

            TranscoderInput input = new TranscoderInput(new FileInputStream(svgFile));

            ImageTranscoder t = new ImageTranscoder() {

                @Override
                public BufferedImage createImage(int w, int h) {
                    return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
                }

                @Override
                public void writeImage(BufferedImage image, TranscoderOutput out)
                        throws TranscoderException {
                    imagePointer[0] = image;
                }
            };
            t.setTranscodingHints(transcoderHints);
            t.transcode(input, null);
        }
        catch (TranscoderException ex) {
            // Requires Java 6
            //throw new IOException("Couldn't convert " + svgFile, ex);
            ex.printStackTrace();
            throw new IOException("Couldn't convert " + svgFile);
        }
        finally {
            cssFile.delete();
        }

        return imagePointer[0];
    }
}

这是我使用 GUI 组件的课程,也是我尝试使用 BufferedImage 的部分

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class JRisk {

    private JFrame mainMap;

    public JRisk() throws IOException{

        initComponents();
    }

    private void initComponents() throws IOException{

        mainMap = new JFrame();
        mainMap.setResizable(false);

                JPanel mainPanel = new JPanel();

                File riskMap = new File("src\\src\\risk.svg");

                BufferedImage bufImage = SVGRasterizer.rasterize(riskMap);

                mainMap.setIconImage(bufImage);

        mainMap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                mainMap.add(mainPanel);
                mainMap.pack();
                mainMap.setVisible(true);
    }

}

最后,包含 main 方法的测试程序。

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;


public class Test {

    /**
     * @param argsh
     */
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                        try {
                            new JRisk();
                        } catch (IOException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        }
            }
        });
    }
}

那么我的问题是编译器给出了一个错误

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/w3c/dom/svg/SVGDocument

整个错误是

at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at org.apache.batik.dom.svg.SVGDOMImplementation.createDocument(Unknown Source)
    at org.apache.batik.dom.util.SAXDocumentFactory.startElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3063)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:881)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(Unknown Source)
    at org.apache.batik.dom.util.SAXDocumentFactory.createDocument(Unknown Source)
    at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown Source)
    at org.apache.batik.dom.svg.SAXSVGDocumentFactory.createDocument(Unknown Source)
    at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(Unknown Source)
    at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(Unknown Source)
    at src.SVGRasterizer.rasterize(SVGRasterizer.java:74)
    at src.JRisk.initComponents(JRisk.java:31)
    at src.JRisk.<init>(JRisk.java:17)
    at src.Test$1.run(Test.java:24)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.ClassNotFoundException: org.w3c.dom.svg.SVGDocument
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 51 more

如果您能就这个话题提供帮助,即使有有用的建议,我将不胜感激!无论如何谢谢

4

3 回答 3

2

尝试添加xml-apis.jar和/或xml-apis-ext.jar。这对我有用。

于 2015-05-12T13:27:32.543 回答
1

以下帖子解决了您的问题:
蜡染不在类路径中

好像 jar 文件可能不包含在 Batik 下载中,您只需下载所需的 jar 库并将其添加到您的项目中即可。

于 2013-03-07T11:26:25.627 回答
0

在我的情况下,蜡染需要多个 jar 文件才能将 SVG 文件渲染到 BufferedImage。错误:几个 java.lang.NoClassDefFoundError。不确定是否全部需要,究竟缺少什么,或者是否有重叠但值得尝试:

w3c-css
w3c-dom
org.w3c.dom.smil
org.w3c.css.sac     
org.w3c.dom.svg
W3C SVG DOM Bindings For Java (aXSL Build)
svg-dom-java
apatche xmlgraphics-commons
于 2022-01-31T15:15:51.133 回答