Exemple #1
0
 internal DataMatrixCode(CodeSize size)
 {
     _size    = size;
     Bounds   = new Bounds(size.Columns, size.Rows);
     Metadata = new Metadata(BarcodeType.DataMatrix, 2);
     _data    = new BitList(size.Rows * size.Columns);
 }
        public static byte[] CalculateEcc(byte[] data, CodeSize size)
        {
            var dataSize = data.Length;
            var result   = new byte[data.Length + size.EccCount];

            Array.Copy(data, result, data.Length);

            for (int block = 0; block < size.BlockCount; block++)
            {
                var dataCnt = size.DataCodewordsForBlock(block);
                var buff    = new int[dataCnt];
                // copy the data for the current block to buff
                var j = 0;
                for (int i = block; i < dataSize; i += size.BlockCount)
                {
                    buff[j] = result[i];
                    j++;
                }
                // calc the error correction codes
                var ecc = ReedSolomonEncoder.Encode(buff, size.ErrorCorrectionCodewordsPerBlock);
                // and append them to the result
                j = 0;
                for (int i = block; i < size.ErrorCorrectionCodewordsPerBlock * size.BlockCount; i += size.BlockCount)
                {
                    result[dataSize + i] = (byte)ecc[j];
                    j++;
                }
            }

            return(result);
        }
        private static DataMatrixCode Render(byte[] data, CodeSize size)
        {
            var codeLayout = new CodeLayout(size);

            codeLayout.SetValues(data);
            return(codeLayout.Merge());
        }
        private static CodeSize GetFixedCodeSizeForData(int fixedNumberOfRows, int dataLength)
        {
            CodeSize codeSize = CodeSizes.All.FirstOrDefault(x => x.Rows == fixedNumberOfRows)
                                ?? throw new InvalidOperationException($"No code size found with fixed number of rows {fixedNumberOfRows}");

            if (codeSize.DataCodewords < dataLength)
            {
                throw new InvalidOperationException($"The fixed code size does not fit {dataLength} codewords");
            }
            return(codeSize);
        }
        public static IBarcode Encode(string content, int?fixedNumberOfRows = null, bool gs1ModeEnabled = false)
        {
            var data = gs1ModeEnabled
                ? EncodeGs1(content)
                : EncodeText(content);

            CodeSize size = fixedNumberOfRows.HasValue
                ? GetFixedCodeSizeForData(fixedNumberOfRows.Value, data.Length)
                : GetSmallestCodeSizeForData(data.Length);

            data = AddPadding(data, size.DataCodewords);
            data = ErrorCorrection.CalculateEcc(data, size);
            var code = Render(data, size)
                       ?? throw new InvalidOperationException("Unable to render barcode");

            code.Content = content;
            return(code);
        }
Exemple #6
0
 public CodeLayout(CodeSize size)
 {
     Size   = size;
     Matrix = new BitList(size.MatrixColumns * size.MatrixRows);
     Occupy = new BitList(size.MatrixColumns * size.MatrixRows);
 }