Exemplo n.º 1
0
                                       3;        // end guard

        /// <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);
            }

            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);
        }
Exemplo n.º 2
0
                                       3;        // end guard

        /// <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);
        }