简体   繁体   English

ImageIO交替读取InputStream到BufferedImage

[英]ImageIO Alternitive to read for InputStream to BufferedImage

I've got a Spring MVC webapp written in Java which you can upload images too. 我有一个用Java编写的Spring MVC Web应用程序,您也可以上传图像。 These images need to be manipulated (resized, cropped, etc) and I'm using the Scalr library for this, which requires the images to be read from a BufferedImage. 这些图像需要进行处理(调整大小,裁剪等),为此,我正在使用Scalr库,该库需要从BufferedImage中读取图像。

In Java you can convert from an InputStream (which is how images come in) to a BufferedImage very easily: 在Java中,您可以轻松地将InputStream(图像的输入方式)转换为BufferedImage:

final BufferedImage img = ImageIO.read(in);

However, ImageIO is really pernickety about image formats and throws all sorts of exceptions if something isn't right about the image. 但是,ImageIO确实对图像格式有些执着,如果图像不正确,则会抛出各种异常。 Users could be uploading almost any quality of image to the web app, so this isn't acceptable. 用户几乎可以将任何质量的图像上传到Web应用程序,因此这是不可接受的。

So I'm looking for an alternative. 所以我正在寻找替代方案。 I've done some googling and one suggestion was Java Advanced Imaging from Oracle. 我做了一些谷歌搜索,一个建议是Oracle的Java Advanced Imaging。 The only problem is that it appears to rely on the Oracle Java implementation and I'm not using that. 唯一的问题是,它似乎依赖于Oracle Java实现,而我没有使用它。

Is there another library I can use? 我可以使用另一个图书馆吗?

I'd say, don't change your code just yet! 我会说,暂时不要更改您的代码!

The good thing about the ImageIO library is that it is completely plugin-based. 关于ImageIO库的好处是它完全基于插件。 If it doesn't read the formats you need right out of the box, you can add support very easily. 如果它没有立即读取您需要的格式,则可以非常轻松地添加支持。

I've written a couple of plugins for the TwelveMonkeys ImageIO project (in particular, it includes a JPEGImageReader that supports CMYK color space). 我为TwelveMonkeys ImageIO项目编写了两个插件(特别是,它包括支持CMYK颜色空间的JPEGImageReader )。

Werner Randelshofer's CMYKJPEGImageReader linked by @MadProgrammer is another plugin you could try out. CMYKJPEGImageReader链接的Werner Randelshofer的CMYKJPEGImageReader是另一个可以尝试的插件。

JAI, as you have mentioned, also has plugins that allows you to read CMYK JPEGs (I think), however many of the plugins requires native libraries and extra installation that might be a hassle in a web app context. 正如您所提到的,JAI也具有允许您读取CMYK JPEG的插件(我认为),但是许多插件都需要本机库和额外的安装,这可能在Web应用程序上下文中很麻烦。 The project also hasn't been updated for years. 该项目也多年未更新。

I'm sure there are commercial plugins available too, but I haven't researched these. 我敢肯定也有商业插件可用,但是我还没有研究它们。

If you really want to change library, have a look at Apache Commons Imaging. 如果您真的想更改库,请查看Apache Commons Imaging。 It has a very nice API, and supports many formats. 它具有非常好的API,并支持多种格式。 Images are read into BufferedImage s. 图像被读入BufferedImage Their JPEG support , however, isn't very mature at the moment, so it most likely will not read your image. 但是, 它们的JPEG支持目前还不是很成熟,因此很可能无法读取您的图像。

JMagick and im4java both use ImageMagick which has very good support for many formats. JMagickim4java都使用ImageMagick,它对许多格式都有很好的支持。 Requires native IM installation. 需要本地IM安装。 The APIs are file based however, which means you often have to write your streams to a temp file before processing. 这些API是基于文件的,这意味着您通常必须在处理之前将流写入临时文件。 Also doesn't directly support BufferedImage s, but you can convert if you like, and in many cases you can use IMs built in image manipulation instead of Java. 同样也不直接支持BufferedImage ,但是您可以根据需要进行转换,并且在许多情况下,可以使用内置于图像处理中的IM代替Java。

Again, there might be complete commercial library alternatives, either in Java or based on native code. 同样,可能有完整的商业库替代方案,可以是Java或基于本机代码的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM