Exemplo n.º 1
0
        /// <summary>
        /// Creates a segment representing the specified text string.
        /// The segment is encoded in alphanumeric mode.
        /// <para>
        /// Allowed characters are: 0 to 9, A to Z (uppercase only), space,
        /// dollar, percent, asterisk, plus, hyphen, period, slash, colon.
        /// </para>
        /// </summary>
        /// <param name="text">The text to encode, consisting of allowed characters only.</param>
        /// <returns>The created segment containing the text.</returns>
        /// <exception cref="ArgumentNullException"><c>text</c> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><c>text</c> contains non-encodable characters.</exception>
        public static QrSegment MakeAlphanumeric(string text)
        {
            Objects.RequireNonNull(text);
            if (!AlphanumericRegex.IsMatch(text))
            {
                throw new ArgumentOutOfRangeException(nameof(text), "String contains unencodable characters in alphanumeric mode");
            }

            BitArray ba = new BitArray(0);
            int      i;

            for (i = 0; i <= text.Length - 2; i += 2)
            {
                // Process groups of 2
                uint temp = (uint)AlphanumericCharset.IndexOf(text[i]) * 45;
                temp += (uint)AlphanumericCharset.IndexOf(text[i + 1]);
                ba.AppendBits(temp, 11);
            }
            if (i < text.Length)  // 1 character remaining
            {
                ba.AppendBits((uint)AlphanumericCharset.IndexOf(text[i]), 6);
            }

            return(new QrSegment(Mode.Alphanumeric, text.Length, ba));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a list of zero or more segments representing the specified text string.
        /// <para>
        /// The text may contain the full range of Unicode characters.
        /// </para>
        /// <para>
        /// The result may multiple segments with various encoding modes in order to minimize the length of the bit stream.
        /// </para>
        /// </summary>
        /// <param name="text">The text to be encoded.</param>
        /// <returns>The created mutable list of segments representing the specified text.</returns>
        /// <exception cref="ArgumentNullException"><c>text</c> is <c>null</c>.</exception>
        /// <remarks>
        /// The current implementation does not use multiple segments.
        /// </remarks>
        public static List <QrSegment> MakeSegments(string text)
        {
            Objects.RequireNonNull(text);

            // Select the most efficient segment encoding automatically
            var result = new List <QrSegment>();

            if (text == "")
            {
                // Leave result empty
            }
            else if (NumericRegex.IsMatch(text))
            {
                result.Add(MakeNumeric(text));
            }
            else if (AlphanumericRegex.IsMatch(text))
            {
                result.Add(MakeAlphanumeric(text));
            }
            else
            {
                result.Add(MakeBytes(Encoding.UTF8.GetBytes(text)));
            }

            return(result);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Tests whether the specified string can be encoded as a segment in alphanumeric mode.
 /// <para>
 /// A string is encodable iff each character is in the range "0" to "9", "A" to "Z" (uppercase only),
 /// space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
 /// </para>
 /// </summary>
 /// <param name="text">the string to test for encodability (not <c>null</c>)</param>
 /// <returns><c>true</c> iff each character is in the alphanumeric mode character set.</returns>
 /// <exception cref="NullReferenceException">if the string is <c>null</c></exception>
 /// <seealso cref="MakeAlphanumeric(string)"/>
 public static bool IsAlphanumeric(string text)
 {
     return(AlphanumericRegex.IsMatch(text));
 }