| java.lang.Object org.wings.util.ImageInfo
ImageInfo | public class ImageInfo (Code) | | Get file format, image resolution, number of bits per pixel and optionally
number of images, comments and physical resolution from
JPEG, GIF, BMP, PCX, PNG, IFF, RAS, PBM, PGM, PPM, PSD and SWF files
(or input streams).
Use the class like this:
ImageInfo ii = new ImageInfo();
ii.setInput(in); // in can be InputStream or RandomAccessFile
ii.setDetermineImageNumber(true); // default is false
ii.setCollectComments(true); // default is false
if (!ii.check()) {
System.err.println("Not a supported image file format.");
return;
}
System.out.println(ii.getFormatName() + ", " + ii.getMimeType() +
", " + ii.getWidth() + " x " + ii.getHeight() + " pixels, " +
ii.getBitsPerPixel() + " bits per pixel, " + ii.getNumberOfImages() +
" image(s), " + ii.getNumberOfComments() + " comment(s).");
// there are other properties, check out the API documentation
You can also use this class as a command line program.
Call it with a number of image file names and URLs as parameters:
java ImageInfo *.jpg *.png *.gif http://somesite.tld/image.jpg
or call it without parameters and pipe data to it:
java ImageInfo < image.jpg
Known limitations:
- When the determination of the number of images is turned off, GIF bits
per pixel are only read from the global header.
For some GIFs, local palettes change this to a typically larger
value. To be certain to get the correct color depth, call
setDetermineImageNumber(true) before calling check().
The complete scan over the GIF file will take additional time.
- Transparency information is not included in the bits per pixel count.
Actually, it was my decision not to include those bits, so it's a feature! ;-)
Requirements:
The latest version can be found at http://www.geocities.com/marcoschmidt.geo/image-info.html.
Written by Marco Schmidt.
This class is contributed to the Public Domain.
Use it at your own risk.
Last modification 2004-02-29.
History:
- 2001-08-24 Initial version.
- 2001-10-13 Added support for the file formats BMP and PCX.
- 2001-10-16 Fixed bug in read(int[], int, int) that returned
- 2002-01-22 Added support for file formats Amiga IFF and Sun Raster (RAS).
- 2002-01-24 Added support for file formats Portable Bitmap / Graymap / Pixmap (PBM, PGM, PPM) and Adobe Photoshop (PSD).
Added new method getMimeType() to return the MIME type associated with a particular file format.
- 2002-03-15 Added support to recognize number of images in file. Only works with GIF.
Use
ImageInfo.setDetermineImageNumber with
true as argument to identify animated GIFs
(
ImageInfo.getNumberOfImages() will return a value larger than 1 ).
- 2002-04-10 Fixed a bug in the feature 'determine number of images in animated GIF' introduced with version 1.1.
Thanks to Marcelo P. Lima for sending in the bug report.
Released as 1.1.1.
- 2002-04-18 Added
ImageInfo.setCollectComments(boolean) .
That new method lets the user specify whether textual comments are to be
stored in an internal list when encountered in an input image file / stream.
Added two methods to return the physical width and height of the image in dpi:
ImageInfo.getPhysicalWidthDpi() and
ImageInfo.getPhysicalHeightDpi() .
If the physical resolution could not be retrieved, these methods return
-1 .
- 2002-04-23 Added support for the new properties physical resolution and
comments for some formats. Released as 1.2.
- 2002-06-17 Added support for SWF, sent in by Michael Aird.
Changed checkJpeg() so that other APP markers than APP0 will not lead to a failure anymore.
Released as 1.3.
- 2003-07-28 Bug fix - skip method now takes return values into consideration.
Less bytes than necessary may have been skipped, leading to flaws in the retrieved information in some cases.
Thanks to Bernard Bernstein for pointing that out.
Released as 1.4.
- 2004-02-29 Added support for recognizing progressive JPEG and
interlaced PNG and GIF. A new method
ImageInfo.isProgressive() returns whether ImageInfo
has found that the storage type is progressive (or interlaced).
Thanks to Joe Germuska for suggesting the feature.
Bug fix: BMP physical resolution is now correctly determined.
Released as 1.5.
|
Method Summary | |
public boolean | check() Call this method after you have provided an input stream or file
using
ImageInfo.setInput(java.io.InputStream) or
ImageInfo.setInput(java.io.DataInput) . | public int | getBitsPerPixel() If
ImageInfo.check() was successful, returns the image's number of bits per pixel. | public String | getComment(int index) Returns the index'th comment retrieved from the image. | public int | getFormat() If
ImageInfo.check() was successful, returns the image format as one
of the FORMAT_xyz constants from this class. | public String | getFormatName() If
ImageInfo.check() was successful, returns the image format's name. | public int | getHeight() If
ImageInfo.check() was successful, returns one the image's vertical
resolution in pixels. | public String | getMimeType() If
ImageInfo.check() was successful, returns a String with the
MIME type of the format.
MIME type, e.g. | public int | getNumberOfComments() If
ImageInfo.check() was successful and
ImageInfo.setCollectComments(boolean) was called with
true as argument, returns the number of comments retrieved
from the input image stream / file. | public int | getNumberOfImages() Returns the number of images in the examined file. | public int | getPhysicalHeightDpi() Returns the physical height of this image in dots per inch (dpi). | public float | getPhysicalHeightInch() If
ImageInfo.check() was successful, returns the physical width of this image in dpi (dots per inch)
or -1 if no value could be found. | public int | getPhysicalWidthDpi() If
ImageInfo.check() was successful, returns the physical width of this image in dpi (dots per inch)
or -1 if no value could be found. | public float | getPhysicalWidthInch() Returns the physical width of an image in inches, or
-1.0f if width information is not available. | public int | getWidth() If
ImageInfo.check() was successful, returns one the image's horizontal
resolution in pixels. | public boolean | isProgressive() Returns whether the image is stored in a progressive (also called: interlaced) way. | public static void | main(String[] args) To use this class as a command line application, give it either
some file names as parameters (information on them will be
printed to standard output, one line per file) or call
it with no parameters. | public void | setCollectComments(boolean newValue) Specify whether textual comments are supposed to be extracted from input. | public void | setDetermineImageNumber(boolean newValue) Specify whether the number of images in a file is to be
determined - default is false . | public void | setInput(DataInput dataInput) Set the input stream to the argument stream (or file). | public void | setInput(InputStream inputStream) Set the input stream to the argument stream (or file). |
COLOR_TYPE_BLACK_AND_WHITE | final public static int COLOR_TYPE_BLACK_AND_WHITE(Code) | | |
COLOR_TYPE_GRAYSCALE | final public static int COLOR_TYPE_GRAYSCALE(Code) | | |
COLOR_TYPE_PALETTED | final public static int COLOR_TYPE_PALETTED(Code) | | |
COLOR_TYPE_TRUECOLOR_RGB | final public static int COLOR_TYPE_TRUECOLOR_RGB(Code) | | |
COLOR_TYPE_UNKNOWN | final public static int COLOR_TYPE_UNKNOWN(Code) | | |
FORMAT_BMP | final public static int FORMAT_BMP(Code) | | Return value of
ImageInfo.getFormat() for BMP streams.
BMP only supports one image per file.
BMP does not allow for comments.
The physical resolution can be stored.
|
FORMAT_GIF | final public static int FORMAT_GIF(Code) | | Return value of
ImageInfo.getFormat() for GIF streams.
ImageInfo can extract comments from GIFs and count the number
of images (GIFs with more than one image are animations).
If you know of a place where GIFs store the physical resolution
of an image, please
send me a mail!
It is determined whether the GIF stream is interlaced (see
ImageInfo.isProgressive() ).
|
FORMAT_JPEG | final public static int FORMAT_JPEG(Code) | | Return value of
ImageInfo.getFormat() for JPEG streams.
ImageInfo can extract physical resolution and comments
from JPEGs (only from APP0 headers).
Only one image can be stored in a file.
It is determined whether the JPEG stream is progressive
(see
ImageInfo.isProgressive() ).
|
FORMAT_NAMES | final public static String[] FORMAT_NAMES(Code) | | The names of all supported file formats.
The FORMAT_xyz int constants can be used as index values for
this array.
|
FORMAT_PCX | final public static int FORMAT_PCX(Code) | | Return value of
ImageInfo.getFormat() for PCX streams.
PCX does not allow for comments or more than one image per file.
However, the physical resolution can be stored.
|
FORMAT_PNG | final public static int FORMAT_PNG(Code) | | Return value of
ImageInfo.getFormat() for PNG streams.
PNG only supports one image per file.
Both physical resolution and comments can be stored with PNG,
but ImageInfo is currently not able to extract those.
It is determined whether the PNG stream is interlaced (see
ImageInfo.isProgressive() ).
|
FORMAT_RAS | final public static int FORMAT_RAS(Code) | | Return value of
ImageInfo.getFormat() for RAS streams.
Sun Raster allows for one image per file only and is not able to
store physical resolution or comments.
|
MIME_TYPE_STRINGS | final public static String[] MIME_TYPE_STRINGS(Code) | | The names of the MIME types for all supported file formats.
The FORMAT_xyz int constants can be used as index values for
this array.
|
getBitsPerPixel | public int getBitsPerPixel()(Code) | | If
ImageInfo.check() was successful, returns the image's number of bits per pixel.
Does not include transparency information like the alpha channel.
number of bits per image pixel |
getFormat | public int getFormat()(Code) | | If
ImageInfo.check() was successful, returns the image format as one
of the FORMAT_xyz constants from this class.
Use
ImageInfo.getFormatName() to get a textual description of the file format.
file format as a FORMAT_xyz constant |
getHeight | public int getHeight()(Code) | | If
ImageInfo.check() was successful, returns one the image's vertical
resolution in pixels.
image height in pixels |
getMimeType | public String getMimeType()(Code) | | If
ImageInfo.check() was successful, returns a String with the
MIME type of the format.
MIME type, e.g. image/jpeg |
getNumberOfImages | public int getNumberOfImages()(Code) | | Returns the number of images in the examined file.
Assumes that setDetermineImageNumber(true); was called before
a successful call to
ImageInfo.check() .
This value can currently be only different from 1 for GIF images.
number of images in file |
getWidth | public int getWidth()(Code) | | If
ImageInfo.check() was successful, returns one the image's horizontal
resolution in pixels.
image width in pixels |
isProgressive | public boolean isProgressive()(Code) | | Returns whether the image is stored in a progressive (also called: interlaced) way.
true for progressive/interlaced, false otherwise |
main | public static void main(String[] args)(Code) | | To use this class as a command line application, give it either
some file names as parameters (information on them will be
printed to standard output, one line per file) or call
it with no parameters. It will then check data given to it
via standard input.
Parameters: args - the program arguments which must be file names |
setCollectComments | public void setCollectComments(boolean newValue)(Code) | | Specify whether textual comments are supposed to be extracted from input.
Default is false .
If enabled, comments will be added to an internal list.
Parameters: newValue - if true , this class will read comments See Also: ImageInfo.getNumberOfComments See Also: ImageInfo.getComment |
setDetermineImageNumber | public void setDetermineImageNumber(boolean newValue)(Code) | | Specify whether the number of images in a file is to be
determined - default is false .
This is a special option because some file formats require running over
the entire file to find out the number of images, a rather time-consuming
task.
Not all file formats support more than one image.
If this method is called with true as argument,
the actual number of images can be queried via
ImageInfo.getNumberOfImages() after a successful call to
ImageInfo.check() .
Parameters: newValue - will the number of images be determined? See Also: ImageInfo.getNumberOfImages |
setInput | public void setInput(InputStream inputStream)(Code) | | Set the input stream to the argument stream (or file).
Parameters: inputStream - the input stream to read from |
|
|