Class ImageInfo
java.lang.Object
com.codename1.backend.ImageInfo
The format and dimensions of an image, read from its header without decoding any pixels.
ImageInfo info = ImageInfo.read(upload);
if (info == null || info.getFormat() != ImageInfo.Format.PNG) {
throw new ResponseStatusException(400, "a PNG is required");
}
if (info.getWidth() != 1290 || info.getHeight() != 2796) {
throw new ResponseStatusException(400, "wrong screenshot size");
}
PNG, JPEG, GIF and WebP are recognized by their signature, never by a file name. The answer is what the header claims: a file with a valid header and a corrupt body still reads, so this checks an upload's shape, not that it will display.
- PNG: width and height from
IHDR, plusgetBitDepth(),getColorType()andhasAlpha()(an alpha color type, or atRNSchunk before the image data). - JPEG: the segments are walked to the first start-of-frame marker
(
SOF0-SOF15, excludingDHT,JPGandDAC), skippingAPPnsegments such as EXIF and fill bytes;isProgressive()reports a progressive frame andgetBitDepth()the sample precision. EXIF orientation is not applied: the dimensions are the stored ones. - GIF: the logical screen size.
- WebP: lossy (
VP8), lossless (VP8L) and extended (VP8X), withhasAlpha().
Anything else answers null.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn image format this class recognizes. -
Method Summary
Modifier and TypeMethodDescriptionintBits per sample: the PNG bit depth or the JPEG sample precision; -1 for GIF and WebP.intThe PNG color type (0 gray, 2 RGB, 3 palette, 4 gray + alpha, 6 RGBA); -1 for the other formats.The format.intThe height in pixels.intgetWidth()The width in pixels.booleanhasAlpha()True when the image can carry transparency: a PNG with an alpha color type or atRNSchunk, or a WebP with an alpha flag.booleanTrue for a progressive JPEG (SOF2,SOF6,SOF10,SOF14).static ImageInforead(byte[] data) Reads the header of an image held in memory; null when the data is not a recognized format or its header is truncated or invalid.static ImageInforead(InputStream in) Readsinto its end (at most 64 MB) and answers [#read(byte[])] of the bytes.toString()Returns a string representation of the object.
-
Method Details
-
read
Reads the header of an image held in memory; null when the data is not a recognized format or its header is truncated or invalid. -
read
Reads
into its end (at most 64 MB) and answers [#read(byte[])] of the bytes. The stream is not closed.Throws
IOException: when reading fails or the stream is longer than 64 MB.
- Throws:
IOException
-
getFormat
The format. -
getWidth
public int getWidth()The width in pixels. -
getHeight
public int getHeight()The height in pixels. -
getBitDepth
public int getBitDepth()Bits per sample: the PNG bit depth or the JPEG sample precision; -1 for GIF and WebP. -
getColorType
public int getColorType()The PNG color type (0 gray, 2 RGB, 3 palette, 4 gray + alpha, 6 RGBA); -1 for the other formats. -
hasAlpha
public boolean hasAlpha()True when the image can carry transparency: a PNG with an alpha color type or atRNSchunk, or a WebP with an alpha flag. Always false for JPEG and GIF. -
isProgressive
public boolean isProgressive()True for a progressive JPEG (SOF2,SOF6,SOF10,SOF14). -
toString
Description copied from class:ObjectReturns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
-