decodeToGray static method

Future<UGrayImage?> decodeToGray(
  1. Uint8List bytes
)

Turns encoded image bytes (JPEG, PNG, WebP, ...) into a grayscale buffer using Flutter's own codecs.

Implementation

static Future<UGrayImage?> decodeToGray(Uint8List bytes) async {
  try {
    final ui.Codec codec = await ui.instantiateImageCodec(bytes);
    final ui.FrameInfo frame = await codec.getNextFrame();
    final ui.Image image = frame.image;
    final ByteData? raw = await image.toByteData();
    final int width = image.width;
    final int height = image.height;
    image.dispose();
    codec.dispose();
    if (raw == null) return null;
    return UGrayImage.fromPacked(raw.buffer.asUint8List(), width, height, bgra: false);
  } catch (_) {
    return null;
  }
}