andrey-ushakov / esc_pos_utils

Basic Flutter/Dart classes for ESC/POS printing
BSD 3-Clause "New" or "Revised" License
140 stars 308 forks source link

Print large image size #68

Closed Khamphay closed 1 year ago

Khamphay commented 2 years ago

Hello, I want to print image but the converted image is larger than the original image size, which it slow to print and cannot resize image before print.

saad3344555 commented 2 years ago

did you find any solution for this?

Khamphay commented 2 years ago

I cann't find a solution, I edited the NetworkPrinterManager.dart file it's still print slow but when print has no issues

sgsm74 commented 1 year ago

I cann't find a solution, I edited the NetworkPrinterManager.dart file it's still print slow but when print has no issues

can you share your solution? I faced this issue

Khamphay commented 1 year ago

I copy the source code in PrinterNetworkManager file and change _socket.destroy() => _socket.close() on funtion disconnect(...)

import 'dart:io';
import 'dart:typed_data' show Uint8List;
import 'package:esc_pos_printer/esc_pos_printer.dart';
import 'package:esc_pos_utils/esc_pos_utils.dart';
import 'package:image/image.dart';

/// Network Printer
class NetworkPrinterManager {
  NetworkPrinterManager(this._paperSize, this._profile,
      {int spaceBetweenRows = 5}) {
    _generator =
        Generator(paperSize, profile, spaceBetweenRows: spaceBetweenRows);
  }

  final PaperSize _paperSize;
  final CapabilityProfile _profile;
  String? _host;
  int? _port;
  late Generator _generator;
  late Socket _socket;
  late bool isClosed;
  bool isPrintCompleted = false;

  int? get port => _port;
  String? get host => _host;
  PaperSize get paperSize => _paperSize;
  CapabilityProfile get profile => _profile;

  Future<PosPrintResult> connect(String host,
      {int port = 91000, Duration timeout = const Duration(seconds: 5)}) async {
    _host = host;
    _port = port;
    try {
      _socket = await Socket.connect(host, port, timeout: timeout);
      _socket.add(_generator.reset());
      isClosed = false;
      return Future<PosPrintResult>.value(PosPrintResult.success);
    } catch (e) {
      isClosed = true;
      return Future<PosPrintResult>.value(PosPrintResult.timeout);
    }
  }

  /// [delayMs]: milliseconds to wait after destroying the socket
  void disconnect({int? delayMs}) async {
    _socket.close();
    isClosed = true;
    isPrintCompleted = true;
    if (delayMs != null) {
      await Future.delayed(Duration(milliseconds: delayMs), () => null);
    }
  }

  // ************************ Printer Commands ************************
  void reset() {
    _socket.add(_generator.reset());
  }

  void beep({int n = 3, PosBeepDuration duration = PosBeepDuration.beep450ms}) {
    _socket.add(_generator.beep(n: n, duration: duration));
    isPrintCompleted = true;
  }
.
.
.

Whe use

NetworkPrinterManager printer =
          NetworkPrinterManager(_paperSize, _profile);
PosPrintResult? result;
result = await printer.connect(
          your_ip,9100,
          timeout: 300);
if (result != PosPrintResult.success) return;

final imagebill.Image? image = imagebill.decodeImage(your_bill_image);

      printer.image(image!);
      printer.cut();
      printer.beep(n: 1, duration: PosBeepDuration.beep100ms);
      if (printer.isPrintCompleted) {
        printer.disconnect();
      }