Venabili
asked on
JAI: Reading bad JPEGs
Here is the code I use:
public int getImage(String fileName) {
try{
if (!fileName.toUpperCase().e ndsWith(". JPG") && !fileName.toUpperCase().en dsWith(".J PEG")){
return 0;
}
RenderedOp image = JAI.create("fileload", fileName);
try{
System.out.println(image.g etParamete rs());
System.out.println(image.g etBounds() );
}catch (Exception e){
return 0;
}
return 1;
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
All is fine, the create operation is ok. But when reading bad JPEGs (0 bytes ones or things that are not JPEGS at all) , the invoking of the methods on the image object is throwing an Exception (getParameters() seems to be the only one working)
All the others throw:
Error: IOException occurs when search for propriate codecs.
Occurs in: com.sun.media.jai.codec.Im ageCodec
java.io.EOFException
at com.sun.media.jai.codec.Se ekableStre am.readFul ly(Seekabl eStream.ja va:329)
at com.sun.media.jai.codec.Se ekableStre am.readFul ly(Seekabl eStream.ja va:306)
at com.sun.media.jai.codec.Im ageCodec.g etDecoderN ames(Image Codec.java :274)
at com.sun.media.jai.opimage. StreamRIF. create(Str eamRIF.jav a:80)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at java.lang.reflect.Method.i nvoke(Meth od.java:32 4)
at javax.media.jai.FactoryCac he.invoke( FactoryCac he.java:13 0)
at javax.media.jai.OperationR egistry.in vokeFactor y(Operatio nRegistry. java:1682)
at javax.media.jai.ThreadSafe OperationR egistry.in vokeFactor y(ThreadSa feOperatio nRegistry. java:481)
at javax.media.jai.registry.R IFRegistry .create(RI FRegistry. java:340)
at com.sun.media.jai.opimage. FileLoadRI F.create(F ileLoadRIF .java:122)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at java.lang.reflect.Method.i nvoke(Meth od.java:32 4)
at javax.media.jai.FactoryCac he.invoke( FactoryCac he.java:13 0)
at javax.media.jai.OperationR egistry.in vokeFactor y(Operatio nRegistry. java:1682)
at javax.media.jai.ThreadSafe OperationR egistry.in vokeFactor y(ThreadSa feOperatio nRegistry. java:481)
at javax.media.jai.registry.R IFRegistry .create(RI FRegistry. java:340)
at javax.media.jai.RenderedOp .createIns tance(Rend eredOp.jav a:830)
at javax.media.jai.RenderedOp .createRen dering(Ren deredOp.ja va:878)
at javax.media.jai.RenderedOp .getColorM odel(Rende redOp.java :2253)
at myclass.getImage(ReadCMYKJ PG.java:13 1)
at myclass.main(ReadCMYKJPG.j ava:151)
And this happens a few times in a row.
Line 131 is
System.out.println(image.g etBounds() );
Any idea how to check the RenderedOp so that I do not call the methids if it is not ok?
Thanks in advance
Venabili
public int getImage(String fileName) {
try{
if (!fileName.toUpperCase().e
return 0;
}
RenderedOp image = JAI.create("fileload", fileName);
try{
System.out.println(image.g
System.out.println(image.g
}catch (Exception e){
return 0;
}
return 1;
}catch (Exception e){
e.printStackTrace();
return 0;
}
}
All is fine, the create operation is ok. But when reading bad JPEGs (0 bytes ones or things that are not JPEGS at all) , the invoking of the methods on the image object is throwing an Exception (getParameters() seems to be the only one working)
All the others throw:
Error: IOException occurs when search for propriate codecs.
Occurs in: com.sun.media.jai.codec.Im
java.io.EOFException
at com.sun.media.jai.codec.Se
at com.sun.media.jai.codec.Se
at com.sun.media.jai.codec.Im
at com.sun.media.jai.opimage.
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at javax.media.jai.FactoryCac
at javax.media.jai.OperationR
at javax.media.jai.ThreadSafe
at javax.media.jai.registry.R
at com.sun.media.jai.opimage.
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at javax.media.jai.FactoryCac
at javax.media.jai.OperationR
at javax.media.jai.ThreadSafe
at javax.media.jai.registry.R
at javax.media.jai.RenderedOp
at javax.media.jai.RenderedOp
at javax.media.jai.RenderedOp
at myclass.getImage(ReadCMYKJ
at myclass.main(ReadCMYKJPG.j
And this happens a few times in a row.
Line 131 is
System.out.println(image.g
Any idea how to check the RenderedOp so that I do not call the methids if it is not ok?
Thanks in advance
Venabili
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you post a link to the image?
ASKER
It's a little copyrighted and just now I have no software to produce another:(
But I think I solved it - once I handle correctly the 0 bytes ones, the JAI takes care for the others :)
So this works:
public static boolean isJPEG(File jpgFile) {
long length = jpgFile.length();
if (length < 1) {
return false;
}
return true;
}
public static BufferedImage getImage(String fileName) {
try{
if (!fileName.toUpperCase().e ndsWith(". JPG") && !fileName.toUpperCase().en dsWith(".J PEG")){
return null;
}
RenderedOp image = JAI.create("fileload", fileName);
BufferedImage result = null;
try {
if (isJPEG(new File(fileName))) {
if (image.getColorModel().get NumCompone nts() == 4) {
result = convertCMYKtoRGB(image);
}
} else {
return null;
}
} catch (Exception e) {
return null;
}
return result;
}catch (Exception e){
return null;
}
}
I need sleep :( and you just earned easy points:) Thanks for the help :)
But I think I solved it - once I handle correctly the 0 bytes ones, the JAI takes care for the others :)
So this works:
public static boolean isJPEG(File jpgFile) {
long length = jpgFile.length();
if (length < 1) {
return false;
}
return true;
}
public static BufferedImage getImage(String fileName) {
try{
if (!fileName.toUpperCase().e
return null;
}
RenderedOp image = JAI.create("fileload", fileName);
BufferedImage result = null;
try {
if (isJPEG(new File(fileName))) {
if (image.getColorModel().get
result = convertCMYKtoRGB(image);
}
} else {
return null;
}
} catch (Exception e) {
return null;
}
return result;
}catch (Exception e){
return null;
}
}
I need sleep :( and you just earned easy points:) Thanks for the help :)
:-) Make sure it still works when you rename X.gif to X.jpg ;-)
ASKER
Seems to be ok :)
Well you might just get back a BufferedImage containing a gif without complaint ;-)
ASKER
The part for the empty file is ok but the second part does not recognise the JPEG :)
Venabili