【问题标题】:How to print QR Image in thermal printer using flutter如何使用颤振在热敏打印机中打印 QR 图像
【发布时间】:2022-11-05 18:28:25
【问题描述】:

我正在使用flutter blueflutter_esc_pos_utils

可以打印文字但不是图像或二维码。我想打印我已经生成的图像(这是一个二维码)。

即使在他们的文档中提供了示例,我也无法打印 QR 这是我打印二维码的代码

void printWithDevice(BluetoothDevice device) async {
    final profile = await CapabilityProfile.load();
    final gen = Generator(PaperSize.mm58, profile);

    final printer = BluePrint();
    final ByteData data = await rootBundle.load('assets/images/qr.png');
    final Uint8List bytes = data.buffer.asUint8List();
    final img.Image image = img.decodeImage(bytes)!;
    printer.add(gen.image(image));               // does not work
    printer.add(gen.qrcode('www.google.com'));  // does not work 
    printer.add(gen.text('hello');              // works
    await printer.printData(device);
  }

这是我从here 获得的 BluePrint 类的实现

class BluePrint {
  BluePrint({this.chunkLen = 512});

  final int chunkLen;
  final _data = List<int>.empty(growable: true);

  void add(List<int> data) {
    _data.addAll(data);
  }

  List<List<int>> getChunks() {
    final chunks = List<List<int>>.empty(growable: true);
    for (var i = 0; i < _data.length; i += chunkLen) {
      chunks.add(_data.sublist(i, min(i + chunkLen, _data.length)));
    }
    return chunks;
  }

  Future<void> printData(BluetoothDevice device) async {
    final data = getChunks();
    final characs = await _getCharacteristics(device);
    for (var i = 0; i < characs.length; i++) {
      if (await _tryPrint(characs[i], data)) {
        break;
      }
    }
  }

  Future<bool> _tryPrint(
    BluetoothCharacteristic charc,
    List<List<int>> data,
  ) async {
    for (var i = 0; i < data.length; i++) {
      try {
        await charc.write(data[i]);
      } catch (e) {
        return false;
      }
    }
    return true;
  }

  Future<List<BluetoothCharacteristic>> _getCharacteristics(
    BluetoothDevice device,
  ) async {
    final services = await device.discoverServices();
    final res = List<BluetoothCharacteristic>.empty(growable: true);
    for (var i = 0; i < services.length; i++) {
      res.addAll(services[i].characteristics);
    }
    return res;
  }
}

这也不行不工作是指它根本不打印任何东西

String qrData = "google.com";
const double qrSize = 200;
try {
  final uiImg = await QrPainter(
    data: qrData,
    version: QrVersions.auto,
    gapless: false,
  ).toImageData(qrSize);
  final dir = await getTemporaryDirectory();
  final pathName = '${dir.path}/qr_tmp.png';
  final qrFile = File(pathName);
  final imgFile = await qrFile.writeAsBytes(uiImg.buffer.asUint8List());
  final img = decodeImage(imgFile.readAsBytesSync());

  generator.image(img);
} catch (e) {
  print(e);
}

我也试过this它打印的是图像的字符串值而不是图像本身,无论如何我可以用这种方法打印图像吗?

还有一件事我想提到的是,当我尝试不同的事情时,我得到了奇怪的问号 (??????),例如 this 我从 here 得到这张图片

两天前我得到了那个输出我现在没有我的代码我必须检查我的本地历史以找出那个代码,因为我不记得我尝试了什么。

我究竟做错了什么?

编辑 - 我正在尝试一些事情,我注意到当我传递简单的文本时,我在这一行中有数据,例如 [14,21,...]

 Future<bool> _tryPrint(
    BluetoothCharacteristic charc,
    List<List<int>> data,
  ) async {
    for (var i = 0; i < data.length; i++) {
      try {
        await charc.write(data[i]);
        print('printing data in chunck ${data[i]} and size ${data.length}');
      } catch (e) {
        return false;
      }
    }
    return true;
  } 

但是当我通过图像时,我得到了 0,0,0,0.... 也许这可能是个问题?

【问题讨论】:

    标签: flutter image qr-code thermal-printer escpos


    【解决方案1】:
     printer.add(gen.image(image));  // does not work
    

    这条线没有工作,因为两个原因-

    1 - 我的打印机不支持 ESC* 命令,

    2 - 我没有注意图像的宽度,

    解决方案-

    1 - 它与GS v 0command 一起使用 像这样gen.imageRaster(image);

    2 - 图像宽度 222 对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 2021-09-12
      • 1970-01-01
      • 2012-10-05
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多