Ejemplo n.º 1
0
        /// <summary>
        /// Renders the specified matrix to its graphically representation
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <param name="format">The format.</param>
        /// <param name="content">The encoded content of the barcode which should be included in the image.
        /// That can be the numbers below a 1D barcode or something other.</param>
        /// <param name="options">The options.</param>
        /// <param name="textualInformation">Textual information associated with the image</param>
        /// <returns>Stream containing a png image</returns>
        public Stream Render(BitMatrix matrix, BarcodeFormat format, string content, EncodingOptions options, TextualInformation?textualInformation)
        {
            var includeText = !string.IsNullOrEmpty(content) && !(options?.PureBarcode ?? true);
            var width       = matrix.Width;

            if (includeText)
            {
                var requiredWidth = PngImageTextWriter.GetRequiredWidth(content);
                width = Math.Max(width, requiredWidth);
            }
            using var pngImageWriter = new PngImageWriter(width, matrix.Height, textualInformation);
            var previousScanLine = Span <byte> .Empty;

            for (int y = 0; y < matrix.Height; y++)
            {
                var bitArray        = matrix.getRow(y, null);
                var currentScanLine = bitArray.GetBytes();
                currentScanLine.Negate();
                currentScanLine.ReverseBits();
                pngImageWriter.WriteLine(currentScanLine, previousScanLine);
                previousScanLine = currentScanLine;
            }
            if (includeText)
            {
                PngImageTextWriter.Write(pngImageWriter, content);
            }
            pngImageWriter.Finish();
            return(pngImageWriter.Stream);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Writes <paramref name="text"/> to the supplied <paramref name="pngImageWriter"/>
        /// Will write over white space if available
        /// </summary>
        /// <param name="pngImageWriter"></param>
        /// <param name="text"></param>
        public static void Write(PngImageWriter pngImageWriter, string text)
        {
            pngImageWriter.EnsureBlankScanLines(13);
            pngImageWriter.WriteLine();
            var         textLen = text.Length;
            Span <byte> row     = stackalloc byte[textLen * 11];

            row.Fill(byte.MaxValue);
            for (int c = 0; c < text.Length; c++)
            {
                if (characterBitmaps.TryGetValue(text[c], out var charBitmap))
                {
                    for (int y = 0; y < 11; y++)
                    {
                        row[(y * textLen) + c] = charBitmap[y];
                    }
                }
            }
            for (int i = 0; i < 11; i++)
            {
                pngImageWriter.WriteLine(row.Slice(textLen * i, textLen));
            }
            pngImageWriter.WriteLine();
        }