ImageIO give me a CMMException due to bad JPEG - java

I try to read a JPEG file with ImageIO.read() but for this image it give me a CMMException. after read this and this I understand ImageIO can't read some kind of jpeg file.
So I need a solution to read all kind of images. JAI library look to be a dead library. And I don't undestrand how TwelveMonkeys works. So if someone have explainations about it or another alternative, I'll take it. Thank's

For reading most JPEGs (even those that that cause CMMExceptions), you can use ImageIO and TwelveMonkeys ImageIO plug-ins. To do so, add the following dependency to your Maven project:
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.0</version>
If you already use ImageIO to read images, there's no need to change your code. :-)
To verify that the plugin is installed and used at run-time, you could use the following code:
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
while (readers.hasNext()) {
System.out.println("reader: " + readers.next());
}
The first line should print:
reader: com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader#somehash

Related

Java - How to convert a DICOM file to a JPEG2000 file?

I need to convert a DICOM file (.dcm) in a jpeg2000 format, someone has a code snippet that perform this operation?
I found in the web only a way for the dcm to jpeg conversion (the first answer to that question: How to convert dicom file to jpg conversion), but that doesn't work for me.
Thanks in advice for te help and sorry for my not perfect english.
The best tool I encountered when doing image conversion is ImageMagick (http://www.imagemagick.org/), which supports Dicom as input and JPEG-2000 as output.
I also have used dcm4che for this purpose (it has the advantage to be a purely Java solution), but the JPEG-2000 encoder had a memory leak - so if you have to do it for lots of images, it might be a problem (recent version might be fixed though).

Converting TIFF file to bufferedImage

I am trying to convert a Tiff file to Buffered Image.
I used ImageIO to do that but i figured out that it doesn't support this type of files.
ImageIO.read(file);
Should i use other than ImageIO or there's a solution ?
You need to put jai_imageio.jar in your classpath. check whether you have appropriate readers by using ImageIO.getReadersByFormatName("tiff").
Hope this helps.

Is is possible to use Java to read bitmap file header?

I found a problem when loading a bitmap file in my working company software. It crashes the software when I drop in the bitmap file. However, I am trying to create a Java app to read the bitmap file header and display the header information. So I know what might causing the problem.
Can anyone suggest the idea how can I grab the bitmap file header information or which class should I use to achieve this goal?
Yes it is possible, I wrote code to do something like this with jpeg headers some months ago.
Basically, you need to learn a bit about the bitmap file format.
Then you need to open the file (for reading bytes).
Finally, you read enough bytes to get to the right field in the header, and decompose that to the Java data type you want.
There may be a class that already does this, in which case I would suggest Google for finding it.
The apache sanselan project provides a BmpImageParser class for parsing BMP files. You can take a look at the source here.

Java ImageIO IIOException: Unsupported image type?

Working with images in Java for the first time and am getting some bizarro exceptions that aren't documented very well. Here's the line of code that is failing:
BufferedImage imgSelected = ImageIO.read(new File("/abs/url/to/file/image.jpg"));
This line is throwing an IIOException with Unsupported image type as the exception message. I have checked and re-checked that it is in fact this line throwing the exception, that the File object is valid, that the URL is valid, and that the image.jpg is in fact a valid JPG that loads perfectly fine in other image viewers.
What could I do to get more information about the nature of this exception? Is this the traditional way for loading images in Java 7, or is this an old/deprecating method? There's just not a lot of info out there about these "Unsupported image type" exceptions, and surely, ImageIO supported JPGs!
Thanks for any help!
Try to check the encoding of the JPEG. ImageIO can't read CMYK-encoded jpeg images for example.
AFAIK, ImageIO hasn't been updated for years, so you'd like to try and use the official alternative/extension: JAI ImageIO.
Unforutnately, JAI ImageIO needs some native libraries installed into the JRE, which might be unwanted.
We do the following:
use Apache Sanselan to detect, whether it's a JPEG
since Sanselan can't read and write JPEG, use the plain old AWT JPEGCodec: JPEGCodec.createJPEGDecoder(...)
to convert CMYK to RGB, we then get the raster of the read BufferedImage and manually convert it (you could use ICC profiles, but the manual conversion fits our needs)
Here's a question of mine that resulted of the fact that ImageIO doesn't support all types of JPEG images, and I there stated a little more of my findings of why you get that message: Pure Java alternative to JAI ImageIO for detecting CMYK images
I've unfortunately come across a lot of standard violating JPEG files. ImageIO is particularly picky and often refuse to load images, which are often loaded and apparently displayed correctly by other software with less strict checks on the file format.
It's not very pretty, but one workaround is to use the Oracle VM internal JPEG decoder directly (com.sun.image.codec.jpeg.JPEGCodec), as it seems to tolerate more spec deviations as the ImageIO wrapper:
BufferedImage img =
JPEGCodec.createJPEGDecoder(inputStream).decodeAsBufferedImage();
This is of course not an ideal solution, since using implementation specific classes will lock you to a specific VM vendor and may break with newer VM versions, but if you'll only used the software in a controlled environment, it may be better than no solution at all.
To work with images in a specific format,you need to add the corresponding dependency, such as imageio-jpeg or imageio-tiff:
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-bmp</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-core</artifactId>
<version>3.3.2</version>
</dependency>
the built-in ImageIO Java API loads the plugins automatically at runtime.
Another option is to use .jar prepared by Werner Randelshofer:
http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/ or Monte Media Library: http://www.randelshofer.ch/monte/
It looks quite easy and similar to ImageIO usage and available under CC license.
This tutorial provided an answer using the apache commons IO library. I found it to be a cleaner implementation. I included the dependency below
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
Here is the code that worked after adding the dependency
public ResponseEntity<byte[]> getImage(#PathVariable("filename") String filename) {
byte[] image = new byte[0];
try {
image = FileUtils.readFileToByteArray(new File(FILE_PATH_ROOT+filename));
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
}

Reading JCS_YCCK images using ImageIO

I'm using ImageIO.read to process uploaded image files. The code is similar to
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image.getContents()));
I've managed to solve most of the issues, but this one has left me clueless. The uploaded image has a JCS_YCCK profile, as defined in com.sun.imageio.plugins.jpeg.JPEG, which is not supported by com.sun.imageio.plugins.jpeg.JPEGImageReader. This leads to a nice stack trace similar to:
Caused by: javax.imageio.IIOException: Unsupported Image Type
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:910)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:885)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at com.example.ImageWriter.resizeEmbeddableImageInPlace(ImageWriter.java:231)
How can I process this type of JPEG using Java's ImageIO?
Update: I've tried Commons-Sanselan, indicated by an answer, but unfortunately it does not support JPEG files:
org.apache.sanselan.ImageReadException: Sanselan cannot read or write JPEG images.
at org.apache.sanselan.formats.jpeg.JpegImageParser.getBufferedImage(JpegImageParser.java:90)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1264)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1231)
One possible solution is to use the Java Advanced Imaging Image IO extensions. When properly installed, the conversion works out of the box.
The problem is that it does not play well with Maven, so I've asked Using Java Advanced Imaging with Maven. If that works out, this answer will be accepted.
I don't know for ImageIO, but you could use the Commons Sanselan library, which offers easy ways to access all sorts of images.

Categories