Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we convert WMF/EMF (MS metafiles) into standard images like JPG or PNG using any Java API?

I have been stuck in converting WMF/EMF images into standard image format such as JPG or PNG using Java.

What are the best options available?

like image 928
pinkb Avatar asked Jul 22 '11 09:07

pinkb


4 Answers

The Batik library is a toolkit to handle SVG in Java. There are converters included like WMFTranscoder to convert from WMF to SVG and JPEGTranscoder and PNGTranscoder to convert SVG to JPEG/PNG. See Transcoder API Docs for more details.

Another alternative is ImageMagick. It's not Java but has Java bindings: im4java and JMagick.

like image 56
vanje Avatar answered Oct 25 '22 11:10

vanje


wmf is a vector file format. For best results, convert them to .svg or .pdf format. I did it in two stages

1) wmf2fig --auto XXXX.wmf

2) fig2pdf --nogv XXXX.fig

I created a python script for bulk conversion

import subprocess as sbp
a = sbp.Popen("ls *.wmf",shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
filelist = a.communicate()[0].splitlines()

for ele in filelist:
    cmdarg = 'wmf2fig --auto '+ ele.rsplit('.',1)[0]+'.wmf'
    a = sbp.Popen(cmdarg, shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
    out = a.communicate()

for ele in filelist:
    cmdarg = 'fig2pdf --nogv '+ ele.rsplit('.',1)[0]+'.fig'
    a = sbp.Popen(cmdarg, shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
    out = a.communicate()

cmdarg = 'rm *.fig'
a = sbp.Popen(cmdarg, shell=True, stderr=sbp.PIPE, stdout=sbp.PIPE)
out = a.communicate()
like image 30
GoodSpeed Avatar answered Oct 25 '22 09:10

GoodSpeed


If you are deploying your application in a Windows environment, then SWT can handle the conversion for you.

    Image image = new Image(Display.getCurrent(), "test.wmf");                
    ImageLoader loader = new ImageLoader();
    loader.data = new ImageData[] { image.getImageData() };
    try(FileOutputStream stream = new FileOutputStream("test.png"))
    {
        loader.save(stream, SWT.IMAGE_PNG);
    }
    image.dispose();

The purpose of SWT is to provide a Java wrapper around native functionality, and in this case it is calling the windows GDI directly to get it to render the WMF.

like image 1
Jon Iles Avatar answered Oct 25 '22 10:10

Jon Iles


I've created some wrappers around the Batik package (as mentioned by vanje's answer) some time ago, that provides ImageIO support for SVG and WMF/EMF.

With these plugins you should be able to write:

ImageIO.write(ImageIO.read(wmfFile), pngFile, "png");

Source code on GitHub.

While the ImageIO plugins are convenient, im4java and JMagick might still have better format support.

like image 1
Harald K Avatar answered Oct 25 '22 09:10

Harald K