示例#1
0
        public void Encode_NonAsciiCharacterInAsciiMode_ShouldThrowException()
        {
            Action action = () => Code39Encoder.Encode("ù", false, true);

            action.Should().Throw <InvalidOperationException>()
            .WithMessage("Only ASCII strings can be encoded");
        }
示例#2
0
        public void Encode_UnderscoreInNonAsciiMode_ShouldThrowException()
        {
            Action action = () => Code39Encoder.Encode("_", false, false);

            action.Should().Throw <InvalidOperationException>()
            .WithMessage("Invalid data! Try full ASCII mode");
        }
        public void Code39EncoderText_SetAndGetValue_ReturnsDefaultValue()
        {
            // Arrange, Act
            var code39Encoder = new Code39Encoder();

            // Assert
            code39Encoder.Text.ShouldBeNull();
        }
        public void Code39EncoderText_SetAndGetValue_ReturnsTheSetValue()
        {
            // Arrange, Act
            var code39Encoder = new Code39Encoder
            {
                Text = DummyStringValue
            };

            // Assert
            code39Encoder.Text.ShouldBe(DummyStringValue);
        }
示例#5
0
        public void Encode(bool includeChecksum, bool fullAsciiMode, string data, string testResult)
        {
            IBarcodeIntCS code = Code39Encoder.Encode(data, includeChecksum, fullAsciiMode);

            code.Bounds.X.Should().Be(testResult.Length);
            code.Bounds.Y.Should().Be(1);
            code.Metadata.CodeKind.Should().Be(BarcodeType.Code39);
            code.Metadata.Dimensions.Should().Be(1);

            string encoded = string.Empty;
            int    i       = 0;

            foreach (var r in testResult)
            {
                encoded += code.At(i++, 0) ? "1" : "0";
            }
            encoded.Should().Be(testResult);
        }
