Example #1
0
        private static BitList EncodeEan13(string content)
        {
            var result = new BitList();

            result.AddBit(true, false, true);
            bool[] firstNum = null;
            var    cpos     = 0;

            foreach (var r in content)
            {
                Constants.EncodedNumber num = Constants.EncodingTable[r];
                if (firstNum == null)
                {
                    firstNum = num.Checksum;
                    cpos++;
                    continue;
                }
                bool[] data = cpos < 7 ? firstNum[cpos - 1] ? num.LeftEven : num.LeftOdd : num.Right;
                if (cpos == 7)
                {
                    result.AddBit(false, true, false, true, false);
                }
                result.AddBit(data);
                cpos++;
            }
            result.AddBit(true, false, true);
            return(result);
        }
Example #2
0
        private static BitList EncodeUpcA(string content)
        {
            var result = new BitList();

            // Start bars
            result.AddBit(true, false, true);

            var cpos = 0;

            foreach (var r in content)
            {
                Constants.EncodedNumber num = Constants.EncodingTable[r];
                bool[] data = cpos < 6 ? num.Left : num.Right;

                // Middle bars
                if (cpos == 6)
                {
                    result.AddBit(false, true, false, true, false);
                }

                result.AddBit(data);
                cpos++;
            }

            // Stop bars
            result.AddBit(true, false, true);

            return(result);
        }
Example #3
0
        public static IBarcode Encode(string content)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (!Regex.IsMatch(content, @"^[ABCD][0-9\-\$\:/\.\+]*[ABCD]$"))
            {
                throw new InvalidOperationException($"{content} could not be encoded");
            }

            var resBits = new BitList();
            var i       = 0;

            foreach (var r in content)
            {
                if (i++ > 0)
                {
                    resBits.AddBit(false);
                }
                resBits.AddBit(Constants.EncodingTable[r]);
            }

            return(new Base1DCode(resBits, BarcodeType.Codabar, content, Constants.Margin));
        }
Example #4
0
        public static IBarcodeIntCS Encode(string content, bool includeChecksum = true, bool gs1ModeEnabled = false)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (gs1ModeEnabled)
            {
                content = Gs1Encoder.Encode(content, Constants.FNC1);
            }

            char[] contentChars = content.ToCharArray();
            if (contentChars.Length <= 0 || contentChars.Length > 80)
            {
                throw new ArgumentException($"Content length should be between 1 and 80 but got {contentChars.Length}", nameof(content));
            }

            BitList?idxList = GetCodeIndexList(contentChars);

            if (!idxList.HasValue)
            {
                throw new InvalidOperationException($"{content} could not be encoded");
            }

            var result = new BitList();
            var sum    = 0;
            var i      = 0;

            foreach (var idx in idxList.Value.GetBytes())
            {
                if (i == 0)
                {
                    sum = idx;
                }
                else
                {
                    sum += i * idx;
                }
                result.AddBit(Constants.EncodingTable[idx]);
                i++;
            }
            sum = sum % 103;

            if (includeChecksum)
            {
                result.AddBit(Constants.EncodingTable[sum]);
            }
            result.AddBit(Constants.EncodingTable[Constants.StopSymbol]);
            return(new Base1DCodeIntCS(result, BarcodeType.Code128, content, sum, Constants.Margin));
        }
Example #5
0
        public static IBarcodeIntCS Encode(string content, bool includeChecksum, bool fullAsciiMode)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (fullAsciiMode)
            {
                content = Prepare(content);
            }
            else if (content.Contains("*"))
            {
                throw new InvalidOperationException("Invalid data! Try full ASCII mode");
            }

            char checksum = GetChecksum(content);

            var data = new StringBuilder("*");

            data.Append(content);
            if (includeChecksum)
            {
                data.Append(checksum);
            }
            data.Append("*");

            var result = new BitList();
            var i      = 0;

            foreach (char r in data.ToString())
            {
                if (i++ != 0)
                {
                    result.AddBit(false);
                }

                if (!Constants.EncodingTable.TryGetValue(r, out (int value, bool[] data)info))
                {
                    throw new InvalidOperationException("Invalid data! Try full ASCII mode");
                }

                result.AddBit(info.data);
            }

            return(new Base1DCodeIntCS(result, BarcodeType.Code39, content, checksum, Constants.Margin));
        }
Example #6
0
        protected void AddPaddingAndTerminator(ref BitList bits, VersionInfo versionInfo)
        {
            int totalDataBits = versionInfo.TotalDataBytes() * 8;

            for (int i = 0; i < 4 && bits.Length < totalDataBits; i++)
            {
                bits.AddBit(false);
            }

            while (bits.Length % 8 != 0)
            {
                bits.AddBit(false);
            }

            for (int i = 0; bits.Length < totalDataBits; i++)
            {
                bits.AddByte((i % 2) == 0 ? (byte)236 : (byte)17);
            }
        }
