Beispiel #1
0
        override public Result decodeRow(int rowNumber,
                                         BitArray row,
                                         Hashtable hints)
        {
            // Compute this location once and reuse it on multiple implementations
            int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
            if (startGuardPattern == null)
            {
                return(null);
            }

            foreach (UPCEANReader reader in readers)
            {
                Result result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
                if (result == null)
                {
                    continue;
                }

                // 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!
                bool ean13MayBeUPCA =
                    result.BarcodeFormat == BarcodeFormat.EAN_13 &&
                    result.Text[0] == '0';
                var possibleFormats =
                    hints == null || !hints.Contains(DecodeHintType.POSSIBLE_FORMATS) ? null : (ArrayList)hints[DecodeHintType.POSSIBLE_FORMATS];
                bool canReturnUPCA = possibleFormats == null || possibleFormats.Contains(BarcodeFormat.UPC_A);

                if (ean13MayBeUPCA && canReturnUPCA)
                {
                    // Transfer the metdata across
                    var resultUPCA = new Result(result.Text.Substring(1),
                                                result.RawBytes,
                                                result.ResultPoints,
                                                BarcodeFormat.UPC_A);
                    resultUPCA.putAllMetadata(result.ResultMetadata);
                    return(resultUPCA);
                }
                return(result);
            }

            return(null);
        }