Exemple #1
0
        /// <summary>
        /// </summary>
        /// <returns>
        /// a byte array of horizontal pixels (false = white, true = black)
        /// </returns>
        public override bool[] encode(String contents)
        {
            int length = contents.Length;

            switch (length)
            {
            case 7:
                // No check digit present, calculate it and add it
                var check = UPCEANReader.getStandardUPCEANChecksum(contents);
                if (check == null)
                {
                    throw new ArgumentException("Checksum can't be calculated");
                }
                contents += check.Value;
                break;

            case 8:
                try
                {
                    if (!UPCEANReader.checkStandardUPCEANChecksum(contents))
                    {
                        throw new ArgumentException("Contents do not pass checksum");
                    }
                }
                catch (FormatException ignored)
                {
                    throw new ArgumentException("Illegal contents", ignored);
                }
                break;

            default:
                throw new ArgumentException("Requested contents should be 7 (without checksum digit) or 8 digits long, but got " + contents.Length);
            }

            checkNumeric(contents);

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

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

            for (int i = 0; i <= 3; i++)
            {
                int digit = Int32.Parse(contents.Substring(i, 1));
                pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], false);
            }

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

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

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Encode the contents to byte array expression of one-dimensional barcode.
        /// Start code and end code should be included in result, and side margins should not be included.
        /// <returns>a {@code boolean[]} of horizontal pixels (false = white, true = black)</returns>
        /// </summary>
        /// <param name="contents"></param>
        /// <returns></returns>
        override public bool[] encode(String contents)
        {
            if (contents.Length < 12 || contents.Length > 13)
            {
                throw new ArgumentException(
                          "Requested contents should be 12 (without checksum digit) or 13 digits long, but got " + contents.Length);
            }
            foreach (var ch in contents)
            {
                if (!Char.IsDigit(ch))
                {
                    throw new ArgumentException("Requested contents should only contain digits, but got '" + ch + "'");
                }
            }

            if (contents.Length == 12)
            {
                contents = CalculateChecksumDigitModulo10(contents);
            }
            else
            {
                if (!UPCEANReader.checkStandardUPCEANChecksum(contents))
                {
                    throw new ArgumentException("Contents do not pass checksum");
                }
            }

            int firstDigit = Int32.Parse(contents.Substring(0, 1));
            int parities   = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];
            var 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 = Int32.Parse(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 = Int32.Parse(contents.Substring(i, 1));
                pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true);
            }
            appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);

            return(result);
        }
Exemple #3
0
        int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString)
        {
            int[] counters = decodeMiddleCounters;
            counters[0] = 0;
            counters[1] = 0;
            counters[2] = 0;
            counters[3] = 0;
            int end       = row.Size;
            int rowOffset = startRange[1];

            int lgPatternFound = 0;

            for (int x = 0; x < 5 && rowOffset < end; x++)
            {
                int bestMatch;
                if (!UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS, out bestMatch))
                {
                    return(-1);
                }
                resultString.Append((char)('0' + bestMatch % 10));
                foreach (int counter in counters)
                {
                    rowOffset += counter;
                }
                if (bestMatch >= 10)
                {
                    lgPatternFound |= 1 << (4 - x);
                }
                if (x != 4)
                {
                    // Read off separator if not last
                    rowOffset = row.getNextSet(rowOffset);
                    rowOffset = row.getNextUnset(rowOffset);
                }
            }

            if (resultString.Length != 5)
            {
                return(-1);
            }

            int checkDigit;

            if (!determineCheckDigit(lgPatternFound, out checkDigit))
            {
                return(-1);
            }

            if (extensionChecksum(resultString.ToString()) != checkDigit)
            {
                return(-1);
            }

            return(rowOffset);
        }
Exemple #4
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);
        }
        internal Result decodeRow(int rowNumber, BitArray row, int rowOffset)
        {
            int[] extensionStartRange = UPCEANReader.findGuardPattern(row, rowOffset, false, EXTENSION_START_PATTERN);
            if (extensionStartRange == null)
            {
                return(null);
            }
            var result = fiveSupport.decodeRow(rowNumber, row, extensionStartRange);

            if (result == null)
            {
                result = twoSupport.decodeRow(rowNumber, row, extensionStartRange);
            }
            return(result);
        }
