2 Replies Latest reply: Jun 9, 2010 1:50 PM by 843802 RSS

    Image size from header

    800606
      Hi,
      I'm trying to extract image size from header for PNG, JPG, TIFF, BMP and GIF files. So far I got PNG working. I'm having problems finding size info for JPG files. Anyone knows where I can read about it (i didnt find nothing on google) or have the code that works? also for the other image types?
      P.S. I dont wanna read the entire image using Image class cause its too slow.
        • 1. Re: Image size from header
          843802
          Try ImageReader's getWidth, and getHeight. The values are returned from the headers, and the methods work for all the supported formats. You can use the method that takes the file extension (png, jpg, tiff) to obtain a reader.

          Claudiu
          • 2. Re: Image size from header
            843802
            Code Snippet:
            ImageInputStream imageStream = 
                    ImageIO.createImageInputStream(/*File or InputStream*/);
             
            java.util.Iterator<ImageReader> readers = 
                    ImageIO.getImageReaders(imageStream);
             
            ImageReader reader = null;
            if(readers.hasNext()) {
                reader = readers.next();
            }else {
                imageStream.close();
                //can't read image format... what do you want to do about it,
                //throw an exception, return ?
            }
             
            reader.setInput(imageStream,true,true);
             
            int imageWidth = reader.getWidth(0);
            int imageHeight = reader.getHeight(0);
             
            reader.dispose();
            imageStream.close();