Java image reading -- Help me rewrite this code

M

Manish Hatwalne

I am using following code instead of ImageIO.read() as we have older Java
version and it gives dark image as specified in Java bug
(http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6372769). It has been
working fine till now, however recently it started throwing exception for
certain photos -

java.lang.IllegalArgumentException: Invalid ICC Profile Data

When I searched, I found that it is yet another Java bug -
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6404011

We **cannot** chnage the Java version on our server. So I have to figure out
workaround for the following code --

public static BufferedImage read(File file) throws IOException {
BufferedImage image = null;
ImageInputStream iis = ImageIO.createImageInputStream(file);
if (iis == null) {
throw new IOException("File not found");
}
Iterator<ImageReader> iir = ImageIO.getImageReaders(iis);
boolean looking = true;
ImageReader reader = null;
ImageReadParam param = null;
while(looking && iir.hasNext()) {
reader = iir.next();
reader.setInput(iis);
param = reader.getDefaultReadParam();
Iterator it = reader.getImageTypes(0); //this line throws
exception java.lang.IllegalArgumentException: Invalid ICC Profile Data

while (looking && it.hasNext()) {
ImageTypeSpecifier type = (ImageTypeSpecifier) it.next();
ColorSpace cs = type.getColorModel().getColorSpace();
if ( cs.isCS_sRGB() ) {
param.setDestinationType(type);
looking = false;
}
if ( cs.getType() != ColorSpace.TYPE_RGB ) {
looking = false;
}
}
}
if (reader != null) {
image = reader.read(0, param);
}
return image;
}
}How can I rewrite the code to do what I desire (read an image) by ignoring
ti sexception??? I tried to enclose the Exption throwing line & loop below
that in try-catch blok, but for soome weird reason, it simply hangs the JVM
and I am lost -- how can I fix this? I am not an image expert and hence
finiding it difficult to decide how to come up with a workaround!!Any help,
pointers would be highly appreciated!!TIA,- Manish
 
G

GArlington

I am using following code instead of ImageIO.read() as we have older Java
version and it gives dark image as specified in Java bug
(http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6372769). It has been
working fine till now, however recently it started throwing exception for
certain photos -

java.lang.IllegalArgumentException: Invalid ICC Profile Data

When I searched, I found that it is yet another Java bug -http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6404011

We **cannot** chnage the Java version on our server. So I have to figure out
workaround for the following code --

public static BufferedImage read(File file) throws IOException {
BufferedImage image = null;
ImageInputStream iis = ImageIO.createImageInputStream(file);
if (iis == null) {
throw new IOException("File not found");
}
Iterator<ImageReader> iir = ImageIO.getImageReaders(iis);
boolean looking = true;
ImageReader reader = null;
ImageReadParam param = null;
while(looking && iir.hasNext()) {
reader = iir.next();
reader.setInput(iis);
param = reader.getDefaultReadParam();
Iterator it = reader.getImageTypes(0); //this line throws
exception java.lang.IllegalArgumentException: Invalid ICC Profile Data

while (looking && it.hasNext()) {
ImageTypeSpecifier type = (ImageTypeSpecifier) it.next();
ColorSpace cs = type.getColorModel().getColorSpace();
if ( cs.isCS_sRGB() ) {
param.setDestinationType(type);
looking = false;
}
if ( cs.getType() != ColorSpace.TYPE_RGB ) {
looking = false;
}
}
}
if (reader != null) {
image = reader.read(0, param);
}
return image;
}}How can I rewrite the code to do what I desire (read an image) by ignoring

ti sexception??? I tried to enclose the Exption throwing line & loop below
that in try-catch blok, but for soome weird reason, it simply hangs the JVM
and I am lost -- how can I fix this? I am not an image expert and hence
finiding it difficult to decide how to come up with a workaround!!Any help,
pointers would be highly appreciated!!TIA,- Manish

It seems that you found the bug details, but failed to read the info.
The workaround is shown in the bottom of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6404011
post.
 
M

Manish Hatwalne

pointers would be highly appreciated!!TIA,- Manish
It seems that you found the bug details, but failed to read the info.
The workaround is shown in the bottom of
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6404011
post.

This workaround doesn't work for me either, is throws exception -

Not a JPEG file: starts with 0x59 0x68

com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with
0x59 0x68
at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native
Method)
at
sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(JPEGImageDecoderImpl.java:210)

Any other ideas?

- Manish
 
G

GArlington

This workaround doesn't work for me either, is throws exception -

Not a JPEG file: starts with 0x59 0x68
This suggests that what you have is MJpeg file...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top