Example #7
0
        private static BitList EncodeEan8(string content)
        {
            var result = new BitList();

            result.AddBit(true, false, true);
            var cpos = 0;

            foreach (var r in content)
            {
                Constants.EncodedNumber num = Constants.EncodingTable[r];
                bool[] data = cpos < 4 ? num.LeftOdd : num.Right;
                if (cpos == 4)
                {
                    result.AddBit(false, true, false, true, false);
                }
                result.AddBit(data);
                cpos++;
            }
            result.AddBit(true, false, true);
            return(result);
        }
Example #8
0
        private static BitList EncodeUpcE(string content, UpcENumberSystem numberSystem)
        {
            var result = new BitList();

            // Find the correct parity pattern
            // To find it we need the check digit of the UPC-A for which this UPC-E barcode encodes
            var upcA           = GetUpcAFromUpcE(content, numberSystem);
            var upcACheckDigit = upcA.Last();

            Constants.ParityPatterns parityPatternTable = Constants.ParityPatternTable[upcACheckDigit];
            Constants.Parity[]       parityPattern      = numberSystem == UpcENumberSystem.Zero ?
                                                          parityPatternTable.NumberSystemZero : parityPatternTable.NumberSystemOne;

            // Start bars
            result.AddBit(true, false, true);

            // Data bars
            for (int i = 0; i < content.Length; i++)
            {
                char c = content[i];
                Constants.EncodedNumber num    = Constants.EncodingTable[c];
                Constants.Parity        parity = parityPattern[i];

                if (parity == Constants.Parity.Even)
                {
                    result.AddBit(num.Even);
                }
                else
                {
                    result.AddBit(num.Odd);
                }
            }

            // Stop bars
            result.AddBit(false, true, false, true, false, true);

            return(result);
        }
Example #9
0
        public static IBarcode Encode(string content, bool includeChecksum, bool fullAsciiMode)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (fullAsciiMode)
            {
                content = Prepare(content);
            }
            else if (content.Contains("*"))
            {
                throw new InvalidOperationException("Invalid data! Try full ASCII mode");
            }

            var data = content;

            if (includeChecksum)
            {
                data += GetChecksum(content, 20);
                data += GetChecksum(data, 15);
            }

            data = "*" + data + "*";

            var result = new BitList();

            foreach (var r in data)
            {
                if (!Code93Constants.EncodingTable.TryGetValue(r, out (int value, uint data)info))
                {
                    throw new InvalidOperationException("Invalid data");
                }
                result.AddBits(info.data, 9);
            }
            result.AddBit(true);

            return(new Base1DCode(result, BarcodeType.Code93, content, Code93Constants.Margin));
        }
Example #10
0
        public static IBarcodeIntCS Encode(string content, bool interleaved, bool includeChecksum)
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            if (!Regex.IsMatch(content, @"^[0-9]*$"))
            {
                throw new InvalidOperationException("Can only encode numerical digits (0-9)");
            }

            char checksum = GetChecksum(content);

            if (includeChecksum)
            {
                content = content + checksum;
            }

            if (interleaved && (content.Length % 2) == 1)
            {
                throw new InvalidOperationException("Can only encode an even number of digits in interleaved mode");
            }

            Constants.EncodeInfo mode = Constants.Modes[interleaved];
            var resBits = new BitList();

            resBits.AddBit(mode.Start);

            char?lastChar = null;

            foreach (var r in content)
            {
                bool[] a, b;
                if (interleaved)
                {
                    if (!lastChar.HasValue)
                    {
                        lastChar = r;
                        continue;
                    }
                    else
                    {
                        a        = Constants.EncodingTable[lastChar.Value];
                        b        = Constants.EncodingTable[r];
                        lastChar = null;
                    }
                }
                else
                {
                    a = Constants.EncodingTable[r];
                    b = Constants.NonInterleavedSpace;
                }

                for (var i = 0; i < Constants.PatternWidth; i++)
                {
                    for (var x = 0; x < mode.Widths[a[i]]; x++)
                    {
                        resBits.AddBit(true);
                    }
                    for (var x = 0; x < mode.Widths[b[i]]; x++)
                    {
                        resBits.AddBit(false);
                    }
                }
            }

            resBits.AddBit(mode.End);

            return(new Base1DCodeIntCS(resBits, interleaved ? BarcodeType.TwoOfFiveInterleaved : BarcodeType.TwoOfFive, content, checksum, Constants.Margin));
        }