drawQRCodeBitmap method

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

Implementation

drawQRCodeBitmap(String context, PrinterOffset offset, PrinterSize size,
    ECCLevelEnum ecc) {
  double width = size.width!;
  if (width % 8 != 0) {
    /// 宽度不是8的倍数则进1
    width = (width ~/ 8 + 1) * 8.0;
  }
  final qrCode =
      QrCode.fromData(data: context, errorCorrectLevel: eccToLibNumber(ecc));
  qrCode.make();
  int zoom = size.width! ~/ qrCode.moduleCount;
  int viewOffset = (size.width! - zoom * qrCode.moduleCount) ~/ 2;
  var bytes = List<List<bool>>.generate(size.height!.toInt(),
      (_) => List<bool>.filled(size.width!.toInt(), false));
  for (int x = 0; x < qrCode.moduleCount; x++) {
    for (int y = 0; y < qrCode.moduleCount; y++) {
      if (qrCode.isDark(y, x) == true) {
        int baseX = x * zoom + viewOffset;
        int baseY = y * zoom + viewOffset;
        for (int j = 0; j < zoom; j++) {
          bytes[baseY + j].fillRange(baseX, baseX + zoom, true);
        }
        // render a dark square on the canvas
      }
    }
  }
  this.drawBitmap(offset, PrinterSize(width, size.height), bytes);
}