示例#6
0
        /// <summary>
        /// Encodes an HIBC primary data string.
        /// </summary>
        /// <param name="primary">The primary data string, consisting of the LIC, the PCN, and the unit of measure digit.</param>
        /// <param name="secondary">An optional <see cref="Barcodes.HIBC.SecondaryData"/> object containing secondary data.</param>
        /// <param name="primaryMode">The symbology to use for the primary barcode.  Must be <see cref="Barcodes.HIBC.PrimaryEncodingMode.Code39"/> or <see cref="Barcodes.HIBC.PrimaryEncodingMode.Code128"/>.</param>
        /// <param name="secondaryMode">The symbology to use for the secondary barcode.  May be any <see cref="Barcodes.HIBC.SecondaryEncodingMode"/> value other than <see cref="Barcodes.HIBC.SecondaryEncodingMode.EAN128"/>, however,
        /// if <paramref name="secondary"/> is null, this value must be <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</param>
        /// <returns>An array of <see cref="Barcodes.IBarcodeEncoder"/> objects, containing one or two pre-set encoders.</returns>
        /// <remarks>This is the same function as <see cref="Barcodes.HIBC.HIBCEncoder.EncodeHIBC(string, SecondaryData, PrimaryEncodingMode, SecondaryEncodingMode)"/>, except
        /// that the primary data (LIC, PCN, and Unit of Measure) is combined into one parameter in the alternate method.</remarks>
        /// <exception cref="System.ArgumentException">There was an error in the primary data.</exception>
        /// <exception cref="System.InvalidOperationException">The <paramref name="primaryMode"/> or <paramref name="secondaryMode"/>
        /// values are invalid for the type of data provided (HIBC vs. GS1).</exception>
        /// <exception cref="System.NullReferenceException">Argument <paramref name="secondary"/> is null and <paramref name="secondaryMode"/> is not <see cref="Barcodes.HIBC.SecondaryEncodingMode.None"/>.</exception>
        public static IBarcodeEncoder[] EncodeHIBC(string primary, SecondaryDataHibc secondary, PrimaryEncodingMode primaryMode, SecondaryEncodingMode secondaryMode)
        {
            if (primaryMode == PrimaryEncodingMode.EAN128 || primaryMode == PrimaryEncodingMode.I2of5)
            {
                throw new InvalidOperationException("The primary mode can only be Code39 or Code128.");
            }
            if (secondaryMode == SecondaryEncodingMode.EAN128)
            {
                throw new InvalidOperationException("The secondary mode can only be Code39, Code128, Combined, or None.");
            }
            if (secondaryMode != SecondaryEncodingMode.None && secondary == null)
            {
                throw new NullReferenceException("The secondary encoding mode is not None and secondary is null.");
            }

            Regex primaryCheck = new Regex("[A-Z][A-Z0-9]{3}[A-Z0-9]{1,13}[0-9]");

            primary = primary.ToUpper();
            if (!primaryCheck.IsMatch(primary))
            {
                throw new ArgumentException("The specified primary data string (LIC+PCN+Unit) is invalid. The proper format is [A-Z][A-Z0-9]{3}[A-Z0-9]{1,13}[0-9].", "primary");
            }

            primary = "+" + primary;
            byte checkSum = CalculateHIBCCheck(primary);

            string secondaryEncoding = "";

            if (secondary != null)
            {
                if (secondary.HIBCShortEncoded != null)
                {
                    secondaryEncoding = secondary.HIBCShortEncoded;
                }
                else
                {
                    secondaryEncoding = secondary.HIBCEncoded;
                }
            }

            if (secondaryMode == SecondaryEncodingMode.Combined)
            {
                primary  = primary + "/" + secondaryEncoding.Substring(1);
                checkSum = CalculateHIBCCheck(primary);
                primary += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"[checkSum];
            }
            else
            {
                char checkChar = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"[checkSum];
                primary           += checkChar;
                secondaryEncoding += checkChar;
                checkSum           = CalculateHIBCCheck(secondaryEncoding);
                secondaryEncoding += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"[checkSum];
            }

            IBarcodeEncoder primaryEncoder, secondaryEncoder;

            if (primaryMode == PrimaryEncodingMode.Code39)
            {
                primaryEncoder      = new Code39Encoder();
                primaryEncoder.Text = primary;
                ((Code39Generator)primaryEncoder.Generator).Text = "*" + primary.Replace(' ', '_') + "*";
            }
            else    //Code 128
            {
                primaryEncoder      = new Code128Encoder();
                primaryEncoder.Text = primary;
                ((Code128Generator)primaryEncoder.Generator).Text = "*" + primary.Replace(' ', '_') + "*";
            }

            if (secondaryMode == SecondaryEncodingMode.Combined || secondaryMode == SecondaryEncodingMode.None)
            {
                return new IBarcodeEncoder[] { primaryEncoder }
            }
            ;

            if (secondaryMode == SecondaryEncodingMode.Code39)
            {
                secondaryEncoder = new Code39Encoder();

                secondaryEncoder.Text = secondaryEncoding;
                ((Code39Generator)secondaryEncoder.Generator).Text = "*" + secondaryEncoding.Replace(' ', '_') + "*";
            }
            else
            {
                secondaryEncoder      = new Code128Encoder();
                secondaryEncoder.Text = secondaryEncoding;
                ((Code128Generator)secondaryEncoder.Generator).Text = "*" + secondaryEncoding.Replace(' ', '_') + "*";
            }

            return(new IBarcodeEncoder[] { primaryEncoder, secondaryEncoder });
        }
示例#7
0
        public void Encode_UnderscoreInAsciiMode_ShouldNotThrowException()
        {
            Action action = () => Code39Encoder.Encode("_", false, true);

            action.Should().NotThrow();
        }
示例#8
0
        public void Encode_Checksum()
        {
            IBarcodeIntCS code = Code39Encoder.Encode("5B79AN", true, true);

            code.Checksum.Should().Be('M');
        }