QrImage constructor

QrImage(
  1. QrCode qrCode
)

Implementation

factory QrImage(QrCode qrCode) {
  final QrImage template = QrImage._template(qrCode);
  final int moduleCount = template.moduleCount;
  final int dataSize = moduleCount * moduleCount;

  final Uint8List dataMap = Uint8List(dataSize)..setRange(0, dataSize, template._data);

  QrImage._fromData(qrCode, 0, dataMap)._mapData(getDataCache(qrCode));

  final Uint8List workingBuffer = Uint8List(dataSize);
  double minLostPoint = double.maxFinite;
  int bestMaskPattern = 0;
  Uint8List? bestData;

  for (int i = 0; i < 8; i++) {
    workingBuffer.setRange(0, dataSize, dataMap);

    final QrImage testImage = QrImage._fromData(qrCode, i, workingBuffer).._applyMask(i, template._data);

    final double lostPoint = _lostPoint(testImage);

    if (lostPoint < minLostPoint) {
      minLostPoint = lostPoint;
      bestMaskPattern = i;

      bestData ??= Uint8List(dataSize);
      bestData.setRange(0, dataSize, workingBuffer);
    }
  }

  final QrImage finalImage = QrImage._fromData(qrCode, bestMaskPattern, bestData!).._setupTypeInfo(bestMaskPattern, false);
  if (finalImage.typeNumber >= 7) {
    finalImage._setupTypeNumber(false);
  }

  return finalImage;
}