printImage method

Future<void> printImage({
  1. required Image image,
  2. PaperSize paperSize = PaperSize.mm58,
  3. void progress(
    1. int total,
    2. int progress
    )?,
})

Implementation

Future<void> printImage({
  required img.Image image,
  PaperSize paperSize = PaperSize.mm58,
  void Function(int total, int progress)? progress,
}) async {
  img.Image src;

  final dotsPerLine = paperSize.width;

  // make sure image not bigger than printable area
  if (image.width > dotsPerLine) {
    double ratio = dotsPerLine / image.width;
    int height = (image.height * ratio).ceil();
    src = img.copyResize(
      image,
      width: dotsPerLine,
      height: height,
    );
  } else {
    src = image;
  }

  final profile = await CapabilityProfile.load();
  final generator = Generator(paperSize, profile);
  final data = generator.image(src);

  await printBytes(
    bytes: Uint8List.fromList(data),
    progress: progress,
  );
}