incfbelgiannode / TimeLapseReg

This repository contains the software developed for a plugin in imageJ for aligning biomedical imaging data.
http://neuroinformatics.be/projects/stackreg-plus-improved-multiple-image-alignment-with-imagej/
5 stars 2 forks source link

Unable to convert jp2 image to jpeg #2

Closed kaif91 closed 3 years ago

kaif91 commented 3 years ago

Hi,

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 got the following exception:

Exception in thread "main" java.lang.NullPointerException at com.nedbank.image.conversion.App.convertJp2(App.java:78) at com.nedbank.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(attached) [Uploading Sample0.zip…]()

Could you guide me what I am missing to use this library?