Exemple #6
0
        int decodeMiddle(BitArray row, int[] startRange, StringBuilder resultString)
        {
            int[] counters = decodeMiddleCounters;
            counters[0] = 0;
            counters[1] = 0;
            counters[2] = 0;
            counters[3] = 0;
            int end       = row.Size;
            int rowOffset = startRange[1];

            int checkParity = 0;

            for (int x = 0; x < 2 && rowOffset < end; x++)
            {
                int bestMatch;
                if (!UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_AND_G_PATTERNS, out bestMatch))
                {
                    return(-1);
                }
                resultString.Append((char)('0' + bestMatch % 10));
                foreach (int counter in counters)
                {
                    rowOffset += counter;
                }
                if (bestMatch >= 10)
                {
                    checkParity |= 1 << (1 - x);
                }
                if (x != 1)
                {
                    // Read off separator if not last
                    rowOffset = row.getNextSet(rowOffset);
                    rowOffset = row.getNextUnset(rowOffset);
                }
            }

            if (resultString.Length != 2)
            {
                return(-1);
            }

            if (int.Parse(resultString.ToString()) % 4 != checkParity)
            {
                return(-1);
            }

            return(rowOffset);
        }
Exemple #7
0
        public override bool[] encode(String contents)
        {
            int length = contents.Length;

            switch (length)
            {
            case 7:
                // No check digit present, calculate it and add it
                var check = UPCEANReader.getStandardUPCEANChecksum(UPCEReader.convertUPCEtoUPCA(contents));
                if (check == null)
                {
                    throw new ArgumentException("Checksum can't be calculated");
                }
                contents += check.Value;
                break;

            case 8:
                try
                {
                    if (!UPCEANReader.checkStandardUPCEANChecksum(contents))
                    {
                        throw new ArgumentException("Contents do not pass checksum");
                    }
                }
                catch (FormatException ignored)
                {
                    throw new ArgumentException("Illegal contents", ignored);
                }
                break;

            default:
                throw new ArgumentException("Requested contents should be 8 digits long, but got " + length);
            }

            checkNumeric(contents);

            int firstDigit = int.Parse(contents.Substring(0, 1));

            if (firstDigit != 0 && firstDigit != 1)
            {
                throw new ArgumentException("Number system must be 0 or 1");
            }

            var checkDigit = int.Parse(contents.Substring(7, 1));
            var parities   = UPCEReader.NUMSYS_AND_CHECK_DIGIT_PATTERNS[firstDigit][checkDigit];
            var result     = new bool[CODE_WIDTH];
            var pos        = 0;

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

            for (var i = 1; i <= 6; i++)
            {
                var digit = int.Parse(contents.Substring(i, 1));
                if ((parities >> (6 - i) & 1) == 1)
                {
                    digit += 10;
                }
                pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false);
            }

            appendPattern(result, pos, UPCEANReader.END_PATTERN, false);

            return(result);
        }
        /// <summary>
        /// Encode the contents to byte array expression of one-dimensional barcode.
        /// Start code and end code should be included in result, and side margins should not be included.
        /// <returns>a {@code boolean[]} of horizontal pixels (false = white, true = black)</returns>
        /// </summary>
        /// <param name="contents"></param>
        /// <returns></returns>
        public override bool[] encode(String contents)
        {
            int length = contents.Length;

            switch (length)
            {
            case 12:
                // No check digit present, calculate it and add it
                var check = UPCEANReader.getStandardUPCEANChecksum(contents);
                if (check == null)
                {
                    throw new ArgumentException("Checksum can't be calculated");
                }
                contents += check.Value;
                break;

            case 13:
                try
                {
                    if (!UPCEANReader.checkStandardUPCEANChecksum(contents))
                    {
                        throw new ArgumentException("Contents do not pass checksum");
                    }
                }
                catch (FormatException ignored)
                {
                    throw new ArgumentException("Illegal contents", ignored);
                }
                break;

            default:
                throw new ArgumentException("Requested contents should be 12 (without checksum digit) or 13 digits long, but got " + contents.Length);
            }

            int firstDigit = Int32.Parse(contents.Substring(0, 1));
            int parities   = EAN13Reader.FIRST_DIGIT_ENCODINGS[firstDigit];
            var result     = new bool[CODE_WIDTH];
            int pos        = 0;

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

            // See EAN13Reader for a description of how the first digit & left bars are encoded
            for (int i = 1; i <= 6; i++)
            {
                int digit = Int32.Parse(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 = Int32.Parse(contents.Substring(i, 1));
                pos += appendPattern(result, pos, UPCEANReader.L_PATTERNS[digit], true);
            }
            appendPattern(result, pos, UPCEANReader.START_END_PATTERN, true);

            return(result);
        }