dbmdz/imageio-jnr

Unable to convert jp2 image to jpeg image

kaif91 opened this issue · 9 comments

My requirement is to convert jp2 format image to jpeg format image. For that purpose I have following code:

public static void convertJp2(String inputPath,String outputPath) throws IOException {
ImageInputStream iis;
InputStream is =
new FileInputStream(inputPath);

   try {
       iis = ImageIO.createImageInputStream(is);
   } catch (IOException e) {
       e.printStackTrace();
       return;
   }
   ImageReader reader = ImageIO.getImageReaders(iis).next();
   System.out.println("using reader " + (reader.getClass()));
   reader.setInput(iis);
   ImageReadParam readParam = reader.getDefaultReadParam();
   BufferedImage outImg = null;
   try {
       outImg = reader.read(0, readParam);
   } catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
   }
   System.out.println("out " + outImg);
   Iterator<ImageWriter> it = ImageIO.getImageWritersByMIMEType("image/jpeg");
   ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/jpeg").next();
   System.out.println("using writer " + writer);
   ImageWriteParam jpgWriteParam = writer.getDefaultWriteParam();
   jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
   jpgWriteParam.setCompressionQuality(0.95f);
   OutputStream os = new FileOutputStream(outputPath);
   ImageOutputStream ios = ImageIO.createImageOutputStream(os);
   writer.setOutput(ios);
   writer.write(null, new IIOImage(outImg, null, null), jpgWriteParam);
   ios.flush();
   ios.close();
   writer.dispose();

}

But when i try to run this piece of code I am getting the following exception:

Exception in thread "main" java.lang.NullPointerException
at image.conversion.App.convertJp2(App.java:78)
at image.conversion.App.main(App.java:35)

I am using ubuntu 18.04 docker image to test this code and i can confirm that image I am passing as input parameter is in correct format.

Could you guide me what I am missing to use this library or does this library can be used to read jp2 format images?

Have you installed OpenJPEG? See http://www.openjpeg.org/.

As per Wiki to use this library, i have installed libturbojpeg1 and libopenjp2-7 packages in ubuntu.

  • Is iis = ImageIO.createImageInputStream(is); the line where the NPE occurs? I can't tell from your snippet.
  • As per the stack trace, the NPE is in your own code, can you determine which value is causing it?

The following line when executes returns null which is causing NPE
ImageReader reader = ImageIO.getImageReaders(iis).next();

  • Can you check which ImageReader implementations are available in general, i.e. is OpenJpegImageReader on the classpath and is it discovered by ImageIO?
  • Can you confirm that the image you're feeding is indeed a valid JP2 that you can e.g. decompress with opj-decompress?
  • Can you set a breakpoint at OpenJpegImageReaderSpi#canDecodeInput and check at which point it thinks that it can't decode the input image?

I checked my classpath but there is no OpenJpegImageReader class available. However, by running System.out.println(Arrays.toString(ImageIO.getReaderFormatNames())); I am getting following output
[JPG, jpg, bmp, BMP, gif, GIF, WBMP, png, PNG, JPEG, jpeg2000, jpeg, wbmp]

Can you also confirm that whether this library supports J2k image format for decoding purpose?

I haven't tested it yet, can you upload your file somewhere?

In any case, please be more precise in the future when you formulate issues, everything you've said so far in this issue has been about JP2 files 🙄

And it'd be a big help if you put all the information you have on the table in the issue description, next time you open one 🙃

Also, have you tried setting a breakpoint?

seems issue with my machine :)