【发布时间】:2011-02-26 23:57:58
【问题描述】:
大家好,
每当我尝试使用ImageIO.createImageInputStream 获取ImageInputStream 对象时,它只会返回null,没有异常、警告或错误。我尝试将不同的数据类型传递给函数,一个简单的File 和一个InputStream,但都返回了null。文档说,如果没有找到合适的ImageInputStreamSpi,那么该函数将返回null,但该文件是沼泽标准JPEG,并且Java 肯定带有开箱即用的这种格式的服务提供商?
感谢您的宝贵时间。
/**
* Reads in an image from a file and returns the image in a
* {@code BufferedImage} object.
*
* @param source the file to create the {@code BufferedImage}
* from.
* @return the {@code BufferedImage} object representing the image
* in {@code source}.
*/
private BufferedImage readImage( File source ) {
// There is only one image in this file
final int imageIndex = 0;
BufferedImage image = null;
try {
// Get the ImageReader object for this filetype
Iterator readers =
ImageIO.getImageReaders( source );
ImageReader reader = (ImageReader) readers.next();
// Create an ImageInputStream object from the source image file
ImageInputStream iis = ImageIO.createImageInputStream( source );
// Raises IllegalArgumentException, because iis is null
reader.setInput( iis, true );
// Read the image file
image = reader.read( imageIndex );
} catch ( Exception exception ) {
exception.printStackTrace();
System.exit( -1 );
}
return image;
}
【问题讨论】:
-
清理您的代码并将其发布在这里以便我们提供帮助:)
-
除了,你没有回答我的问题,你只是给了我一个我以前已经在使用的不同的解决方案,如下详述......我原来的问题,我仍然有问题,仍未得到答复...
-
我也有同样的问题。出于某种原因,它可以在 Eclipse 中的 MacOSX 1.6 上运行,但在 debian linux 环境的生产环境中失败。我的应用程序是基于 OSGI 的 Web 应用程序,构建在 Equinox 和 Jetty servlet 容器上。从我目前阅读的所有内容来看,它似乎与 JAI 和 OSGI (stackoverflow.com/questions/1493199/running-jai-in-osgi) 有关。但是我还没有解决。
标签: java javax.imageio