Class JpegCodec

java.lang.Object
com.codename1.backend.image.JpegCodec

public final class JpegCodec extends Object

Reads and writes JPEG files in pure Java.

[#decode(byte[])] reads baseline and progressive Huffman-coded JPEG (JFIF, Exif and Adobe files) with any chroma subsampling, restart intervals, 8- and 16-bit quantization tables, gray, YCbCr and Adobe CMYK/YCCK color. Arithmetic coding and 12-bit lossless JPEG are not supported. Chroma is upsampled by replication, so a decode may differ from libjpeg's by a few levels near sharp color edges.

encode(RasterImage, float, boolean) writes baseline JPEG with the standard tables of the JPEG specification, 4:4:4 (no chroma subsampling), scaled to a quality the way libjpeg scales them, so quality 0.75 here gives the same tables as libjpeg's (and Pillow's) quality 75. Alpha is dropped.

  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    componentCount(byte[] data)
    Returns the number of color components of a JPEG file: 1 for gray, 3 for color, 4 for CMYK, or 0 when no frame header is found.
    decode(byte[] data)
    Decodes a JPEG file to an opaque ARGB image.
    static byte[]
    encode(RasterImage img, float quality)
    Encodes an image as baseline color JPEG.
    static byte[]
    encode(RasterImage img, float quality, boolean grayscale)
    Encodes an image as baseline JPEG.
    static boolean
    isJpeg(byte[] data)
    Returns true when data starts with a JPEG start-of-image marker.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isJpeg

      public static boolean isJpeg(byte[] data)

      Returns true when data starts with a JPEG start-of-image marker.

      Parameters
      • data: file bytes
      Returns

      whether the data looks like a JPEG file

    • componentCount

      public static int componentCount(byte[] data)

      Returns the number of color components of a JPEG file: 1 for gray, 3 for color, 4 for CMYK, or 0 when no frame header is found.

      Parameters
      • data: the file
      Returns

      the component count

    • decode

      public static RasterImage decode(byte[] data) throws IOException

      Decodes a JPEG file to an opaque ARGB image.

      Parameters
      • data: the file
      Returns

      the image

      Throws
      • IOException: when the data is not a JPEG file this codec can read
      Throws:
      IOException
    • encode

      public static byte[] encode(RasterImage img, float quality)

      Encodes an image as baseline color JPEG.

      Parameters
      • img: the image; alpha is ignored
      • quality: 0 to 1, as libjpeg's 0 to 100 divided by 100
      Returns

      the JPEG file

    • encode

      public static byte[] encode(RasterImage img, float quality, boolean grayscale)

      Encodes an image as baseline JPEG.

      Parameters
      • img: the image; alpha is ignored
      • quality: 0 to 1, as libjpeg's 0 to 100 divided by 100
      • grayscale: true to write one gray component (the luma of each pixel) instead of three
      Returns

      the JPEG file