Package sk.iway.iwcm.gallery
Class ImageInfo
java.lang.Object
sk.iway.iwcm.gallery.ImageInfo
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()) { Logger.error(this,"Not a supported image file format."); return; } Logger.println(ImageInfo.class,ii.getFormatName() + ", " + ii.getMimeType() + ", " + ii.getWidth() + " x " + ii.getHeight() + " pixels, " + ii.getBitsPerPixel() + " bits per pixel, " + ii.getNumberOfImages() + " image(s), " + ii.getNumberOfComments() + " comment(s).");You can also use this class as a command line program. Call it with a number of image file names as parameters:
java ImageInfo *.jpg *.png *.gifor call it without parameters and pipe data to it:
cat image.jpg | java ImageInfo
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:
- Java 1.1 or higher
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 2002-06-17.
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
setDetermineImageNumber(boolean)
withtrue
as argument to identify animated GIFs (getNumberOfImages()
will return a value larger than1
). - 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
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:getPhysicalWidthDpi()
andgetPhysicalHeightDpi()
. 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Return value ofgetFormat()
for BMP streams.static final int
Return value ofgetFormat()
for GIF streams.static final int
Return value ofgetFormat()
for IFF streams.static final int
Return value ofgetFormat()
for JPEG streams.static final int
Return value ofgetFormat()
for PBM streams.static final int
Return value ofgetFormat()
for PCX streams.static final int
Return value ofgetFormat()
for PGM streams.static final int
Return value ofgetFormat()
for PNG streams.static final int
Return value ofgetFormat()
for PPM streams.static final int
Return value ofgetFormat()
for PSD streams.static final int
Return value ofgetFormat()
for RAS streams.static final int
Return value ofgetFormat()
for SWF (Shockwave) streams. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
check()
Call this method after you have provided an input stream or file usingsetInput(InputStream)
orsetInput(DataInput)
.int
Ifcheck()
was successful, returns the image's number of bits per pixel.getComment
(int index) Returns the index'th comment retrieved from the image.int
Ifcheck()
was successful, returns the image format as one of the FORMAT_xyz constants from this class.Ifcheck()
was successful, returns the image format's name.int
Ifcheck()
was successful, returns one the image's vertical resolution in pixels.Ifcheck()
was successful, returns a String with the MIME type of the format.int
Ifcheck()
was successful andsetCollectComments(boolean)
was called withtrue
as argument, returns the number of comments retrieved from the input image stream / file.int
Returns the number of images in the examined file.int
Returns the physical height of this image in dots per inch (dpi).float
Ifcheck()
was successful, returns the physical width of this image in dpi (dots per inch) or -1 if no value could be found.int
Ifcheck()
was successful, returns the physical width of this image in dpi (dots per inch) or -1 if no value could be found.float
Returns the physical width of an image in inches, or-1.0f
if width information is not available.int
getWidth()
Ifcheck()
was successful, returns one the image's horizontal resolution in pixels.long
readUBits
(int numBits) Read an unsigned value from the given number of bitsvoid
setCollectComments
(boolean newValue) Specify whether textual comments are supposed to be extracted from input.void
setDetermineImageNumber
(boolean newValue) Specify whether the number of images in a file is to be determined - default isfalse
.void
Set the input stream to the argument stream (or file).void
setInput
(InputStream inputStream) Set the input stream to the argument stream (or file).void
Reset the bit buffer
-
Field Details
-
FORMAT_JPEG
public static final int FORMAT_JPEGReturn value ofgetFormat()
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.- See Also:
-
FORMAT_GIF
public static final int FORMAT_GIFReturn value ofgetFormat()
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!- See Also:
-
FORMAT_PNG
public static final int FORMAT_PNGReturn value ofgetFormat()
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.- See Also:
-
FORMAT_BMP
public static final int FORMAT_BMPReturn value ofgetFormat()
for BMP streams. BMP only supports one image per file. BMP does not allow for comments. The physical resolution can be stored. The specification that I have says that the values must be interpreted as dots per meter. However, given that I only encounter typical dpi values like 72 or 300, I currently consider those values dpi. Maybe someone can shed some light on this, please send me a mail in that case.- See Also:
-
FORMAT_PCX
public static final int FORMAT_PCXReturn value ofgetFormat()
for PCX streams. PCX does not allow for comments or more than one image per file. However, the physical resolution can be stored.- See Also:
-
FORMAT_IFF
public static final int FORMAT_IFFReturn value ofgetFormat()
for IFF streams.- See Also:
-
FORMAT_RAS
public static final int FORMAT_RASReturn value ofgetFormat()
for RAS streams. Sun Raster allows for one image per file only and is not able to store physical resolution or comments.- See Also:
-
FORMAT_PBM
public static final int FORMAT_PBMReturn value ofgetFormat()
for PBM streams.- See Also:
-
FORMAT_PGM
public static final int FORMAT_PGMReturn value ofgetFormat()
for PGM streams.- See Also:
-
FORMAT_PPM
public static final int FORMAT_PPMReturn value ofgetFormat()
for PPM streams.- See Also:
-
FORMAT_PSD
public static final int FORMAT_PSDReturn value ofgetFormat()
for PSD streams.- See Also:
-
FORMAT_SWF
public static final int FORMAT_SWFReturn value ofgetFormat()
for SWF (Shockwave) streams.- See Also:
-
-
Constructor Details
-
ImageInfo
public ImageInfo() -
ImageInfo
-
ImageInfo
-
ImageInfo
public ImageInfo(sk.iway.upload.UploadedFile file) -
ImageInfo
-
-
Method Details
-
check
public boolean check()Call this method after you have provided an input stream or file usingsetInput(InputStream)
orsetInput(DataInput)
. If true is returned, the file format was known and you information about its content can be retrieved using the various getXyz methods.- Returns:
- if information could be retrieved from input
-
getBitsPerPixel
public int getBitsPerPixel()Ifcheck()
was successful, returns the image's number of bits per pixel. Does not include transparency information like the alpha channel.- Returns:
- number of bits per image pixel
-
getComment
Returns the index'th comment retrieved from the image.- Throws:
IllegalArgumentException
- if index is smaller than 0 or larger than or equal to the number of comments retrieved- See Also:
-
getFormat
public int getFormat()Ifcheck()
was successful, returns the image format as one of the FORMAT_xyz constants from this class. UsegetFormatName()
to get a textual description of the file format.- Returns:
- file format as a FORMAT_xyz constant
-
getFormatName
Ifcheck()
was successful, returns the image format's name. UsegetFormat()
to get a unique number.- Returns:
- file format name
-
getHeight
public int getHeight()Ifcheck()
was successful, returns one the image's vertical resolution in pixels.- Returns:
- image height in pixels
-
getMimeType
Ifcheck()
was successful, returns a String with the MIME type of the format.- Returns:
- MIME type, e.g.
image/jpeg
-
getNumberOfComments
public int getNumberOfComments()Ifcheck()
was successful andsetCollectComments(boolean)
was called withtrue
as argument, returns the number of comments retrieved from the input image stream / file. Any number >= 0 and smaller than this number of comments is then a valid argument for thegetComment(int)
method.- Returns:
- number of comments retrieved from input image
-
getNumberOfImages
public int getNumberOfImages()Returns the number of images in the examined file. Assumes thatsetDetermineImageNumber(true);
was called before a successful call tocheck()
. This value can currently be only different from1
for GIF images.- Returns:
- number of images in file
-
getPhysicalHeightDpi
public int getPhysicalHeightDpi()Returns the physical height of this image in dots per inch (dpi). Assumes thatcheck()
was successful. Returns-1
on failure.- Returns:
- physical height (in dpi)
- See Also:
-
getPhysicalHeightInch
public float getPhysicalHeightInch()Ifcheck()
was successful, returns the physical width of this image in dpi (dots per inch) or -1 if no value could be found.- Returns:
- physical height (in dpi)
- See Also:
-
getPhysicalWidthDpi
public int getPhysicalWidthDpi()Ifcheck()
was successful, returns the physical width of this image in dpi (dots per inch) or -1 if no value could be found.- Returns:
- physical width (in dpi)
- See Also:
-
getPhysicalWidthInch
public float getPhysicalWidthInch()Returns the physical width of an image in inches, or-1.0f
if width information is not available. Assumes thatcheck()
has been called successfully.- Returns:
- physical width in inches or
-1.0f
on failure - See Also:
-
getWidth
public int getWidth()Ifcheck()
was successful, returns one the image's horizontal resolution in pixels.- Returns:
- image width in pixels
-
readUBits
Read an unsigned value from the given number of bits- Throws:
IOException
-
synchBits
public void synchBits()Reset the bit buffer -
setCollectComments
public void setCollectComments(boolean newValue) Specify whether textual comments are supposed to be extracted from input. Default isfalse
. If enabled, comments will be added to an internal list.- Parameters:
newValue
- iftrue
, this class will read comments- See Also:
-
setDetermineImageNumber
public void setDetermineImageNumber(boolean newValue) Specify whether the number of images in a file is to be determined - default isfalse
. 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 withtrue
as argument, the actual number of images can be queried viagetNumberOfImages()
after a successful call tocheck()
.- Parameters:
newValue
- will the number of images be determined?- See Also:
-
setInput
Set the input stream to the argument stream (or file). Note thatRandomAccessFile
implementsDataInput
.- Parameters:
dataInput
- the input stream to read from
-
setInput
Set the input stream to the argument stream (or file).- Parameters:
inputStream
- the input stream to read from
-