Convert EMF+ (not WMF / EMF) file to PNG in java

2019-05-23 16:06发布

Problem:

I have a an image of Enhanced Metafile Fromat Plus Extension EMF+. I need to convert this file to to a PNG image file with java.

Attempts to resolve problem in java:

  1. Apache Batik does not support EMF+ format (Batik supports WMF only format).

  2. FreeHEP does not support EMF+ too. It can convert EMF to SVG and then SVG to PNG. I hoped it can help:

    EMF2SVG.main( new String[] {inputMetaFileName,ontputSvgFileName});
    

    this statement must convert EMF to SVG, but gives an exception:

    java.lang.NullPointerException at org.freehep.graphicsio.exportchooser.AbstractExportFileType.getGraphics(AbstractExportFileType.java:261) at org.freehep.graphicsio.exportchooser.AbstractExportFileType.exportToFile(AbstractExportFileType.java:248) at org.freehep.graphicsio.exportchooser.AbstractExportFileType.exportToFile(AbstractExportFileType.java:282) at org.freehep.graphicsio.emf.EMFConverter.export(EMFConverter.java:81) at org.freehep.graphicsio.emf.EMF2SVG.main(EMF2SVG.java:27)

  3. I used code from here:

    try {
        // read the EMF file
        EMFRenderer emfRenderer = new EMFRenderer( new EMFInputStream(
            new ByteArrayInputStream(emfBytes)));
    
        EMFPanel emfPanel = new EMFPanel();
        emfPanel.setRenderer(emfRenderer);
    
        // prepare Graphics2D
        FileOutputStream svg = new FileOutputStream("svg.svg");
        SVGGraphics2D graphics2D = new SVGGraphics2D (svg, emfPanel);
    
        // export to SVG
        graphics2D.startExport();
        emfPanel.print(graphics2D);
        graphics2D.endExport();
        svg.flush();
        svg.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    This code creates SVG file, but it file has no image.

0条回答
登录 后发表回答