Пример #1
0
        public override Result decodeRow(int rowNumber, BitArray row, System.Collections.Hashtable hints)
        {
            // Compute this location once and reuse it on multiple implementations
            int[] startGuardPattern = AbstractUPCEANReader.findStartGuardPattern(row);
            int   size = readers.Count;

            for (int i = 0; i < size; i++)
            {
                UPCEANReader reader = (UPCEANReader)readers[i];
                Result       result;
                try {
                    result = reader.decodeRow(rowNumber, row, startGuardPattern);
                } catch (ReaderException re) {
                    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.
                if (result.getBarcodeFormat().Equals(BarcodeFormat.EAN_13) && result.getText()[0] == '0')
                {
                    return(new Result(result.getText().Substring(1), null, result.getResultPoints(), BarcodeFormat.UPC_A));
                }
                return(result);
            }

            throw new ReaderException();
        }
Пример #2
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public com.google.zxing.Result decodeRow(int rowNumber, com.google.zxing.common.BitArray row, java.util.Map<com.google.zxing.DecodeHintType,?> hints) throws com.google.zxing.NotFoundException
        public override Result decodeRow(int rowNumber, BitArray row, IDictionary <DecodeHintType, object> hints)
        {
            // Compute this location once and reuse it on multiple implementations
            int[] startGuardPattern = UPCEANReader.findStartGuardPattern(row);
            foreach (UPCEANReader reader in readers)
            {
                Result result;
                try
                {
                    result = reader.decodeRow(rowNumber, row, startGuardPattern, hints);
                }
                catch (ReaderException re)
                {
                    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';
                //ICollection<BarcodeFormat> possibleFormats = hints == null ? null : (ICollection<BarcodeFormat>) hints[DecodeHintType.POSSIBLE_FORMATS];
                ICollection <BarcodeFormat> possibleFormats = null;
                if (hints != null && hints.ContainsKey(DecodeHintType.POSSIBLE_FORMATS))
                {
                    possibleFormats = (ICollection <BarcodeFormat>)hints[DecodeHintType.POSSIBLE_FORMATS];
                }

                bool canReturnUPCA = possibleFormats == null || possibleFormats.Contains(BarcodeFormat.UPC_A);

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

            throw NotFoundException.NotFoundInstance;
        }
Пример #3
0
        public override bool[] encode(string contents)
        {
            if (contents.Length != 13)
            {
                throw new System.ArgumentException("Requested contents should be 13 digits long, but got " + contents.Length);
            }
            try
            {
                if (!UPCEANReader.checkStandardUPCEANChecksum(contents))
                {
                    throw new System.ArgumentException("Contents do not pass checksum");
                }
            }
            catch (FormatException fe)
            {
                throw new System.ArgumentException("Illegal contents");
            }

            int firstDigit = Convert.ToInt32(contents.Substring(0, 1));
            int parities   = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];

            bool[] result = new bool[CODE_WIDTH];
            int    pos    = 0;

            pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);

            // See {@link #EAN13Reader} for a description of how the first digit & left bars are encoded
            for (int i = 1; i <= 6; i++)
            {
                int digit = Convert.ToInt32(contents.Substring(i, 1));
                if ((parities >> (6 - i) & 1) == 1)
                {
                    digit += 10;
                }
                pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false);
            }

            pos += appendPattern(result, pos, UPCEANReader.MIDDLE_PATTERN, false);

            for (int i = 7; i <= 12; i++)
            {
                int digit = Convert.ToInt32(contents.Substring(i, 1));
                pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true);
            }
            pos += appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);

            return(result);
        }