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

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

问题描述

我正在尝试将JPEG2000(.jp2)图像转换为其他格式(JPEG或PNG),因此我尝试使用javax.imageio包的写入方法.这对于其他格式(例如,从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(来源:

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 :-) )

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

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