Re: getting img dimensions..
 
Remi Arntzen wrote:
maya wrote:
can you do with java what you can do with PHP as described here?
http://us2.php.net/manual/en/function.getimagesize.php
 > >
namely, get image-dimensions..  is there a way to do this w/Java ON THE
SERVER, not in an applet or swing app..
thank you..
java.awt.image.BufferedImage image =
javax.imageio.ImageIO.read(java.io.File);
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
Although this works, it is a bit heavy handed. There's no reason to
read the whole image into memory. Try something like this instead:
        /**
     * Attempts to read image width and height from file.
     *
     * @param file File that contains image
     * @return image dimensions or null if file does not contain
     * an image or an error occurs while reading file.
     */
    public static Dimension readDimensions(File file)
    {
        ImageInputStream iis = null;
        ImageReader reader = null;
        try
        {
            iis = new FileImageInputStream(file);
            Iterator it = ImageIO.getImageReaders(iis);
            if (!it.hasNext())
                return null;
            reader = (ImageReader) it.next();
            reader.setInput(iis, true, true);
            return new Dimension(reader.getWidth(0), reader.getHeight(0));
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            try
            {
                if (reader != null)
                    reader.dispose();
                if (iis != null)
                    iis.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
Regards,
Daniel Sj=F6blom