imageToUint8List function

Future<Uint8List> imageToUint8List(
  1. Image? image
)

Converts a ui.Image to a Uint8List representation.

This function takes a ui.Image and converts it to a Uint8List containing the raw RGBA data of the image.

Parameters:

  • image: The source image to be converted. Can be null.

Returns: A Future that resolves to a Uint8List containing the raw RGBA data of the image. If the input image is null or conversion fails, returns an empty Uint8List.

Implementation

Future<Uint8List> imageToUint8List(final ui.Image? image) async {
  if (image == null) {
    return Uint8List(0);
  }
  final ByteData? data =
      await image.toByteData(format: ui.ImageByteFormat.rawRgba);
  return data?.buffer.asUint8List() ?? Uint8List(0);
}