9

I'm using Java 8 (OpenJDK 8, specifically) for a project and I need to read an image from a JPG file. Searching around got me to some similar questions at first (e.g. Read byte array into buffered image WITHOUT ImageIO and Is there a 100% Java alternative to ImageIO for reading JPEG files?), but my problem is different and the solutions there do not meet my requirements.

It turns out that the JPEGImageReader class is still missing. (See openjdk-8: Missing JPEGImageReader functions in libjavajpeg.so) Although that bug report is for Debian, I'm using Kubuntu 14.10 and it's also affected.

Based on the report's last message, no one seems to be working on this issue at this time...

The code snippet to reproduce this error is:

// ...
BufferedImage img = null;
try {
    img = ImageIO.read(new File(filename));
} catch (IOException e) {
    throw new RuntimeException(e);
}
// ...

The path to the file is valid and this works normally if I use Java 1.7, but changing to 1.8 causes the following excpetion on ImageIO.read call:

Caught UnsatisfiedLinkError: com.sun.imageio.plugins.jpeg.JPEGImageReader.initReaderIDs(Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Class;)

I'd like help working around this problem while avoiding:

  1. additional dependencies on other/external libraries;
  2. going back to Java 1.7;
  3. having to rebuild from source;
  4. Oracle's proprietary implementation of the JDK

Working code snippets appreciated.

EDIT-1: Added point #4 to the list.

EDIT-2: Reworded a portion of the main section and added another reference.

code_dredd
  • 5,915
  • 1
  • 25
  • 53
  • Is using the Oracle JDK a possibility? If so, there is some info here on a ppa you can use: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html – clstrfsck Feb 22 '15 at 23:54
  • I'm trying to stay away from Oracle's proprietary implementation, if at all possible. I'll update the question to mention this. – code_dredd Feb 23 '15 at 00:25
  • So [ImageIO.getImageReadersByFormatName("jpeg")](http://docs.oracle.com/javase/8/docs/api/javax/imageio/ImageIO.html#getImageReadersByFormatName-java.lang.String-) does not yield a JPEG reader? – Joop Eggen Feb 23 '15 at 01:34
  • It tries, but can't do it because the `JPEGImageReader` class is missing, as per the bug report linked above. That reproduces the `UnsatisfiedLinkError: com.sun.imageio.plugins.jpeg.JPEGImageReader ...` exception mentioned in the original post. – code_dredd Feb 23 '15 at 02:10
  • @msandiford: It seems there's currently no way to work around this issue in OpenJDK 8, so I went ahead and (reluctantly) installed Oracle's implementation of JDK 8 and it worked. I'll be going back to OpenJDK 8 as soon as they get this serious bug fixed. However, I'm not sure I can mark a comment as the accepted answer, though. – code_dredd Feb 23 '15 at 02:24
  • Hi @ray. I've converted the comment to an answer (of sorts). – clstrfsck Feb 23 '15 at 03:14

4 Answers4

3

Is using the Oracle JDK a possibility?

If so, there is some info here on a ppa you can use with ubuntu.

clstrfsck
  • 14,715
  • 4
  • 44
  • 59
3

I believe this has now been fixed as of March 17th, 2015 release, as per Matthias Klose's email:

Source: openjdk-8 Source-Version: 8u40-b27-1

We believe that the bug you reported is fixed in the latest version of openjdk-8, which is due to be installed in the Debian FTP archive.

A summary of the changes between this version and the previous one is attached.

Thank you for reporting the bug, which will now be closed. If you have further comments please address them to 760926@bugs.debian.org, and the maintainer will reopen the bug report if appropriate.

Debian distribution maintenance software pp. Matthias Klose (supplier of updated openjdk-8 package)

(This message was generated automatically at their request; if you believe that there is a problem with it please contact the archive administrators by mailing ftpmaster@ftp-master.debian.org)

Mahmoud Abdelkader
  • 23,011
  • 5
  • 41
  • 54
  • I saw the email yesterday, but I have not yet seen a package update being made available in the repositories or had time to try it out. I will update this after I try it out myself. – code_dredd Mar 19 '15 at 03:23
  • I confirmed that the packages have been updated and made available today, using java-8-openjdk-amd64 from Kubuntu 15.04. – code_dredd May 25 '15 at 01:35
1

You could install the java advanced imaging libraries which also provides a jpeg (and other codec) implementation.

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
  • Thanks for the suggestion, but this goes against point #1, which explicitly tries to avoid additional dependencies on external libraries. Installing the JAI does exactly this. – code_dredd Feb 23 '15 at 00:38
  • Could you please elaborate more on how to do that? I don't mind having additional dependecies. – KareemJ May 23 '19 at 15:11
  • @KareemJeiroudi, the install link provides instructions. – Brett Okken May 28 '19 at 13:34
1

I believe the problem here is with the fact that the generated image is in JPEG format.Once i got it generated it in PNG format,the issue got resolved. I used Grabzit API to capture screenshot and get it saved in PNG format

Nik
  • 452
  • 5
  • 17