Using ImageIO to convert from JPEG2000 to PNG

自作多情 提交于 2019-12-13 07:14:57

问题


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"));

回答1:


ImageIO comes with the following formats built in: BMP, GIF, JPEG, PNG, WBMP (source: the API documentation). If you try to read an image in a different format, the ImageIO.read(...) methods will simply return null, which is why you get the IllegalArgumentException: im == null later in your method.

However, ImageIO also uses a plugin mechanism (service provider interface, or SPI), to allow for extra or third-party plugins to be installed.

To be able to read JPEG2000 or TIFF, you need such a plugin.

  • For JPEG2000 the best option is probably JAI. JAI also has a TIFF plugin. JAI was developed by Sun (now Oracle), but unfortunately, there hasn't been updates and bug fixes for years.

  • There's also Java bindings for OpenJPEG that should contain an ImageIO plugin for JPEG2000.

  • For TIFF you can also use my TwelveMonkeys ImageIO TIFF plugin. TwelveMonkeys does not currently have a JPEG2000 plugin, so it might be less useful for you.

(This list is not exhaustive, Google might help you find more :-) )



来源:https://stackoverflow.com/questions/37320814/using-imageio-to-convert-from-jpeg2000-to-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!