createImageFromPixels function
Creates a new ui.Image from a Uint8List of pixel data.
This function takes a Uint8List containing the pixel data, the width
,
and the height
of the image, and creates a new ui.Image from it.
Parameters:
pixels
: The Uint8List containing the pixel data.width
: The width of the image.height
: The height of the image.
Returns: A Future that resolves to a ui.Image created from the pixel data.
Implementation
Future<ui.Image> createImageFromPixels(
final Uint8List pixels,
final int width,
final int height,
) async {
// Create a new ui.Image from the modified pixels
final ui.ImmutableBuffer buffer =
await ui.ImmutableBuffer.fromUint8List(pixels);
// Create a new ui.Image from the modified pixels
final ui.ImageDescriptor descriptor = ui.ImageDescriptor.raw(
buffer,
width: width,
height: height,
pixelFormat: ui.PixelFormat.rgba8888,
);
final ui.Codec codec = await descriptor.instantiateCodec();
final ui.FrameInfo frameInfo = await codec.getNextFrame();
return frameInfo.image;
}