5

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
pinkb
  • 543
  • 2
  • 6
  • 15
  • 3
    people producing WMF/EMF images should be shot to death but for what it's worth: I know for sure that the wonderful (and free) *ImageMagick* used to be able to convert WMF files. There also used to be a Java wrapper for ImageMagick (called JMagick if memory serves). So you may want to look in there. That said as I recall it WMF/EMF are just "shells" or "wrappers" around a variety of formats so it may be a moving target and there may very well be a **lot** of different WMF/EMF files and I'm really not sure this is still dealt with correctly. It used to work that said... – SyntaxT3rr0r Jul 22 '11 at 15:50

5 Answers5

2

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.

vanje
  • 10,180
  • 2
  • 31
  • 47
1

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()
icodebuster
  • 8,890
  • 7
  • 62
  • 65
GoodSpeed
  • 37
  • 4
  • While the answer isn't really about Java, I agree it's probably best to keep the converted result as vector. Batik will be able to convert to SVG. – Harald K Jun 27 '13 at 11:48
1

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.

Harald K
  • 26,314
  • 7
  • 65
  • 111
1

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.

Jon Iles
  • 2,519
  • 1
  • 20
  • 30
0

Here is one way.

  1. Get (or make) a Java component that can render the files in question.
  2. Create a BufferedImage the same size as the component needs to display the image.
  3. Get the Graphics object from the BufferedImage.
  4. Call renderComponent.paintComponent(Graphics)
  5. Save the image using one of the ImageIO.write() variants.

See my answer to Swing: Obtain Image of JFrame for steps 2-5. Step 1. is something I'd ask Google about.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Can show me a piece of code..So that I can understand..I could not create an image(java.awt.Image) out of the file in question. Which again raises an issue in creating BufferedImage..Hope you understand. – pinkb Jul 22 '11 at 12:01