fromProvider static method

Future<Uint8List> fromProvider(
  1. NetworkImage image,
  2. int width
)

Process and convert a NetworkImage to a Uint8List suitable for printing Maintains aspect ratio and handles transparency

Implementation

static Future<Uint8List> fromProvider(NetworkImage image, int width) async {
  try {
    // Get the original image dimensions to maintain aspect ratio
    final ui.Image? imageInfo = await getImageDimensions(image.url);
    final int originalWidth = imageInfo?.width ?? width;
    final int originalHeight = imageInfo?.height ?? width;

    // Calculate height based on aspect ratio
    final double aspectRatio = originalWidth / originalHeight;
    final int scaledHeight = (width / aspectRatio).round();

    // Download the image using http package
    final http.Response response = await http.get(Uri.parse(image.url));
    if (response.statusCode != 200) {
      throw Exception('Failed to load image data: ${response.statusCode}');
    }

    // Decode the image
    final img.Image? originalImage = img.decodeImage(response.bodyBytes);
    if (originalImage == null) {
      throw Exception('Failed to decode image');
    }

    // Resize the image maintaining aspect ratio
    final img.Image resizedImage = img.copyResize(
      originalImage,
      width: width,
      height: scaledHeight,
      interpolation: img.Interpolation.linear
    );

    // Create a white background image
    final img.Image whiteBackground = img.Image(
      width: width,
      height: scaledHeight,
    );

    // Fill with white color
    img.fill(whiteBackground, color: img.ColorRgba8(255, 255, 255, 255));

    // Composite the resized image over the white background
    img.compositeImage(whiteBackground, resizedImage);

    // Convert to Uint8List with BMP format
    final Uint8List bmpData = img.encodeBmp(whiteBackground);

    return bmpData;
  } catch (e) {
    throw Exception('Error processing bitmap image: $e');
  }
}