1

Has anyone ever had an issue with Loading JPEG files in java? One of our clients is sending files that cannot be resolved, but these same files can be opened in windows. (the other 99% of jpeg files we recieve, process without any problems)

I have tried a couple of libraries to read these:

itextpdf : com.itextpdf.text.Jpeg (getInstance(imageData) - gives  "java.io.IOException: Premature EOF while reading JPG."
sanselan : org.apache.sanselan.ImageInfo ( Sanselan.getImageInfo(imageData) gives "java.io.IOException: Invalid Segment: insufficient data")
where imageData is a byte[]

The errors here suggest to me that the jpegs are broken, however, this does not explain why they are still viewable.

If anyone has any suggestions, I would be most grateful

Thanks

Andy
  • 14,427
  • 3
  • 52
  • 76
Causteau
  • 117
  • 1
  • 10
  • Did you try decreasing the picture size using paint ? – VirtualTroll Aug 09 '12 at 14:40
  • Check for encoding issues. Possibly on both ends. You or your client may by using some class (a `Reader` perhaps) that converts bytes depending on locale / encoding, and that breaks the data. – npe Aug 09 '12 at 14:40
  • Maybe the image use the CMYK model. http://stackoverflow.com/questions/2408613/problem-reading-jpeg-image-using-imageio-readfile-file – Xavier Delamotte Aug 09 '12 at 14:48
  • 1
    *"this does not explain why they are still viewable."* By what application on what OS? Note that most image viewers go to extraordinary lengths to guess image data out of corrupt image files. – Andrew Thompson Aug 09 '12 at 14:59
  • 2
    I'd try converting the images using some robust image manipulation tool such as [GraphicsMagick](http://www.graphicsmagick.org/) or [ImageMagick](http://www.imagemagick.org/script/index.php). Just convert the problematic JPG file into a new JPG file and try to process that. It's possible that the problematic JPGs are non-standard or broken, but some robust decoders might be able to deal with that (probably that's why they're viewable). It's just a workaround, but it could help. – Petr Aug 09 '12 at 15:02
  • I was afraid that windows would be guessing and filling in the blanks. It really appears to me that the images I have are in some way corrupt. I will speak with my clients and see how they create the images as well as looking at my own readers etc. Thanks very much for the pointers. – Causteau Aug 09 '12 at 15:04
  • thanks Petr - Yeah, just opening the images in an editor and saving as jpeg and running them through the code allows them to be recognised, thanks – Causteau Aug 09 '12 at 15:06
  • 1
    Your latest comments to @Petr & myself are shaping up into an answer of its own. Perhaps you should enter an answer, now you know more. – Andrew Thompson Aug 09 '12 at 15:27
  • Please send me a sample image and I'll tell you what's wrong with it (bitbank@pobox.com). – BitBank Aug 09 '12 at 19:12

1 Answers1

1

The "solution" I have gone for is to run the byte array we recieve through our current process which identifies type. If this can't identify the type, then I take the byte array and forcably save it to a JPEG. This can then be re read and pushed back through the current process.

Bit hacky, but its just a work around for a couple of months.

Thanks for everyone's help here!

Causteau
  • 117
  • 1
  • 10
  • I am also running into to the same problem. How did you exactly fixed the issue? Any help will be appreciated. Thanks, – ajm Jun 23 '17 at 12:51
  • @ashishjmeshram - I'm afraid its a long time ago that i actually "fixed" this. but if i remember correctly I just took the bytestream that came in, output it to a temp file on the server, then re read that file. Hardly what I'd call a solution... but it worked for the purpose. – Causteau Jul 19 '17 at 12:24