decodeImage function

Future<RgbaImage> decodeImage(
  1. Uint8List encoded
)

Decodes a PNG or JPEG (anything Flutter decodes) into RGBA8. Must run on the root isolate; Flutter decodes off the UI thread itself.

Implementation

Future<RgbaImage> decodeImage(Uint8List encoded) async {
  final codec = await ui.instantiateImageCodec(encoded);
  try {
    final frame = await codec.getNextFrame();
    final image = frame.image;
    try {
      final data = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
      if (data == null) throw StateError('Could not read the decoded image.');
      return RgbaImage(image.width, image.height, Uint8List.sublistView(data));
    } finally {
      image.dispose();
    }
  } finally {
    codec.dispose();
  }
}