selvam920 / drago_usb_printer

GNU General Public License v3.0
1 stars 6 forks source link

Printed as text when printing an image #1

Closed shinxxxxwon closed 2 years ago

shinxxxxwon commented 2 years ago

If you write() the image data, it is printed as text, and the text is also printed by dividing it into several sheets.

Is there a way to print an image?

selvam920 commented 2 years ago

share your sample code & printed photo

shinxxxxwon commented 2 years ago
 _print() async {
    try {
       // var data = Uint8List.fromList(
       //     utf8.encode(" Hello world Testing ESC POS printer..."));
       var bytes = await rootBundle.load('assets/images/1-1.JPG');
       var img = bytes.buffer.asUint8List();
      await dragoUsbPrinter.write(img);
      // await DragoUsbPrinter.printRawData("text");
      // await DragoUsbPrinter.printText("Testing ESC POS printer...");
    } on PlatformException {
      //response = 'Failed to get platform version.';
    }
  }

The code used your example code. I just encoded the AssetImage as Uint8List and called the write() method.

Print Photo image

selvam920 commented 2 years ago

You can not write image directly Use escposutils package to convert image to bytes

shinxxxxwon commented 2 years ago
import 'dart:io';
import 'package:image/image.dart';

final ByteData data = await rootBundle.load('assets/logo.png');
final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes);

Is this correct what you meant?

selvam920 commented 2 years ago

No, use below package https://pub.dev/packages/esc_pos_utils

shinxxxxwon commented 2 years ago

Thank you for your valuable reply. Let's try!!! Thanks again for your package!

shinxxxxwon commented 2 years ago

Which method in the escposutils package converts the image to bytes??? I can't find it by looking for the API.

selvam920 commented 2 years ago

final ByteData data = await rootBundle.load('assets/logo.png'); final Uint8List bytes = data.buffer.asUint8List(); final Image image = decodeImage(bytes); // Using ESC * generator.image(image);

shinxxxxwon commented 2 years ago
 var data = await rootBundle.load('assets/images/1-1.JPG');
       final Uint8List bytes = data.buffer.asUint8List();
       var image = decodeImage(bytes);
       final profile = await CapabilityProfile.load();
       final generator = Generator(PaperSize.mm80, profile);
       generator.image(image!);

I wrote as per your suggestion. generator.image(image!); Are you giving it to the printer in the method? Shouldn't we use the existing await dragoUsbPrinter.write();??

selvam920 commented 2 years ago

Use drago_pos_printer package You will get example

shinxxxxwon commented 2 years ago
The current Dart SDK version is 2.15.0-116.0.dev.

Because stack_over depends on win32 >=2.3.7 which requires SDK version >=2.15.0 <3.0.0, version solving failed.
pub get failed (1; Because stack_over depends on win32 >=2.3.7 which requires SDK version >=2.15.0 <3.0.0, version solving failed.)

Do you know what the problem is?

selvam920 commented 2 years ago

you can use flutter master branch now. will update the plugin stable version

shinxxxxwon commented 2 years ago
_connect(USBPrinter printer) async {
    var paperSize = PaperSize.mm80;
    var profile = await CapabilityProfile.load();
    var manager = USBPrinterManager(printer, paperSize, profile);
    await manager.connect();
    setState(() {
      _manager = manager;
      printer.connected = true;
    });
  }

In this example source, the parameters of USBPrinterManager(printer, paperSize, profile); seem to be incorrect

method prototype

USBPrinterManager(
    POSPrinter printer,
    int paperSizeWidthMM,
    int maxPerLine,
    CapabilityProfile profile, {
    int spaceBetweenRows = 5,
    int port: 9100,
  })

What does maxPerLine mean?

selvam920 commented 2 years ago

https://github.com/selvam920/drago_pos_printer/tree/main/example

selvam920 commented 2 years ago

maxPerLine means max char per line

shinxxxxwon commented 2 years ago

CapabilityProfile crashes. esc_pos_utils and drago_pos_printer

selvam920 commented 2 years ago

Remove esc_pos_utils package in your pub yaml file

shinxxxxwon commented 2 years ago

I think I misunderstood what you said. I am trying to print an image via USB from my android phone.

  1. Using DragoUsbPrinter, the printer was searched and connected.
    
    List<Map<String, dynamic>> devices = [];
    DragoUsbPrinter dragoUsbPrinter = DragoUsbPrinter();
    bool connected = false;

_getDevicelist() async { List<Map<String, dynamic>> results = []; results = await DragoUsbPrinter.getUSBDeviceList();

print(" length: ${results.length}");
setState(() {
  devices = results;
});

}

_connect(int vendorId, int productId) async { bool? returned = false; try { returned = await dragoUsbPrinter.connect(vendorId, productId); } on PlatformException { //response = 'Failed to get platform version.'; } if (returned!) { setState(() { connected = true; }); } }


2. I used 'EscPosUtils' to convert the image data to bytes.
  var data = await rootBundle.load('assets/images/1-1.JPG');
   final Uint8List bytes = data.buffer.asUint8List();
   var image = decodeImage(bytes);
   final profile = await CapabilityProfile.load();
   final generator = Generator(PaperSizeWidth.mm80, 200, profile);
   generator.image(image!);


3.Is it possible to use `DragoPosPrinter` only for printing?

If not, is it necessary to perform `scan()` in `Dragon PosPrinter` and `_startPrinter()` after performing `connect(USB Printer printer)`?? Sorry for my poor English skills.
shinxxxxwon commented 2 years ago

I looked at the git data you gave me and completed the search, linkage, and printing. However, the image data is output as text as before. If my guess is correct, among the methods that print,

if (_data.isEmpty) {
      final content = Demo.getShortReceiptContent();
      var bytes = await WebcontentConverter.contentToImage(
        content: content,
        executablePath: WebViewHelper.executablePath(),
      );
      var service = ESCPrinterService(bytes);
      var data = await service.getBytes();
      if (mounted) setState(() => _data = data);
    }

It seems that this part is missing. Any advice on where the Demo, WebViewHelper and ESCPrinterService classes are located would be appreciated. Thank you so much for answering my continuing question.