Class ImageInfo

java.lang.Object
com.codename1.backend.ImageInfo

public final class ImageInfo extends Object

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 Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    An image format this class recognizes.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Bits per sample: the PNG bit depth or the JPEG sample precision; -1 for GIF and WebP.
    int
    The PNG color type (0 gray, 2 RGB, 3 palette, 4 gray + alpha, 6 RGBA); -1 for the other formats.
    The format.
    int
    The height in pixels.
    int
    The width in pixels.
    boolean
    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.
    boolean
    True for a progressive JPEG (SOF2, SOF6, SOF10, SOF14).
    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.
    static ImageInfo
    Reads in to its end (at most 64 MB) and answers [#read(byte[])] of the bytes.
    Returns a string representation of the object.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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.
      Throws:
      IOException
    • 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()
      Description copied from class: Object
      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())
      Overrides:
      toString in class Object