Beispiel #1
0
        /// <summary>
        /// Method TryCreateQRCode makes attempt conversion data
        /// </summary>
        /// <param name="QRdata">array will contain conversion data</param>
        /// <returns>Returns true if attempt was success and false if not</returns>
        private bool TryCreateQRCode(out byte[] QRdata)
        {
            QRdata = default;

            byte[][] dataBlocks;
            byte[][] correctionBlocks;

            if (serviceData == null)
            {
                serviceData = new ServiceData(CodingType, CorrectionLevel);
            }
            serviceData.CodingType      = CodingType;
            serviceData.CorrectionLevel = CorrectionLevel;

            if (coder.TryEncoding(out byte[] binaryData))
            {
                if (serviceData.TryGetData(binaryData.Length, coder.DataLength, out byte[] binaryServiceData))
                {
                    binaryData       = DataCombiner.FillingData(serviceData.Capacity, binaryServiceData, binaryData);
                    dataBlocks       = DataDivider.DivideByBlocks(binaryData, CorrectionLevel, serviceData.Version);
                    correctionBlocks = Correction.GetCorrectionBlocks(dataBlocks, CorrectionLevel, serviceData.Version);
                    QRdata           = DataCombiner.MergeBlocks(dataBlocks, correctionBlocks);
                    return(true);
                }
                ErrorHandler?.Invoke("Превышен лимит данных! \r\nУменьшите кол-во данных или используйте другой уровень коррекции");
                return(false);
            }
        /// <summary>
        /// Method TryEncoding encoding input data to bits sequence
        /// </summary>
        /// <param name="result"> array consisting sequence of bits of encoded data </param>
        /// <returns>Return true if input data not empty, and false if not </returns>
        internal override bool TryEncoding(out byte[] result)
        {
            if (!IsValid(data))
            {
                result = default;
                return(false);
            }

            //Converting data from string to array of bytes
            result     = DataCombiner.ConvertByteToBinary(data);
            dataLength = result.Length / 8;
            return(true);
        }
        /// <summary>
        /// Method DivideByBlocks dividing data by blocks
        /// </summary>
        /// <param name="data"></param>
        /// <param name="correctionLevel"></param>
        /// <param name="version"></param>
        /// <returns>Returns data divided by blocks</returns>
        internal static byte[][] DivideByBlocks(byte[] data, CorrectionLevel correctionLevel, int version)
        {
            byte[][] dataBlocks;
            int      blocksCount = GetBlocksCount(correctionLevel, version);

            dataBlocks = new byte[blocksCount][];
            int[] blocksLengths = GetBlocksLengths(blocksCount, data.Length);
            for (int i = 0; i < dataBlocks.Length; i++)
            {
                dataBlocks[i] = new byte[blocksLengths[i]];
            }
            dataBlocks = DataCombiner.FillingBlocks(data, dataBlocks);
            return(dataBlocks);
        }