How to get the formatName of an image?

Hi,

I'm trying to write a little class which gets as input an image file (gif, jpg or png) and outputs a resized version of it. My aim is to output allways an image having the same format like the input one. I saw that ImageIO.write accepts as parameter the String formatName.

The question is how can somebody get the formatName for the input-image?

Thanks,

IMIA

[394 byte] By [IMIAa] at [2007-11-27 11:35:06]
# 1

What would the input be, precisely?

BigDaddyLoveHandlesa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 2

As input we have all kind of images uploaded by visitors over a web interface. It could be everything.

IMIAa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 3

> As input we have all kind of images uploaded by

> visitors over a web interface. It could be everything.

Too vague. I'll try again. Give my the signature of the method you are trying to write.

BigDaddyLoveHandlesa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 4

I'd like to have a method like this:

public static String resizeImg(String imgPath, Integer newWidth, Integer newHeight)

It gets as parameter the absolutePath to an image and the new width&height; it should resize the image and return me the absolute path to the resized image.

What I want is that in the case the imgPath points to a gif image the resized image should be a gif as well. If it's a jpg the resized img should be a jpg as well. The only one pb I have is to find out inside the resizeImg what format the imgPath image has, the rest would be clear.

Thanks for your help.

IMIAa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 5

Would you be happy with the file's suffix, for example:

C:/temp/feist.png

has suffix "png". Or are you concerned that jpeg file might be given suffix gif?

In that case you have to examine the first few bytes to determine the file format.

BigDaddyLoveHandlesa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 6

Yes, the file suffix can be manipulated, I would avoid using it.

It's nothing in Java which'd help in this case?

IMIAa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 7

> It's nothing in Java which'd help in this case?

Help get the suffix of a string or help sniff the format of an input stream?

BigDaddyLoveHandlesa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 8

I think nobody has a problem with String operations. The interesting problem is how to detect if an input stream it's a gif or a jpeg or a png?

IMIAa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...
# 9

Found a nice example how to solve the problem using the javax.imageio package.

The ideea behind is:

S1) Getting an ImageInputStream for our image file:

ImageInputStream iis = ImageIO.createImageInputStream(o);

S2) Getting all image-readers which are able to read the format:

Iterator iter = ImageIO.getImageReaders(iis);

S3) Get the first reader and call its getFormatName()

The full example is available here:

http://www.exampledepot.com/egs/javax.imageio/DiscType.html

Seems to work fine for me.

Thanks for all your help!

IMIAa at 2007-7-29 17:00:56 > top of Java-index,Java Essentials,New To Java...