Java IllegalArgumentException:源栅格波段数和源颜色空间分量不匹配对于彩色图像异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18351583/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:48:08  来源:igfitidea点击:

IllegalArgumentException: Numbers of source Raster bands and source color space components do not match For a color image Exception

javaimage-processingbufferedimagejavax.imageio

提问by Shanaka Kuruwita

The above answer that someone has suggest, converts my colored image to a black and white one. So it's not appropriate for my question.

有人建议的上述答案将我的彩色图像转换为黑白图像。所以它不适合我的问题。

File file = new File("path");          
BufferedImage bufferedImage = ImageIO.read( file );

here is the code and below is the image. Download the image and save in your pc. And try to run the above code with a correct value of path, it will throw an exception in the topic

这是代码,下面是图像。下载图像并保存在您的电脑中。并尝试使用正确的路径值运行上面的代码,它会在主题中抛出异常

Download image: https://skydrive.live.com/?cid=19547371C4F3B839&id=19547371C4F3B839%21105

图片下载:https: //skydrive.live.com/?cid=19547371C4F3B839&id=19547371C4F3B839%21105

Simply if someone can obtain a java.awt.image.BufferedImage object from the image given that's enough (should not convert the image to gray scale one).

只要有人可以从图像中获取 java.awt.image.BufferedImage 对象就足够了(不应将图像转换为灰度图像)。

You are a genius if you can answer this :D. Plz help.

如果你能回答这个问题,你就是天才:D。请帮忙。

采纳答案by haraldK

There's nothing wrong with your code here. I could read your image using my JPEGImageReaderplugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.

您的代码在这里没有任何问题。我可以使用我的 ImageIO JPEGImageReader插件读取您的图像。这个图像阅读器试图对 JPEG 错误保持宽容,并且比标准的 Java JPEGImageReader 稍微强大一些。

However, your JPEG file seems to have a number of problems so it can't be read 100%:

但是,您的 JPEG 文件似乎有很多问题,因此无法 100% 读取:

  • First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply ignore the ICC profile.
  • Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.
  • 首先,图像中的 ICC 颜色配置文件有 4 个颜色分量,而图像数据只有 3 个颜色分量(这是导致您看到的异常)。根本原因可能是糟糕的转换软件。使用 ExifTool 或类似软件查看元数据以进行进一步调查。我的读者会简单地忽略 ICC 配置文件。
  • 其次,JPEG 流过早结束(缺少 EOI)。您会注意到图像右下方有一些垃圾像素。除了获取原始图像(好吧,实际上,图像包含缩略图并且缩略图未损坏;如果确实需要,您可以尝试从中重新创建数据),您无能为力。我的阅读器返回的图像与本机软件读取的图像一致。