用于检测 CMYK 图像的 JAI ImageIO 的纯 Java 替代品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6829428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pure Java alternative to JAI ImageIO for detecting CMYK images
提问by Thomas
first I'd like to explain the situation/requirements that lead to the question:
首先,我想解释导致问题的情况/要求:
In our web application we can't support CMYK images (JPEG) since IE 8 and below can't display them. Thus we need to detect when someone wants to upload such an image and deny it.
在我们的 Web 应用程序中,我们不能支持 CMYK 图像 (JPEG),因为 IE 8 及以下版本无法显示它们。因此,我们需要检测何时有人想要上传这样的图像并拒绝它。
Unfortunately, Java's ImageIO won't read those images or would not enable me to get the detected color space. From debugging it seems like JPEGImageReader
internally gets the color space code 11 (which would mean JCS_YCCK
) but I can't safely access that information.
不幸的是,Java 的 ImageIO 不会读取这些图像或无法让我获得检测到的色彩空间。从调试来看,似乎在JPEGImageReader
内部获得了颜色空间代码 11(这意味着JCS_YCCK
),但我无法安全地访问该信息。
When querying the reader for the image types I get nothing for CMYK, so I might assume no image types = unsupported image
.
当向读者询问图像类型时,我对 CMYK 一无所获,因此我可能假设no image types = unsupported image
.
I converted the source CMYK image to RGB using an imaging tool in order to test whether it would then be readable (I tried to simulate the admin's steps when getting the message "No CMYK supported"). However, JPEGImageReader
would not read that image, since it assumes(comment in the source!)3-component RGB color space but the image header reports 4 components (maybe RGBA or ARGB) and thus an IllegalArgumentException
is thrown.
我使用成像工具将源 CMYK 图像转换为 RGB,以测试它是否可读(我尝试在收到消息“不支持 CMYK”时模拟管理员的步骤)。但是,JPEGImageReader
不会读取该图像,因为它假定(在源代码中注释!)3 分量 RGB 颜色空间,但图像标题报告 4 个分量(可能是 RGBA 或 ARGB),因此IllegalArgumentException
会抛出一个。
Thus, ImageIO is not an option since I can't reliably get the color space of an image and I can't tell the admin why an otherwise fine image (it can be displayed by the browser) would not be accepted due to some internal error.
因此,ImageIO 不是一个选项,因为我无法可靠地获得图像的色彩空间,而且我无法告诉管理员为什么由于某些内部原因而不会接受其他精美的图像(它可以由浏览器显示)错误。
This led me to try JAI ImageIO whose CLibJPEGImageReader
does an excellent job and correctly reads all my test images.
这让我尝试了 JAI ImageIO,它CLibJPEGImageReader
做得非常好,并且可以正确读取我所有的测试图像。
However, since we're deploying our application in a JBoss that might host other applications as well, we'd like to keep them as isolated as possible. AFAIK, I'd need to install JAI ImageIO to the JRE or otherwise make the native libs available in order to use them, and thus other applications might get access to them as well, which might cause side effects (at least we'd have to test a lot to ensure that's not the case).
但是,由于我们将应用程序部署在可能托管其他应用程序的 JBoss 中,因此我们希望尽可能将它们隔离。AFAIK,我需要将 JAI ImageIO 安装到 JRE 或以其他方式使本机库可用才能使用它们,因此其他应用程序也可能访问它们,这可能会导致副作用(至少我们有进行大量测试以确保情况并非如此)。
That's the explanation for the question, and here it comes again: Is there any pure Java alternative to JAI ImageIO which reliably detects and possibly converts CMYK images?
这就是问题的解释,这里又来了: 是否有任何纯 Java 替代 JAI ImageIO 可以可靠地检测并可能转换 CMYK 图像?
Thanks in advance,
提前致谢,
Thomas
托马斯
采纳答案by Thomas
I found a solution that is ok for our needs: Apache Commons Sanselan. This library reads JPEG headers quite fast and accurate (at least all my test images) as well as a number of other image formats.
我找到了一个可以满足我们需求的解决方案:Apache Commons Sanselan。这个库可以非常快速和准确地读取 JPEG 标头(至少我的所有测试图像)以及许多其他图像格式。
The downside is that it won't read JPEG image data, but I can do that with the basic JRE tools.
缺点是它不会读取 JPEG 图像数据,但我可以使用基本的 JRE 工具来做到这一点。
Reading JPEG images for conversion is quite easy (the ones that ImageIO
refuses to read, too):
读取 JPEG 图像进行转换非常容易(ImageIO
拒绝读取的图像也是如此):
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new FileInputStream( new File(pFilename) ) );
BufferedImage sourceImg = decoder.decodeAsBufferedImage();
Then if Sanselan tells me the image is actually CMYK, I get the source image's raster and convert myself:
然后,如果 Sanselan 告诉我图像实际上是 CMYK,我将获取源图像的光栅并自行转换:
for( /*each pixel in the raster, which is represented as int[4]*/ )
{
double k = pixel[3] / 255.0;
double r = (255.0 - pixel[0])*k;
double g = (255.0 - pixel[1])*k;
double b = (255.0 - pixel[2])*k;
}
This give quite good results in the RGB images not being too bright or dark. However, I'm not sure why multiplying with k
prevents the brightening. The JPEG is actually decoded in native code and the CMYK->RGB conversion I got states something different, I just tried the multiply to see the visual result.
这在不太亮或不太暗的 RGB 图像中给出了很好的结果。但是,我不确定为什么乘以k
防止变亮。JPEG 实际上是在本机代码中解码的,我得到的 CMYK->RGB 转换状态有所不同,我只是尝试了乘法以查看视觉结果。
If anybody could shed some light on this, I'd be grateful.
如果有人能对此有所了解,我将不胜感激。
回答by Codo
I've posted a pure Java solutionfor reading all sorts of JPEG images and converting them to RGB.
我已经发布了一个纯 Java解决方案,用于读取各种 JPEG 图像并将它们转换为 RGB。
It's built on the following facts:
它建立在以下事实之上:
- While ImageIO cannot read JPEG images with CMYK as a buffered image, it can read the raw pixel data (raster).
- Sanselan (or Apache Commons Imaging as it's called now) can be used to read the details of CMYK images.
- There are images with inverted CMYK values (an old Photoshop bug).
- There are images with YCCK instead of CMYK (can easily be converted).
- 虽然 ImageIO 无法读取带有 CMYK 的 JPEG 图像作为缓冲图像,但它可以读取原始像素数据(光栅)。
- Sanselan(或现在称为 Apache Commons Imaging)可用于读取 CMYK 图像的详细信息。
- 有些图像具有倒置的 CMYK 值(一个旧的 Photoshop 错误)。
- 有 YCCK 而不是 CMYK 的图像(可以轻松转换)。
回答by tsmets
Beware of another post as the Java 7 does not allow to use directly Sun's implementation without special parameters as indicated in import com.sun.image.codec.jpeg.*.
请注意另一篇文章,因为 Java 7 不允许在没有特殊参数的情况下直接使用 Sun 的实现,如import com.sun.image.codec.jpeg.* 所示。
回答by SyntaxT3rr0r
In our web application we can't support CMYK images (JPEG) since IE 8 and below can't display them. Thus we need to detect when someone wants to upload such an image and deny it.
在我们的 Web 应用程序中,我们不能支持 CMYK 图像 (JPEG),因为 IE 8 及以下版本无法显示它们。因此,我们需要检测何时有人想要上传这样的图像并拒绝它。
I don't agree with your "Thus we need to detect when someone wants to upload such an image and deny it". A much more user-friendly policy would be to convert it to something else than CMYK.
我不同意你的“因此我们需要检测何时有人想要上传这样的图像并拒绝它”。一个更加用户友好的策略是将其转换为 CMYK 以外的其他内容。
The rest of your post is a bit confusing in that regards seen that you ask both for detection and conversion, which are two different things. Once again, I think converting the image is much more user-friendly.
您的帖子的其余部分在这方面有点令人困惑,因为您同时要求检测和转换,这是两件不同的事情。再一次,我认为转换图像对用户更加友好。
No need to write in bold btw:
顺便说一句,不需要用粗体写:
Is there any pure Java alternative to JAI ImageIO which reliably detects and possibly converts CMYK images?
是否有任何纯 Java 替代 JAI ImageIO 可以可靠地检测并可能转换 CMYK 图像?
Pure Java I don't know, but ImageMagick works fine to convert CMYK image to RGB ones. Calling ImageMagickon the server-side from Java really isn't complicated. I used to do it manually by calling an external process but nowadays there are wrappers like JMagickand im4java.
纯 Java 我不知道,但 ImageMagick 可以很好地将 CMYK 图像转换为 RGB 图像。从 Java 在服务器端调用ImageMagick确实并不复杂。我曾经通过调用外部进程手动完成,但现在有像JMagick和im4java这样的包装器。