decodeImageFromPixels function

void decodeImageFromPixels (
  1. Uint8List pixels,
  2. int width,
  3. int height,
  4. PixelFormat format,
  5. ImageDecoderCallback callback,
  6. {int rowBytes,
  7. int targetWidth,
  8. int targetHeight,
  9. bool allowUpscaling: true}
)

Convert an array of pixel values into an Image object.

The pixels parameter is the pixel data in the encoding described by format.

The rowBytes parameter is the number of bytes consumed by each row of pixels in the data buffer. If unspecified, it defaults to width multiplied by the number of bytes per pixel in the provided format.

The targetWidth and targetHeight arguments specify the size of the output image, in image pixels. If they are not equal to the intrinsic dimensions of the image, then the image will be scaled after being decoded. If the allowUpscaling parameter is not set to true, both dimensions will be capped at the intrinsic dimensions of the image, even if only one of them would have exceeded those intrinsic dimensions. If exactly one of these two arguments is specified, then the aspect ratio will be maintained while forcing the image to match the other given dimension. If neither is specified, then the image maintains its intrinsic size.

Scaling the image to larger than its intrinsic size should usually be avoided, since it causes the image to use more memory than necessary. Instead, prefer scaling the Canvas transform. If the image must be scaled up, the allowUpscaling parameter must be set to true.

Implementation

void decodeImageFromPixels(
  Uint8List pixels,
  int width,
  int height,
  PixelFormat format,
  ImageDecoderCallback callback, {
  int? rowBytes,
  int? targetWidth,
  int? targetHeight,
  bool allowUpscaling = true,
}) {
  if (targetWidth != null) {
    assert(allowUpscaling || targetWidth <= width);
  }
  if (targetHeight != null) {
    assert(allowUpscaling || targetHeight <= height);
  }

  final _ImageInfo imageInfo = _ImageInfo(width, height, format.index, rowBytes);
  final Future<Codec> codecFuture = _futurize((_Callback<Codec> callback) {
    return _instantiateImageCodec(
      pixels,
      callback,
      imageInfo,
      targetWidth ?? _kDoNotResizeDimension,
      targetHeight ?? _kDoNotResizeDimension,
      allowUpscaling,
    );
  });
  codecFuture.then((Codec codec) => codec.getNextFrame())
      .then((FrameInfo frameInfo) => callback(frameInfo.image));
}