suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

Getting JAI JPEG2000 to run on Glassfish server without a NPE

1 min read

If you are using JPEG2000 images you will need the JAI jars to decode them. Although these projects appear essentially dead, they are still needed for JPEG2000 decoding in Java. They run fine on your test machine but put them on a Java EE server such as  Glassfish (or Tomcat) and you get a null pointer exception. What a nightmare! Well, here is a solution if you have this issue….

If you try this code,

BufferedImage img = ImageIO.read(new File(“image.jp2”));

this code will throw exception when it is try to execute below command even if you have the JPEG2000 JAI jar on the classpath (you are using the version where we fixed the memory leak, aren’t you?).

I have drilled down and this is the code which is actually failing. JAI has a powerful, expendable lookup mechanism which works brilliantly (so long as it works).

iter = theRegistry.getServiceProviders(ImageInputStreamSpi.class,
true);

What the code above does is it looks the spi(such as jpeg2000 spi) in services folder under meta-inf files and if it is found then which allocate lookup table for the classes to execute.

for example this is another code which look for png supported buffered image classes

iir = ImageIO.getImageReadersByFormatName(“png”).next();

in both cases you need Buffered image in order to process or convert the image into another format.

if you are using JAI JPEG2000 library you can access buffered image using different mechanism

here is the code what iam using to access the buffered image using J2K library

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;
}

i have used fully qualified class names in order to reduce your effort to find and allocate the classes accurately.

I hope this would be helpful for you to use multiple instances of JPEG2000 objects.

This post is part of our “GlassFish Articles Index” series. In these articles, we aim to explore Glassfish in different ways, from useful tutorials, to JaveOne and general.

Update
In the end we gave up with trying to patch ImageIO and wrote our own ImageIO replacement from scratch. Lots of other developers seemed to have the problems so we made this commercially available as the JDeli Image library.

Why use JDeli?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • implements unsupported image formats
  • reduce output file size
  • improve read/write performance
  • supports threading
  • superior image scaling algorithms

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

One Reply to “Getting JAI JPEG2000 to run on Glassfish server without…”

  1. Hello, this is actually a very good blog and an excellent post, more when there is not much information about how work with JPEG2000 format in Java. Here you post the code to decode an image, but at this moment what I need is the inverse process, is to say, convert an image to JPEG2000, please if you or someone else have done something like that, please let me know, I will thank very much.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.