4

I am developing a website that allows the user to upload images. I would like to be able to accept BMP, GIF, JPEG and PNG (and maybe one or two others like TIFF) formats. I am using the Apache Commons FileUpload library to achieve this. In order that all the images end up in the same format, I would like to convert all image formats to JPEG format.

What is the easiest way to do this which will work will all of the above image formats?

I have tried:

  • ImageIO.read(), which gave javax.imageio.IIOException: Unexpected block type 0! for some GIF files and java.awt.color.CMMException: Invalid image format for JPEG files.
  • JAI, which gave a humongous error trace for GIF files.

Edit: Information about the JAI method:

Stack trace:

Error: One factory fails for the operation "gif"

Occurs in: javax.media.jai.ThreadSafeOperationRegistry

followed by literally hundreds of lines of trace.

Code:

public static void convertToJPG(String originalFile, String newFile) throws Exception {
    RenderedImage image = JAI.create("fileload", originalFile);
    JAI.create("filestore", image, newFile, "JPEG");
}

Image:

An image which doesn't work.

Community
  • 1
  • 1
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • JAI can handle decoding of GIF files, can you share some GIF files or show us some of your code and at least the top of the error trace? – Mark Elliot Feb 02 '11 at 02:41

1 Answers1

4

Take a look at ImageMagick, and the convert command, in particular.

seandavi
  • 2,818
  • 4
  • 25
  • 52
  • I've had a look, but it looks rather complicated to get running with Java and I can't really see much documentation or examples. – DanielGibbs Feb 02 '11 at 03:27
  • Just grab a Runtime and then exec the command and arguments. If you wrap this up in a little class to abstract the details, then you can replace the actual converter at a later time. – seandavi Feb 02 '11 at 03:35
  • I am unfamiliar with how to do this, would you be able to provide a short code snippet as an example? – DanielGibbs Feb 02 '11 at 20:06