PDA

View Full Version : How to find image type, size, width and height in Java?


echarcha
December 26th, 2006, 04:29 PM
I am uploading a image (JPG or GIF) using a JSP page and a servlet to a webserver.

I can get the file size of the image and determine if it is less than or equal to the restrictions I have put.

However, I wish to determine the following -
Type of image - GIF, JPEG, PNG.
Attributes of image - width in pixels, height in pixels.

Upon searching the net, I came across Java Advanced Imaging API or JAI for short. However, it has so many things and I just need these basic simple things. Is there an alternative way?

echarcha
December 26th, 2006, 09:40 PM
I had used ImageMagic long ago but its not written in Java. Though there is a JMagick plugin to allow Java code to access it, but its overkill for the simple tasks I need to do.

GunsNRoses
December 26th, 2006, 09:52 PM
Already told echarcha this over IM, but I figure a link here wouldn't hurt as well, in case someone else is looking for the same solution.
http://schmidt.devlib.org/image-info/

echarcha
December 26th, 2006, 09:59 PM
Thanks a lot GnR.. Good that you posted this link..

I searched on Google today for so long and all it came up with was sites showing how to use JAI and some other commercial libraries! :(

Thanks for this link....

krantikari
December 28th, 2006, 04:07 PM
Already told echarcha this over IM, but I figure a link here wouldn't hurt as well, in case someone else is looking for the same solution.
http://schmidt.devlib.org/image-info/


Nice link GNR. By the way eCharcha, if you are by chance uploading photos clicked by digital cameras and displaying them onthe screen or generating report, you can really expect trouble from some notorious cameras that do not generate JPEG images confirming to JFIF (JPEG File interchange format) standards. Please do follow the links provided in the page sent by GNR for getting dimensions and resolution of those kind of images.... but to display them on the screen or put them in a report... you can still expect pain in the wrong place even if you find the correct dimension and resolution of those images. If those images are of higher resolution they can grow big out of the screen and you will seem to have no control on them. I had deviced a method to correct such images that takes care of this problem quite somee time ago. I will post the document when I find it. But do let me know if your requirement is somewhat like this.

echarcha
December 28th, 2006, 09:17 PM
Yes please krantikari.. This is a situation I am going to face.. Please post..

krantikari
December 30th, 2006, 05:47 PM
I will post it soon. I need to locate in which hard disk it is.

krantikari
December 30th, 2006, 06:14 PM
I have attached the document. Yon can first try to correct problem images manually using any Hexadecimal editor and try. Lots of free hex editors are available. Then you need to programatically identify the problem images and apply the correction. Sorry I do nto have the code as I had only given the solution and POC by correcting some images manually. Implementation was done by some other team.

Ravi
December 31st, 2006, 11:20 PM
Great question and great answers.
If I see one such thread, how can I make it a favorite in eCharcha, instead of bookmarking it in browser?

AmthaLal
January 1st, 2007, 09:09 AM
Great question and great answers.
If I see one such thread, how can I make it a favorite in eCharcha, instead of bookmarking it in browser?



You can subscribe perticular thraeds.;)

echarcha
January 2nd, 2007, 01:15 PM
Thanks for all the responses. I think the link which GnR gave solves my problem of detecting type of image (gif, jpg, etc). Kranti, your answers also helped. However, the whole process has to be automatic and hence I will modify the ImageInfo class to look for special headers for JPGs generated by digital camcorders and digital still cameras.

Thanks again... Ravi. just subscribe to a thread to 'bookmark' it.