Ejemplo n.º 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);
            }

            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);
        }
Ejemplo n.º 2
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);
            }

            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);
        }
Ejemplo n.º 3
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>
        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);
        }