如何使用 Flutter 和热敏蓝牙打印机打印图像

问题描述 投票:0回答:2

我有一个“销售收据”屏幕,因此我需要使用蓝牙热敏打印机打印此屏幕,但是当我尝试打印图像时,我只收到打印的“黑色”正方形。

打印结果(图像只是一个没有背景的徽标,所以我也尝试了不同的图片,但没有成功)

使用 blue_ Thermal_printer 包进行编码(我尝试使用其他包,但得到了相同的结果)

printContent(BluetoothDevice device) async {
  await connectToPrinter(device);

  final imageBytes = await imagePathToUint8List('assets/images/logo_ps.png');
  await bluetooth.printImageBytes(imageBytes);
  await bluetooth.paperCut();

  await disconnectToPrinter();
}

Future<Uint8List> imagePathToUint8List(String path) async {
//converting to Uint8List to pass to printer

  ByteData data = await rootBundle.load(path);
  Uint8List imageBytes =
    data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
  return imageBytes;
}

请问有人可以告诉我我做错了什么吗?

flutter dart printing bluetooth thermal-printer
2个回答
2
投票

我的错误是使用“无背景”的 png 来使用热敏打印机进行打印。当我使用具有白色背景的图像时,它起作用了。


0
投票

试试这个。

  ByteData bytesData = await rootBundle.load("assets/logo.png");
  String dir = (await getApplicationDocumentsDirectory()).path;
  File file = await File('$dir/logo.png')
  .writeAsBytes(bytesData.buffer.asUint8List(bytesData.offsetInBytes, bytesData.lengthInBytes));

然后这个

 bluetooth.printImage(file.path);
© www.soinside.com 2019 - 2024. All rights reserved.