I'm trying to convert a JPEG2000 (.jp2) image to other formats (JPEG or PNG), so I try to use write method of javax.imageio
package. This works fine for other formats (eg. JPEG to PNG), but when it comes to JPEG2000 (or TIFF) it throws an exception. Could anyone tell me what are the possible formats of the input image?
Exception in thread "main" java.lang.IllegalArgumentException: im == null!
at javax.imageio.ImageIO.write(ImageIO.java:1457)
at javax.imageio.ImageIO.write(ImageIO.java:1565)
at decodeincodeimages.AndroidInterface.convertFormat(AndroidInterface.java:199)
at Main_package.Execute.main(Execute.java:69)
Java Result: 1
And this is the method:
public static boolean convertFormat(String inputImagePath,
String outputImagePath, String formatName) throws IOException {
FileInputStream inputStream = new FileInputStream(inputImagePath);
FileOutputStream outputStream = new FileOutputStream(outputImagePath);
// reads input image from file
BufferedImage inputImage = ImageIO.read(inputStream);
// writes to the output image in specified format
boolean result = ImageIO.write(inputImage, formatName, outputStream);
// needs to close the streams
outputStream.close();
inputStream.close();
return result;
}
And I call it like this:
System.out.println(AndroidInterface.convertFormat("g:\\picture.jp2","g:\\conv.gif", "gif"));