drawQRCode method

  1. @override
dynamic drawQRCode(
  1. String context,
  2. PrinterOffset offset,
  3. PrinterSize size,
  4. ECCLevelEnum ecc,
)
override

打印二维码

context 内容

offset 偏移位置

size 大小

ecc 纠错等级

Implementation

@override
drawQRCode(String context, PrinterOffset offset, PrinterSize size, ECCLevelEnum ecc) {
  Paint paint = Paint();
  paint.color = Color(0xFF000000);

  final qrCode =
      QrCode.fromData(data: context, errorCorrectLevel: eccToLibNumber(ecc));
  qrCode.make();
  int zoom = size.width! ~/ qrCode.moduleCount;
  PrinterSize moduleSize = PrinterSize(zoom.toDouble(), zoom.toDouble());
  for (int x = 0; x < qrCode.moduleCount; x++) {
    for (int y = 0; y < qrCode.moduleCount; y++) {
      if (qrCode.isDark(y, x) == true) {
        var moduleOffset = offset.translate((x * zoom).toDouble(), (y * zoom).toDouble());

        canvas!.drawRect(Rect.fromLTWH(moduleOffset.x!, moduleOffset.y!, moduleSize.width!, moduleSize.height!), paint);
        // render a dark square on the canvas
      }
    }
  }
}