|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ecar.pojo.ImageInfo
public class 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 and PSD 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 documentationYou 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.jpgor call it without parameters and pipe data to it:
java ImageInfo < image.jpg
Known limitations:
Requirements:
The latest version can be found at http://schmidt.devlib.org/image-info/.
Written by Marco Schmidt.
This class is contributed to the Public Domain. Use it at your own risk.
Last modification 2005-07-26.
setDetermineImageNumber(boolean)
with
true
as argument to identify animated GIFs (
getNumberOfImages()
will return a value larger than 1
).
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()
and getPhysicalHeightDpi()
. If
the physical resolution could not be retrieved, these methods return
-1
.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.
Field Summary | |
---|---|
private int |
bitBuf
|
private int |
bitPos
|
private int |
bitsPerPixel
|
private boolean |
collectComments
|
private java.util.Vector |
comments
|
private boolean |
determineNumberOfImages
|
private java.io.DataInput |
din
|
private int |
format
|
static int |
FORMAT_BMP
Return value of getFormat() for BMP streams. |
static int |
FORMAT_GIF
Return value of getFormat() for GIF streams. |
static int |
FORMAT_IFF
Return value of getFormat() for IFF streams. |
static int |
FORMAT_JPEG
Return value of getFormat() for JPEG streams. |
private static java.lang.String[] |
FORMAT_NAMES
The names of all supported file formats. |
static int |
FORMAT_PBM
Return value of getFormat() for PBM streams. |
static int |
FORMAT_PCX
Return value of getFormat() for PCX streams. |
static int |
FORMAT_PGM
Return value of getFormat() for PGM streams. |
static int |
FORMAT_PNG
Return value of getFormat() for PNG streams. |
static int |
FORMAT_PPM
Return value of getFormat() for PPM streams. |
static int |
FORMAT_PSD
Return value of getFormat() for PSD streams. |
static int |
FORMAT_RAS
Return value of getFormat() for RAS streams. |
private int |
height
|
private java.io.InputStream |
in
|
private static java.lang.String[] |
MIME_TYPE_STRINGS
The names of the MIME types for all supported file formats. |
private int |
numberOfImages
|
private int |
physicalHeightDpi
|
private int |
physicalWidthDpi
|
private boolean |
progressive
|
private int |
width
|
Constructor Summary | |
---|---|
ImageInfo()
|
Method Summary | |
---|---|
private void |
addComment(java.lang.String s)
|
boolean |
check()
Call this method after you have provided an input stream or file using setInput(InputStream) or setInput(DataInput) . |
private boolean |
checkBmp()
|
private boolean |
checkGif()
|
private boolean |
checkIff()
|
private boolean |
checkJpeg()
|
private boolean |
checkPcx()
|
private boolean |
checkPng()
|
private boolean |
checkPnm(int id)
|
private boolean |
checkPsd()
|
private boolean |
checkRas()
|
private static boolean |
determineVerbosity(java.lang.String[] args)
Run over String list, return false iff at least one of the arguments equals -c . |
private static boolean |
equals(byte[] a1,
int offs1,
byte[] a2,
int offs2,
int num)
|
int |
getBitsPerPixel()
If check() was successful, returns the image's number of bits per
pixel. |
java.lang.String |
getComment(int index)
Returns the index'th comment retrieved from the file. |
int |
getFormat()
If check() was successful, returns the image format as one of the
FORMAT_xyz constants from this class. |
java.lang.String |
getFormatName()
If check() was successful, returns the image format's name. |
int |
getHeight()
If check() was successful, returns one the image's vertical
resolution in pixels. |
private static int |
getIntBigEndian(byte[] a,
int offs)
|
private static int |
getIntLittleEndian(byte[] a,
int offs)
|
java.lang.String |
getMimeType()
If check() was successful, returns a String with the MIME type of
the format. |
int |
getNumberOfComments()
If check() was successful and setCollectComments(boolean)
was called with true as argument, returns the number of
comments retrieved from the input image stream / file. |
int |
getNumberOfImages()
Returns the number of images in the examined file. |
int |
getPhysicalHeightDpi()
Returns the physical height of this image in dots per inch (dpi). |
float |
getPhysicalHeightInch()
If check() was successful, returns the physical width of this
image in dpi (dots per inch) or -1 if no value could be found. |
int |
getPhysicalWidthDpi()
If check() was successful, returns the physical width of this
image in dpi (dots per inch) or -1 if no value could be found. |
float |
getPhysicalWidthInch()
Returns the physical width of an image in inches, or -1.0f if
width information is not available. |
private static int |
getShortBigEndian(byte[] a,
int offs)
|
private static int |
getShortLittleEndian(byte[] a,
int offs)
|
int |
getWidth()
If check() was successful, returns one the image's horizontal
resolution in pixels. |
boolean |
isProgressive()
Returns whether the image is stored in a progressive (also called: interlaced) way. |
static void |
main(java.lang.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. |
private static void |
print(java.lang.String sourceName,
ImageInfo ii,
boolean verbose)
|
private static void |
printCompact(java.lang.String sourceName,
ImageInfo imageInfo)
|
private static void |
printLine(int indentLevels,
java.lang.String text,
float value,
float minValidValue)
|
private static void |
printLine(int indentLevels,
java.lang.String text,
int value,
int minValidValue)
|
private static void |
printLine(int indentLevels,
java.lang.String text,
java.lang.String value)
|
private static void |
printVerbose(java.lang.String sourceName,
ImageInfo ii)
|
private int |
read()
|
private int |
read(byte[] a)
|
private int |
read(byte[] a,
int offset,
int num)
|
private java.lang.String |
readLine()
|
private java.lang.String |
readLine(java.lang.StringBuffer sb)
|
private int |
readSBits(int numBits)
Read a signed integer value from input. |
private long |
readUBits(int numBits)
|
void |
redimensionaImagem(int limite)
|
private static void |
run(java.lang.String sourceName,
java.io.InputStream in,
ImageInfo imageInfo,
boolean verbose)
|
void |
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 is false . |
void |
setInput(java.io.DataInput dataInput)
Set the input stream to the argument stream (or file). |
void |
setInput(java.io.InputStream inputStream)
Set the input stream to the argument stream (or file). |
private void |
setPhysicalHeightDpi(int newValue)
|
private void |
setPhysicalWidthDpi(int newValue)
|
private void |
skip(int num)
|
void |
zoomImagem(java.lang.String zoom)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int FORMAT_JPEG
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 isProgressive()
).
public static final int FORMAT_GIF
getFormat()
for GIF streams. ImageInfo can extract
comments from GIFs and count the number of images (GIFs with more than one
image are animations). It is determined whether the GIF stream is
interlaced (see isProgressive()
).
public static final int FORMAT_PNG
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 isProgressive()
).
public static final int FORMAT_BMP
getFormat()
for BMP streams. BMP only supports one
image per file. BMP does not allow for comments. The physical resolution
can be stored.
public static final int FORMAT_PCX
getFormat()
for PCX streams. PCX does not allow
for comments or more than one image per file. However, the physical
resolution can be stored.
public static final int FORMAT_IFF
getFormat()
for IFF streams.
public static final int FORMAT_RAS
getFormat()
for RAS streams. Sun Raster allows for
one image per file only and is not able to store physical resolution or
comments.
public static final int FORMAT_PBM
getFormat()
for PBM streams.
public static final int FORMAT_PGM
getFormat()
for PGM streams.
public static final int FORMAT_PPM
getFormat()
for PPM streams.
public static final int FORMAT_PSD
getFormat()
for PSD streams.
private static final java.lang.String[] FORMAT_NAMES
private static final java.lang.String[] MIME_TYPE_STRINGS
private int width
private int height
private int bitsPerPixel
private boolean progressive
private int format
private java.io.InputStream in
private java.io.DataInput din
private boolean collectComments
private java.util.Vector comments
private boolean determineNumberOfImages
private int numberOfImages
private int physicalHeightDpi
private int physicalWidthDpi
private int bitBuf
private int bitPos
Constructor Detail |
---|
public ImageInfo()
Method Detail |
---|
private void addComment(java.lang.String s)
public boolean check()
setInput(InputStream)
or setInput(DataInput)
. If true is
returned, the file format was known and information on the file's content
can be retrieved using the various getXyz methods.
private boolean checkBmp() throws java.io.IOException
java.io.IOException
private boolean checkGif() throws java.io.IOException
java.io.IOException
private boolean checkIff() throws java.io.IOException
java.io.IOException
private boolean checkJpeg() throws java.io.IOException
java.io.IOException
private boolean checkPcx() throws java.io.IOException
java.io.IOException
private boolean checkPng() throws java.io.IOException
java.io.IOException
private boolean checkPnm(int id) throws java.io.IOException
java.io.IOException
private boolean checkPsd() throws java.io.IOException
java.io.IOException
private boolean checkRas() throws java.io.IOException
java.io.IOException
private static boolean determineVerbosity(java.lang.String[] args)
-c
.
args
- string list to checkprivate static boolean equals(byte[] a1, int offs1, byte[] a2, int offs2, int num)
public int getBitsPerPixel()
check()
was successful, returns the image's number of bits per
pixel. Does not include transparency information like the alpha channel.
public java.lang.String getComment(int index)
index
- int index of comment to return
java.lang.IllegalArgumentException
- if index is smaller than 0 or larger than
or equal to the number of comments retrievedgetNumberOfComments()
public int getFormat()
check()
was successful, returns the image format as one of the
FORMAT_xyz constants from this class. Use getFormatName()
to get a
textual description of the file format.
public java.lang.String getFormatName()
check()
was successful, returns the image format's name. Use
getFormat()
to get a unique number.
public int getHeight()
check()
was successful, returns one the image's vertical
resolution in pixels.
private static int getIntBigEndian(byte[] a, int offs)
private static int getIntLittleEndian(byte[] a, int offs)
public java.lang.String getMimeType()
check()
was successful, returns a String with the MIME type of
the format.
image/jpeg
public int getNumberOfComments()
check()
was successful and setCollectComments(boolean)
was called with true
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 the
getComment(int)
method.
public int getNumberOfImages()
setDetermineImageNumber(true);
was called before a successful
call to check()
. This value can currently be only different from
1
for GIF images.
public int getPhysicalHeightDpi()
check()
was successful. Returns -1
on failure.
getPhysicalWidthDpi()
,
getPhysicalHeightInch()
public float getPhysicalHeightInch()
check()
was successful, returns the physical width of this
image in dpi (dots per inch) or -1 if no value could be found.
getPhysicalHeightDpi()
,
getPhysicalWidthDpi()
,
getPhysicalWidthInch()
public int getPhysicalWidthDpi()
check()
was successful, returns the physical width of this
image in dpi (dots per inch) or -1 if no value could be found.
getPhysicalHeightDpi()
,
getPhysicalWidthInch()
,
getPhysicalHeightInch()
public float getPhysicalWidthInch()
-1.0f
if
width information is not available. Assumes that check()
has been
called successfully.
-1.0f
on failuregetPhysicalWidthDpi()
,
getPhysicalHeightInch()
private static int getShortBigEndian(byte[] a, int offs)
private static int getShortLittleEndian(byte[] a, int offs)
public int getWidth()
check()
was successful, returns one the image's horizontal
resolution in pixels.
public boolean isProgressive()
public static void main(java.lang.String[] args)
args
- the program arguments which must be file namesprivate static void print(java.lang.String sourceName, ImageInfo ii, boolean verbose)
private static void printCompact(java.lang.String sourceName, ImageInfo imageInfo)
private static void printLine(int indentLevels, java.lang.String text, float value, float minValidValue)
private static void printLine(int indentLevels, java.lang.String text, int value, int minValidValue)
private static void printLine(int indentLevels, java.lang.String text, java.lang.String value)
private static void printVerbose(java.lang.String sourceName, ImageInfo ii)
private int read() throws java.io.IOException
java.io.IOException
private int read(byte[] a) throws java.io.IOException
java.io.IOException
private int read(byte[] a, int offset, int num) throws java.io.IOException
java.io.IOException
private java.lang.String readLine() throws java.io.IOException
java.io.IOException
private java.lang.String readLine(java.lang.StringBuffer sb) throws java.io.IOException
java.io.IOException
private long readUBits(int numBits) throws java.io.IOException
java.io.IOException
private int readSBits(int numBits) throws java.io.IOException
numBits
- number of bits to read
java.io.IOException
private static void run(java.lang.String sourceName, java.io.InputStream in, ImageInfo imageInfo, boolean verbose)
public void setCollectComments(boolean newValue)
false
. If enabled, comments will be added to an
internal list.
newValue
- if true
, this class will read commentsgetNumberOfComments()
,
getComment(int)
public void setDetermineImageNumber(boolean newValue)
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
getNumberOfImages()
after a successful call to check()
.
newValue
- will the number of images be determined?getNumberOfImages()
public void setInput(java.io.DataInput dataInput)
RandomAccessFile
implements DataInput
.
dataInput
- the input stream to read frompublic void setInput(java.io.InputStream inputStream)
inputStream
- the input stream to read fromprivate void setPhysicalHeightDpi(int newValue)
private void setPhysicalWidthDpi(int newValue)
private void skip(int num) throws java.io.IOException
java.io.IOException
public void redimensionaImagem(int limite)
public void zoomImagem(java.lang.String zoom)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |