ImageIO不支持的图像类型-TwelveMonkeys带有修复程序的插件无法正常工作? [英] ImageIO Unsupported Image Type - TwelveMonkeys Plugin with fix not working?

查看:1197
本文介绍了ImageIO不支持的图像类型-TwelveMonkeys带有修复程序的插件无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用 com.sun.imageio.plugins.jpeg.JPEGImageReader 的颜色配置文件不兼容,我遇到了不支持的图像类型错误。后来,我发现TwelveMonkeys插件被证明可以解决此问题,并在我的项目类路径中引用了相关的.jars。我从TwelveMonkeys github存储库下载了它们。请注意,我正在使用3.0.2版,因为我正在Java 6和JDK 1.6.0_45上运行。这些是我添加到项目中的.jar文件:

  common-lang-3.0.2.jar 
common-io-3.0.2.jar
common-image-3.0.2.jar
imageio-core-3.0.2.jar
imageio-metadata-3.0.2.jar
imageio-jpeg-3.0.2.jar

我能够测试该库是否已安装并使用以下测试工作:

  Iterator< ImageReader> readers = ImageIO.getImageReadersByFormatName( JPEG); 
while(readers.hasNext()){
System.out.println( reader: + readers.next());
}

哪个输出:

 阅读器:com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader@4102799c 
阅读器:com.sun.imageio.plugins.jpeg.JPEGImageReader@33d6f122

当我运行代码时,它仍尝试使用 com.sun.imageio读取JPEG。 .plugins.jpeg.JPEGImageReader 并继续抛出IIOException。有想法吗?



更新:
看起来像iTextPDF引起了问题,该问题是项目使用的库。我设置了一个准系统测试应用程序,该应用程序将CMYK JPEG转换为 BufferedImage ,然后调用 ImageIO.read(img)并它工作正常。我正在努力寻找为什么iText在相同项目和类路径中调用 ImageIO.read(img)时找不到TwelveMonkeys插件的原因,但这可能是由于我的知识有限。我还应该补充一点,我正在使用的应用程序是网络服务API的一部分。

解决方案

通常,当未在Web应用程序上运行时使用ImageIO插件时,原因是的原因是找不到服务提供商,因为 ImageIO 已被初始化,并在网络之前调用了 scanForPlugins()应用程序的库可用于JVM。


来自在网络应用中部署[ImageIO]插件


因为ImageIO插件注册表( IIORegistry )是 VM全局,默认情况下,它不适用于servlet上下文。如果从 WEB-INF / lib classes 文件夹加载插件,这一点尤其明显。除非您在代码中的某处添加 ImageIO.scanForPlugins(),否则这些插件可能根本不可用。


此外,servlet上下文动态加载和卸载类(每个上下文使用一个新的类加载器)。如果重新启动应用程序,则默认情况下,旧类将永久保留在内存中(因为下次调用 scanForPlugins 时,它将是另一个 ClassLoader 扫描/加载类,因此它们将是注册表中的新实例)。如果使用剩余的旧之一尝试读取。读者,可能会发生奇怪的异常(例如访问静态最终初始化字段时为 NullPointerExceptions 或未初始化内部类为 NoClassDefFoundErrors )。 / p>

要解决发现问题和资源泄漏,强烈建议使用 IIOProviderContextListener 来实现动态加载和卸载用于Web应用程序的ImageIO插件。


IIOProviderContextListener 包含在中十二monkeys-servlet.jar ,并且必须在应用程序的 web.xml 中注册(如果使用Spring或其他框架,则必须类似)。有关详细信息,请参见上面的链接。


使用上下文侦听器的另一种安全替代方法是将JAR文件放置在应用程序服务器的共享或公共lib文件夹中,而不是 Web应用程序中的WEB-INF / lib 文件夹。


PS:以上问题/解决方案通常适用于ImageIO插件,而不仅仅是TwelveMonkeys插件。因此,上下文侦听器不依赖于TwelveMonkeys ImageIO插件,并且可以与JAI ImageIO或其他ImageIO插件一起使用。


I encountered the Unsupported Image Type error due to an incompatible colour profile using com.sun.imageio.plugins.jpeg.JPEGImageReader. I later found the TwelveMonkeys plugins that are proven to fix this issue and have referenced the dependent .jars in my project classpath. I downloaded them from the TwelveMonkeys github repository. Note i'm using version 3.0.2 because I'm running on Java 6 with JDK 1.6.0_45. These are the .jars I've added to my project:

common-lang-3.0.2.jar
common-io-3.0.2.jar
common-image-3.0.2.jar
imageio-core-3.0.2.jar
imageio-metadata-3.0.2.jar
imageio-jpeg-3.0.2.jar

I was able to test the library is installed and working using the following test:

Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
while (readers.hasNext()) {
    System.out.println("reader: " + readers.next());
}

Which outputs:

reader: com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader@4102799c
reader: com.sun.imageio.plugins.jpeg.JPEGImageReader@33d6f122

When i run my code, it is still trying to read the JPEG with com.sun.imageio.plugins.jpeg.JPEGImageReader and continues to throw the IIOException. Any ideas?

UPDATE: Its looking like iTextPDF is causing the issue which is a library used by the project. I setup a barebone test application that converts a CMYK JPEG to a BufferedImage and then calls ImageIO.read(img) and it works fine. I'm struggling to see a reason why iText wouldn't be finding the TwelveMonkeys plugin when it calls ImageIO.read(img) when they're in the same project and classpath, but that's probably due to my limited knowledge. I should also add that the application i'm working on is part of a web service API.

解决方案

As often is the case, when an ImageIO plugin isn't used at run-time from a web application, the cause is that the service provider isn't found because ImageIO is already initialized, and has invoked scanForPlugins() before the web app's libraries were available to the JVM.

From Deploying [ImageIO] plugins in a web app:

Because the ImageIO plugin registry (the IIORegistry) is "VM global", it doesn't by default work well with servlet contexts. This is especially evident if you load plugins from the WEB-INF/lib or classes folder. Unless you add ImageIO.scanForPlugins() somewhere in your code, the plugins might never be available at all.

In addition, servlet contexts dynamically loads and unloads classes (using a new class loader per context). If you restart your application, old classes will by default remain in memory forever (because the next time scanForPlugins is called, it's another ClassLoader that scans/loads classes, and thus they will be new instances in the registry). If a read is attempted using one of the remaining "old" readers, weird exceptions (like NullPointerExceptions when accessing static final initialized fields or NoClassDefFoundErrors for uninitialized inner classes) may occur.

To work around both the discovery problem and the resource leak, it is strongly recommended to use the IIOProviderContextListener that implements dynamic loading and unloading of ImageIO plugins for web applications.

The IIOProviderContextListener is contained in the twelvemonkeys-servlet.jar, and must be registered in your application's web.xml (or similar if using Spring or other framework). See the above link for details.

Another safe alternative to using the context listener, is to place the JAR files in the application server's shared or common lib folder, instead of the WEB-INF/lib folder inside your web application.

PS: The above problem/solution applies to ImageIO plugins in general, not just the TwelveMonkeys plugins. Because of this, the context listener has no dependencies to the TwelveMonkeys ImageIO plugins, and may be used with JAI ImageIO or other ImageIO plugins as well.

这篇关于ImageIO不支持的图像类型-TwelveMonkeys带有修复程序的插件无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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