0

I've got a Spring MVC webapp written in Java which you can upload images too. These images need to be manipulated (resized, cropped, etc) and I'm using the Scalr library for this, which requires the images to be read from a BufferedImage.

In Java you can convert from an InputStream (which is how images come in) to a BufferedImage very easily:

final BufferedImage img = ImageIO.read(in);

However, ImageIO is really pernickety about image formats and throws all sorts of exceptions if something isn't right about the image. Users could be uploading almost any quality of image to the web app, so this isn't acceptable.

So I'm looking for an alternative. I've done some googling and one suggestion was Java Advanced Imaging from Oracle. The only problem is that it appears to rely on the Oracle Java implementation and I'm not using that.

Is there another library I can use?

Paul Grenyer
  • 1,713
  • 3
  • 30
  • 51
  • 3
    Curiosity, why would the quality of the image affect the ability for `ImageIO` to read it...The only, obvious, errors are, incompatible format or bad format (missing resource). I'd imagine just about any library would have issues those errors as well...never had an issue with `ImageIO` due to "poor quality" – MadProgrammer Sep 09 '14 at 06:09
  • It's possible that I'm not using the term 'quality' correctly. The particular exception I'm getting at the moment is 'javax.imageio.IIOException: Incompatible color conversion'. However the image displays fine in the browser. – Paul Grenyer Sep 09 '14 at 06:13
  • What type of image is it? – MadProgrammer Sep 09 '14 at 06:14
  • It's a jpg: https://dl.dropboxusercontent.com/u/90562408/Facebook-banner-v01-small.jpg – Paul Grenyer Sep 09 '14 at 06:18
  • I get `Unsupported Image Type` when I try and load it from the disk. The image is using a CMYK color model. If I convert it to RGB, it loads just fine. Not really helpful you right now – MadProgrammer Sep 09 '14 at 06:34
  • 3
    Have a look at [this previous answer](http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-using-imageio-readfile-file) for some ideas...and [this example](http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/) – MadProgrammer Sep 09 '14 at 06:35

1 Answers1

3

I'd say, don't change your code just yet!

The good thing about the ImageIO library is that it is completely plugin-based. If it doesn't read the formats you need right out of the box, you can add support very easily.

I've written a couple of plugins for the TwelveMonkeys ImageIO project (in particular, it includes a JPEGImageReader that supports CMYK color space).

Werner Randelshofer's CMYKJPEGImageReader linked by @MadProgrammer is another plugin you could try out.

JAI, as you have mentioned, also has plugins that allows you to read CMYK JPEGs (I think), however many of the plugins requires native libraries and extra installation that might be a hassle in a web app context. The project also hasn't been updated for years.

I'm sure there are commercial plugins available too, but I haven't researched these.

If you really want to change library, have a look at Apache Commons Imaging. It has a very nice API, and supports many formats. Images are read into BufferedImages. Their JPEG support, however, isn't very mature at the moment, so it most likely will not read your image.

JMagick and im4java both use ImageMagick which has very good support for many formats. Requires native IM installation. The APIs are file based however, which means you often have to write your streams to a temp file before processing. Also doesn't directly support BufferedImages, but you can convert if you like, and in many cases you can use IMs built in image manipulation instead of Java.

Again, there might be complete commercial library alternatives, either in Java or based on native code.

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