Search code examples
javax.imageio

Scala ImageIO.read(byteArrayInputStream) IllegalArgumentException: Numbers of source Raster bands and source color space components do not match


I am using ImageIo to directly read ByteArrayInputStream but for some specific images, I am getting the error as shown below:

IllegalArgumentException: Numbers of source Raster bands and source color space components do not match

I am not sure what exactly is going wrong here.

Image Causing the issue

I followed a bunch of threads in SO already:

Getting a BufferedImage as a resource so it will work in JAR file

Numbers of source Raster bands and source color space components do not match when i read image

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

But not able to follow up on what exactly needs to be changed in the code to handle inappropriate images (which get some corrupted pixels while transformation).

Edit 1: I somehow displaced the confidential content and here's the image I am trying to read: https://i.imgur.com/r8vXYnB.jpeg

Can someone help me with the code to fix these image errors?


Solution

  • For me the issue was that by default JPEG is not supported for javax.ImageIO. I added this in my build to make things work:

          // JPEG Support
          "com.twelvemonkeys.imageio" % "imageio-jpeg" % "3.9.3"
    

    Based on the image type you may have you would need to add more such extensions.

    You can find all of them in this README: https://github.com/haraldk/TwelveMonkeys/blob/master/README.md