使用 ImageIO 将 JPEG2000 转换为 PNG [英] Using ImageIO to convert from JPEG2000 to PNG

查看:61
本文介绍了使用 ImageIO 将 JPEG2000 转换为 PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JPEG2000 (.jp2) 图像转换为其他格式(JPEG 或 PNG),因此我尝试使用 javax.imageio 包的 write 方法.这适用于其他格式(例如 JPEG 到 PNG),但是当涉及 JPEG2000(或 TIFF)时,它会引发异常.谁能告诉我输入图像的可能格式是什么?

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

这是方法:

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;
}

我这样称呼它:

System.out.println(AndroidInterface.convertFormat("g:\\picture.jp2","g:\\conv.gif", "gif"));

推荐答案

ImageIO 内置以下格式:BMP、GIF、JPEG、PNG、WBMP(来源:API 文档).如果您尝试读取不同格式的图像,ImageIO.read(...) 方法将简单地返回 null,这就是您获得 的原因IllegalArgumentException: im == null 稍后在您的方法中.

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.

但是,ImageIO 还使用插件机制(服务提供者接口,或 SPI),以允许安装额外的或第三方插件.

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

为了能够读取 JPEG2000 或 TIFF,您需要这样的插件.

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

  • 对于 JPEG2000,最好的选择可能是 JAI.JAI 还有一个 TIFF 插件.JAI 由 Sun(现为 Oracle)开发,但遗憾的是,多年来一直没有更新和错误修复.

  • 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.

还有用于 OpenJPEG 的 Java 绑定,其中应包含用于 JPEG2000 的 ImageIO 插件.

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

对于 TIFF,您还可以使用我的 TwelveMonkeys ImageIO TIFF 插件.TwelveMonkeys 目前没有 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.

(此列表并不详尽,Google 可能会帮助您找到更多信息 :-) )

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

PS:从 Java 9 (JEP-262) 及更高版本开始,还内置了 TIFF 格式支持.

PS: From Java 9 (JEP-262) and later, TIFF format support is also built in.

这篇关于使用 ImageIO 将 JPEG2000 转换为 PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