Solved

JAI: Reading bad JPEGs

Posted on 2004-09-30
7
1,635 Views
Last Modified: 2012-06-27
Here is the code I use:
    public int getImage(String fileName) {
        try{
            if (!fileName.toUpperCase().endsWith(".JPG") && !fileName.toUpperCase().endsWith(".JPEG")){
                return 0;
            }
          RenderedOp image = JAI.create("fileload", fileName);
            try{
                System.out.println(image.getParameters());
                System.out.println(image.getBounds());
            }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.ImageCodec

java.io.EOFException
      at com.sun.media.jai.codec.SeekableStream.readFully(SeekableStream.java:329)
      at com.sun.media.jai.codec.SeekableStream.readFully(SeekableStream.java:306)
      at com.sun.media.jai.codec.ImageCodec.getDecoderNames(ImageCodec.java:274)
      at com.sun.media.jai.opimage.StreamRIF.create(StreamRIF.java:80)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
      at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
      at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
      at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
      at com.sun.media.jai.opimage.FileLoadRIF.create(FileLoadRIF.java:122)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at javax.media.jai.FactoryCache.invoke(FactoryCache.java:130)
      at javax.media.jai.OperationRegistry.invokeFactory(OperationRegistry.java:1682)
      at javax.media.jai.ThreadSafeOperationRegistry.invokeFactory(ThreadSafeOperationRegistry.java:481)
      at javax.media.jai.registry.RIFRegistry.create(RIFRegistry.java:340)
      at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:830)
      at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
      at javax.media.jai.RenderedOp.getColorModel(RenderedOp.java:2253)
      at myclass.getImage(ReadCMYKJPG.java:131)
      at myclass.main(ReadCMYKJPG.java:151)
And this happens a few times in a row.

Line 131 is                
System.out.println(image.getBounds());

Any idea how to check the RenderedOp so that I do not call the methids if it is not ok?

Thanks in advance
Venabili
0
Comment
Question by:Venabili
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12192647
>>(0 bytes ones or things that are not JPEGS at all)

Try this:

      public static boolean isJPEG(File jpgFile) {
            long length = jpgFile.length();
            if (length < 1) {
                  return false;
            }
            FileInputStream in = null;
            final int HEAD_LENGTH = 10;
            byte[] header = new byte[HEAD_LENGTH];
            try {
                  in = new FileInputStream(jpgFile);
                  in.read(header);
                  return "JFIF".equals(new String(header, 6, 4));
            }
            catch (IOException e) {
                  e.printStackTrace();
                  return false;
            }
            finally {
                  try {
                        if (in != null) {
                              in.close();
                        }
                  }
                  catch(IOException e) {
                        /* ignore */
                  }
            }
      }
0
 
LVL 20

Author Comment

by:Venabili
ID: 12192867
It does not recognise a valid CMYK JPEG.
The part for the empty file is ok but the second part does not recognise the JPEG :)

Venabili
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12192933
Can you post a link to the image?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 20

Author Comment

by:Venabili
ID: 12193005
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().endsWith(".JPG") && !fileName.toUpperCase().endsWith(".JPEG")){
                return null;
            }
            RenderedOp image = JAI.create("fileload", fileName);
            BufferedImage result = null;
            try {
                if (isJPEG(new File(fileName))) {
                    if (image.getColorModel().getNumComponents() == 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 :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12193147
:-) Make sure it still works when you rename X.gif to X.jpg ;-)
0
 
LVL 20

Author Comment

by:Venabili
ID: 12193217
Seems to be ok :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12193263
Well you might just get back a BufferedImage containing a gif without complaint ;-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 70
topping2 challenge 13 61
word0 challenge 3 58
Eclipse IDE - Cannot copy/paste from console output 8 70
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now