decodeMatrix method

DecoderResult decodeMatrix(
  1. BitMatrix bits, [
  2. DecodeHint? hints
])

Decodes a QR Code represented as a [BitMatrix]. A 1 or "true" is taken to mean a black module.

@param bits booleans representing white/black QR Code modules @param hints decoding hints that should be used to influence decoding @return text and bytes encoded within the QR Code @throws FormatException if the QR Code cannot be decoded @throws ChecksumException if error correction fails

Implementation

DecoderResult decodeMatrix(
  BitMatrix bits, [
  DecodeHint? hints,
]) {
  // Construct a parser and read version, error-correction level
  final parser = BitMatrixParser(bits);
  FormatsException? fe;
  ChecksumException? ce;
  try {
    return _decodeParser(parser, hints);
  } on FormatsException catch (e) {
    fe = e;
  } on ChecksumException catch (e) {
    ce = e;
  }

  try {
    // Revert the bit matrix
    parser.remask();

    // Will be attempting a mirrored reading of the version and format info.
    parser.setMirror(true);

    // Preemptively read the version.
    parser.readVersion();

    // Preemptively read the format information.
    parser.readFormatInformation();

    /*
     * Since we're here, this means we have successfully detected some kind
     * of version and format information when mirrored. This is a good sign,
     * that the QR code may be mirrored, and we should try once more with a
     * mirrored content.
     */
    // Prepare for a mirrored reading.
    parser.mirror();

    final result = _decodeParser(parser, hints);

    // Success! Notify the caller that the code was mirrored.
    result.other = QRCodeDecoderMetaData(true);

    return result;
  } on ChecksumException catch (_) {
    // Throw the exception from the original reading
    if (fe != null) {
      throw fe;
    }
    throw ce!; // If fe is null, this can't be
  } on FormatsException catch (_) {
    // Throw the exception from the original reading
    if (fe != null) {
      throw fe;
    }
    throw ce!; // If fe is null, this can't be
  }
}