decode static method

Future<Pixels> decode(
  1. Uint8List bytes, {
  2. int? width,
  3. int? height,
})

bytes (PNG, JPEG, …) decoded, scaled to width × height when given.

Implementation

static Future<Pixels> decode(
  Uint8List bytes, {
  int? width,
  int? height,
}) async {
  final codec = await ui.instantiateImageCodec(bytes);
  final ui.Image image;
  try {
    image = (await codec.getNextFrame()).image;
  } finally {
    codec.dispose();
  }
  try {
    final w = width ?? image.width, h = height ?? image.height;
    if (w == image.width && h == image.height) return await _of(image);
    final recorder = ui.PictureRecorder();
    ui.Canvas(recorder).drawImageRect(
      image,
      ui.Rect.fromLTWH(0, 0, image.width.toDouble(), image.height.toDouble()),
      ui.Rect.fromLTWH(0, 0, w.toDouble(), h.toDouble()),
      ui.Paint()..filterQuality = ui.FilterQuality.medium,
    );
    final picture = recorder.endRecording();
    final scaled = await picture.toImage(w, h);
    picture.dispose();
    try {
      return await _of(scaled);
    } finally {
      scaled.dispose();
    }
  } finally {
    image.dispose();
  }
}