decodeRow method

  1. @override
Result decodeRow(
  1. int rowNumber,
  2. BitArray row,
  3. DecodeHint? hints
)
override

Attempts to decode a one-dimensional barcode format given a single row of an image.

@param rowNumber row number from top of the row @param row the black/white pixel data of the row @param hints decode hints @return Result containing encoded string and start/end of barcode @throws NotFoundException if no potential barcode is found @throws ChecksumException if a potential barcode is found but does not pass its checksum @throws FormatException if a potential barcode is found but format is invalid

Implementation

@override
Result decodeRow(
  int rowNumber,
  BitArray row,
  DecodeHint? hints,
) {
  // Compute this location once and reuse it on multiple implementations
  final startGuardPattern = UPCEANReader.findStartGuardPattern(row);
  for (UPCEANReader reader in _readers) {
    try {
      final result =
          reader.decodeRow(rowNumber, row, hints, startGuardPattern);
      // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
      // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
      // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
      // Individually these are correct and their readers will both read such a code
      // and correctly call it EAN-13, or UPC-A, respectively.
      //
      // In this case, if we've been looking for both types, we'd like to call it
      // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
      // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
      // result if appropriate.
      //
      // But, don't return UPC-A if UPC-A was not a requested format!
      final ean13MayBeUPCA = result.barcodeFormat == BarcodeFormat.ean13 &&
          result.text[0] == '0';
      // @SuppressWarnings("unchecked")
      final possibleFormats = hints?.possibleFormats;
      final canReturnUPCA = possibleFormats == null ||
          possibleFormats.contains(BarcodeFormat.upcA);

      if (ean13MayBeUPCA && canReturnUPCA) {
        // Transfer the metadata across
        final resultUPCA = Result(
          result.text.substring(1),
          result.rawBytes,
          result.resultPoints,
          BarcodeFormat.upcA,
        );
        resultUPCA.putAllMetadata(result.resultMetadata);
        return resultUPCA;
      }
      return result;
    } on ReaderException catch (_) {
      //
      // continue
    }
  }

  throw NotFoundException.instance;
}