0

I converted the widget into an image and then printed it, I do not know what the problem is for the image to appear with these strange texts, but I tried repeatedly to solve the problem and unfortunately I could not, so I hope you will help me to print the image perfectly with the printers

a receipt for printer

My Printer Model : Citypos mini printer cp-p100u

and my code :

  void pos_print() {
    screenshotController.capture().then((Uint8List? image) async {
      if (image == null) return null;

      Completer<PrinterDevice> completer = Completer<PrinterDevice>();

      Future.delayed(Duration.zero, () {
        PrinterManager.instance
            .discovery(type: PrinterType.usb)
            .listen((printer) {
          completer.complete(printer);
        });
      });

      msg_alert("Searching for printers ...");
      PrinterDevice printer;

      try {
        printer = await completer.future.timeout(Duration(seconds: 10));
      } on TimeoutException catch (e) {
        msg_alert("We dont Find Any Printer");
        return;
      }

      msg_alert("We Found the printer , Now We Connect with it");

      try {
        await PrinterManager.instance
            .connect(
                type: PrinterType.usb,
                model: UsbPrinterInput(
                    name: printer.name,
                    productId: printer.productId,
                    vendorId: printer.vendorId))
            .timeout(Duration(seconds: 5));
      } on TimeoutException catch (e) {
        msg_alert("Failed To Connect With Printer");
        return;
      }

      msg_alert("Printing ...");

      List<int> bytes = [];
      final generator =
          Generator(PaperSize.mm80, await CapabilityProfile.load());

      bytes += generator.feed(1);
      bytes += generator.image(img.decodePng(image)!, align: PosAlign.center);
      bytes += generator.feed(1);
      bytes += generator.cut(mode: PosCutMode.partial);

      bool success;

      try {
        success = await PrinterManager.instance
            .send(type: PrinterType.usb, bytes: bytes)
            .timeout(Duration(seconds: 5));
      } on TimeoutException catch (e) {
        success = false;
      }

      if (success)
        msg_alert("Print success");
      else
        msg_alert("print error");
    }).catchError((onError) {
      print(onError);
    });
  }

Fix the Image Issue

1 Answers1

0

Try below code,

  final ByteData data = await rootBundle.load('assets/ic_launcher.png');
    if (data.lengthInBytes > 0) {
      final Uint8List imageBytes = data.buffer.asUint8List();
      // decode the bytes into an image
      final decodedImage = img.decodeImage(imageBytes)!;
      // Create a black bottom layer
      // Resize the image to a 130x? thumbnail (maintaining the aspect ratio).
      img.Image thumbnail = img.copyResize(decodedImage, height: 130);
      // creates a copy of the original image with set dimensions
      img.Image originalImg = img.copyResize(decodedImage, width: 380, height: 130);
      // fills the original image with a white background
      img.fill(originalImg, color: img.ColorRgb8(255, 255, 255));
      var padding = (originalImg.width - thumbnail.width) / 2;

      //insert the image inside the frame and center it
      drawImage(originalImg, thumbnail, dstX: padding.toInt());

      // convert image to grayscale
      var grayscaleImage = img.grayscale(originalImg);

      bytes += generator.feed(1);
      // bytes += generator.imageRaster(img.decodeImage(imageBytes)!, align: PosAlign.center);
      bytes += generator.imageRaster(grayscaleImage, align: PosAlign.center);
      bytes += generator.feed(1);
    }