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.
suda
Latest posts by suda (see all)
- Draw Components in XFA - June 4, 2013
- XFA Forms Vs Swing Forms - May 14, 2013
- XFA components structure and finding x,y coordinates - April 24, 2013
- How to access external HTML resources in the GlassFish server - March 28, 2013
- Getting JAI JPEG2000 to run on Glassfish server without a NPE - March 13, 2013
