detectFormat static method

ImageFormat? detectFormat(
  1. Uint8List bytes
)

Detect the image format from raw bytes.

Returns the detected ImageFormat or null if the format is not supported.

Supported formats: JPEG, PNG Unsupported formats: HEIC, WebP, GIF, BMP, TIFF, etc.

bytes - Raw image data

Implementation

static ImageFormat? detectFormat(Uint8List bytes) {
  if (bytes.length < 4) return null;

  // JPEG: starts with FF D8 FF
  if (bytes[0] == 0xFF && bytes[1] == 0xD8 && bytes[2] == 0xFF) {
    return ImageFormat.jpeg;
  }

  // PNG: starts with 89 50 4E 47 (PNG magic number)
  if (bytes[0] == 0x89 &&
      bytes[1] == 0x50 &&
      bytes[2] == 0x4E &&
      bytes[3] == 0x47) {
    return ImageFormat.png;
  }

  return null;
}