Closed shinxxxxwon closed 2 years ago
share your sample code & printed photo
_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
You can not write image directly Use escposutils package to convert image to bytes
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?
No, use below package https://pub.dev/packages/esc_pos_utils
Thank you for your valuable reply. Let's try!!! Thanks again for your package!
Which method in the escposutils package converts the image to bytes??? I can't find it by looking for the API.
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);
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();
??
Use drago_pos_printer package You will get example
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?
you can use flutter master branch now. will update the plugin stable version
_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?
maxPerLine means max char per line
CapabilityProfile
crashes. esc_pos_utils
and drago_pos_printer
Remove esc_pos_utils package in your pub yaml file
I think I misunderstood what you said. I am trying to print an image via USB from my android phone.
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.
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.
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?