Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class ImageInfo

  1. Object
  2. 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, plus getBitDepth(), getColorType() and hasAlpha() (an alpha color type, or a tRNS chunk before the image data).
  • JPEG: the segments are walked to the first start-of-frame marker (SOF0-SOF15, excluding DHT, JPG and DAC), skipping APPn segments such as EXIF and fill bytes; isProgressive() reports a progressive frame and getBitDepth() 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), with hasAlpha().

Anything else answers null.

Nested types

enum ImageInfo.FormatAn image format this class recognizes.

Methods

public static ImageInfo read(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.
public static ImageInfo read(InputStream in) throws IOExceptionReads in to its end (at most 64 MB) and answers [#read(byte[])] of the bytes.
public ImageInfo.Format getFormat()The format.
public int getWidth()The width in pixels.
public int getHeight()The height in pixels.
public int getBitDepth()Bits per sample: the PNG bit depth or the JPEG sample precision; -1 for GIF and WebP.
public int getColorType()The PNG color type (0 gray, 2 RGB, 3 palette, 4 gray + alpha, 6 RGBA); -1 for the other formats.
public boolean hasAlpha()True when the image can carry transparency: a PNG with an alpha color type or a tRNS chunk, or a WebP with an alpha flag.
public boolean isProgressive()True for a progressive JPEG (SOF2, SOF6, SOF10, SOF14).
public String toString()Returns a string representation of the object.

Inherited methods

Method details

read

public static ImageInfo read(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.

read

public static ImageInfo read(InputStream in) throws IOException
Reads in to 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.

getFormat

public ImageInfo.Format 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 a tRNS chunk, 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

public String toString()
Returns 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())