Image Size in pixels

I need to determine the image size in pixels.

I have tried loading the image from the file system using getDefaultToolkit() and then using getWidth on the Image object.

But the return value is always -1. Any ideas on this or a sample code that loads the image from the file system and then determines the width in pixels would be great.

Thanks

[370 byte] By [nganesh21a] at [2007-10-3 3:59:04]
# 1

Images are loaded asynchronously. This means, that your image isnt't already loaded when the method .getImage returns.

Return value of -1 means, that the width isn't known because the loading thread is still working.

Class MediaTracker has the capability to control and observe te loading.

These are only hints, not the real code:

int id = 0;

MediaTracker mt = new MediaTracker ( ... );

mt.addImage ( myImage, id ); // Adds image to MediaTracker

mt..checkID ( id, true );// Starts physical loading

mt.waitForID ( id);// Sleeps until image is loaded - needs try / catch

After processing this code .getWidth will return correct size.

Its also possible to add several images at the same time and load them concurrently.

harryBKAa at 2007-7-14 21:57:45 > top of Java-index,Security,Cryptography...
# 2
If asynchronous loading is not requirement you may want to look at ImageIO (javax.imageio).For tutorial see http://java.sun.com/j2se/1.5.0/docs/guide/imageio/spec/apps.fm1.htmlor http://javaalmanac.com/egs/javax.imageio/BasicImageRead.html
neigora at 2007-7-14 21:57:45 > top of Java-index,Security,Cryptography...