Example #1
0
        /// <summary>
        /// Creates a QR code representing the specified binary data using the specified error correction level.
        /// <para>
        /// This function encodes the data in the binary segment mode. The maximum number of
        /// bytes allowed is 2953. The smallest possible QR code version is automatically chosen.
        /// The resulting ECC level will be higher than the one specified if it can be achieved without increasing the size (version).
        /// </para>
        /// </summary>
        /// <param name="data">The binary data to encode.</param>
        /// <param name="ecl">The minimum error correction level to use.</param>
        /// <returns>The created QR code representing the specified data.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="data"/> or <paramref name="ecl"/> is <c>null</c>.</exception>
        /// <exception cref="DataTooLongException">The specified data is too long to fit in the largest QR code size (version)
        /// at the specified error correction level.</exception>
        public static QrCode EncodeBinary(byte[] data, Ecc ecl)
        {
            Objects.RequireNonNull(data);
            Objects.RequireNonNull(ecl);
            QrSegment seg = QrSegment.MakeBytes(data);

            return(EncodeSegments(new List <QrSegment> {
                seg
            }, ecl));
        }
        private static List <QrSegment> SplitIntoSegments(int[] codePoints, Mode[] charModes)
        {
            if (codePoints.Length == 0)
            {
                throw new ArgumentException();
            }

            var result = new List <QrSegment>();

            // Accumulate run of modes
            var curMode = charModes[0];
            int start   = 0;

            for (int i = 1; ; i++)
            {
                if (i < codePoints.Length && charModes[i] == curMode)
                {
                    continue;
                }

                var s = FromCodePoint(codePoints, start, i - start);
                if (curMode == Mode.BYTE)
                {
                    result.Add(QrSegment.MakeBytes(Encoding.UTF8.GetBytes(s)));
                }
                else if (curMode == Mode.NUMERIC)
                {
                    result.Add(QrSegment.MakeNumeric(s));
                }
                else if (curMode == Mode.ALPHANUMERIC)
                {
                    result.Add(QrSegment.MakeAlphanumeric(s));
                }
                else if (curMode == Mode.KANJI)
                {
                    result.Add(MakeKanji(s));
                }
                else
                {
                    throw new ApplicationException();
                }

                if (i >= codePoints.Length)
                {
                    return(result);
                }
                curMode = charModes[i];
                start   = i;
            }
        }