readCodewords method

Uint8List readCodewords()

Implementation

Uint8List readCodewords() {
  final UQrFormatInformation formatInfo = readFormatInformation();
  final UQrVersion version = readVersion();
  final UBitMatrix functionPattern = version.buildFunctionPattern();

  bool readingUp = true;
  final Uint8List result = Uint8List(version.totalCodewords);
  int resultOffset = 0;
  int currentByte = 0;
  int bitsRead = 0;
  final int dimension = _bits.height;

  for (int j = dimension - 1; j > 0; j -= 2) {
    if (j == 6) j--;
    for (int count = 0; count < dimension; count++) {
      final int i = readingUp ? dimension - 1 - count : count;
      for (int col = 0; col < 2; col++) {
        final int x = j - col;
        if (functionPattern.get(x, i)) continue;
        bitsRead++;
        currentByte <<= 1;
        bool bit = _bits.get(x, i);
        if (UQrDataMask.isMasked(formatInfo.dataMask, i, x)) bit = !bit;
        if (bit) currentByte |= 1;
        if (bitsRead == 8) {
          if (resultOffset < result.length) result[resultOffset++] = currentByte;
          bitsRead = 0;
          currentByte = 0;
        }
      }
    }
    readingUp = !readingUp;
  }
  if (resultOffset != version.totalCodewords) throw const UCodeDecodeException("QR codeword count mismatch");
  return result;
}