0

I am facing the problem with loading J2K image files (jp2, jp2000) in my java application. What is strange is the fact, that the application runs correctly (the file is successfully read from disk) when it runs as standalone java application (or in tests).

After deployment on Tomcat application server the ImageIO.read(..) method returns null every time.

Any help is appriciated.

Shimon

Harald K
  • 26,314
  • 7
  • 65
  • 111
shimon001
  • 733
  • 9
  • 24
  • If you are deploying JAI as part of your application (ie, JAR files in `WEB-INF/lib`) have a look at [deploying ImageIO plugins in a web app](https://github.com/haraldk/TwelveMonkeys#deploying-the-plugins-in-a-web-app). It shouldn't be necessary to rewrite your code. – Harald K Mar 30 '15 at 19:06
  • @haraldK - Thanks for your comment. I will try to add the IIOProviderContextListener in my web.xml and edit my answer on this page if it will work. – shimon001 Mar 31 '15 at 07:29

1 Answers1

1

Update: After reviewing comment from @haraldK - the solution is well described on page https://github.com/haraldk/TwelveMonkeys (section Deploying the plugins in a web app).

You need to define listener in your web.xml:

<web-app ...>

...

    <listener>
        <display-name>ImageIO service provider loader/unloader</display-name>
        <listener-class>com.twelvemonkeys.servlet.image.IIOProviderContextListener</listener-class>
    </listener>

...

</web-app>

You also need to add this Maven dependency to your project:

<dependency>
   <groupId>com.twelvemonkeys.servlet</groupId>
   <artifactId>servlet</artifactId>
   <version>3.0.2</version> 
</dependency>

Other, less prefered solution is (this was my first solution mentioned here): After doing some googling I have found this page - https://blog.idrsolutions.com/2013/03/getting-jai-jpeg2000-to-run-on-glassfish-server-without-a-npe/ which discribes problem of resolution of J2K imageio service provider, when using application server such as glassfish or tomcat.

According to this article, the solution is simple. Just use the reader directly:

public BufferedImage getJPEG2000Image(byte[] data){

ImageInputStream iis = null;
BufferedImage image=null;
  try {
    iis = ImageIO.createImageInputStream(new ByteArrayInputStream(data));
    com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderSpi j2kImageReaderSpi = new com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderSpi();
    com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader j2kReader = new com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader(j2kImageReaderSpi);
    j2kReader.setInput(iis, true);
    image = j2kReader.read(0, new com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam());
  }
  catch (Exception e){
      e.printStackTrace();
  }
  return image;
}

This Maven dependency is needed as well:

<dependency>
   <groupId>com.sun.media</groupId>
   <artifactId>jai_imageio</artifactId>
   <version>1.1</version>
</dependency>
shimon001
  • 733
  • 9
  • 24