Skip to main content

how to retrieve image type from a file

2 replies [Last post]
bucsie
Offline
Joined: 2008-07-03

Hi

In a small app I'm developing I need to get a tiff image file, extract some info (image format, compression) and then display it in a web page. Even if it has the proper extension (eg: tiff) i cannot be sure that the file is actually a tiff file. So I would need to validate somehow that indeed the file is of a certain format.

How can I do that?
calling this on a tiff (or jpeg file)
ImageInputStream iis = ImageIO.createImageInputStream(inputStream); //input stream is a file input stream
Iterator readerIterator = ImageIO.getImageReaders(iis);
if (readerIterator.hasNext()) {
ImageReader reader = readerIterator.next();
reader.getFormatName();
}
results in an iterator with hasNext()==false.
I'm not even sure that getFormatName is really what I need.

So - how can I get an image file's properties - type, compression algorithm?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Harald Kuhr

Hi bucsie,

It's very probable that you don't have an ImageReader(Spi) registered
for TIFF in your VM. I think that's only included by default in
Apple's JRE. If you have JAI-ImageIO installed, you should have a TIFF
ImageReader available, and your code should work. If you have the same
problem with JPEG, it seems that there is something very wrong going on.

You can see what readers you have installed at runtime calling
ImageIO.getReaderFormatNames().

The properties you are interested in, is probably available in the
stream/image meta data. Good luck! :-)

That said, if all you want to do is to check wether a file is a TIFF,
using ImageIO might be a little overkill.

--
Harald K

On 3. juli. 2008, at 15.09, jai-imageio@javadesktop.org wrote:

> Hi
>
> In a small app I'm developing I need to get a tiff image file,
> extract some info (image format, compression) and then display it in
> a web page. Even if it has the proper extension (eg: tiff) i cannot
> be sure that the file is actually a tiff file. So I would need to
> validate somehow that indeed the file is of a certain format.
>
> How can I do that?
> calling this on a tiff (or jpeg file)
> ImageInputStream iis =
> ImageIO.createImageInputStream(inputStream); //input stream is a
> file input stream
> Iterator readerIterator = ImageIO.getImageReaders(iis);
> if (readerIterator.hasNext()) {
> ImageReader reader = readerIterator.next();
> reader.getFormatName();
> }
> results in an iterator with hasNext()==false.
> I'm not even sure that getFormatName is really what I need.
>
> So - how can I get an image file's properties - type, compression
> algorithm?
> [Message sent by forum member 'bucsie' (bucsie)]
>
> http://forums.java.net/jive/thread.jspa?messageID=284153
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

bucsie
Offline
Joined: 2008-07-03

thank you for replying.
the formats are ok now (though i did nothing else but call ImageIO.getReaderFormatNames(). before asking for the specific reader.)

I need to check whether the image is actually a tiff

and this does the trick
ImageReader reader = (ImageReader) iter.next();
String format = reader.getFormatName();

and then I have to check if it's b/w, greyscale or color

and if it's TIFF 6 what compression algorithm was used ( CCITT T.4 , CCITT T.6, LZW ...)

for this I noticed in debug mode that ImageReader has

imageReadParam.allowedTagSets []allowedTagsByName
[5] TreeMap$Entry: key "Compression" (id=15884)
value BaselineTIFFTagSet$Compression (3=CCITT T.4, 4=CCITT T.6, 5=LZW, 6=Old JPEG..)

but i can't figure out how to retrieve the corresponding data for this image.

any suggestions are welcome

Message was edited by: bucsie