using System; using System.Collections.Generic; using System.Text; using static QRCoder.QRCodeGenerator; namespace QRCoder { public class AsciiQRCode : AbstractQRCode, IDisposable { /// /// Constructor without params to be used in COM Objects connections /// public AsciiQRCode() { } public AsciiQRCode(QRCodeData data) : base(data) { } /// /// Returns a strings that contains the resulting QR code as ASCII chars. /// /// Number of repeated darkColorString/whiteSpaceString per module. /// public string GetGraphic(int repeatPerModule) { return string.Join("\n", GetLineByLineGraphic(repeatPerModule)); } /// /// Returns a strings that contains the resulting QR code as ASCII chars. /// /// Number of repeated darkColorString/whiteSpaceString per module. /// String for use as dark color modules. In case of string make sure whiteSpaceString has the same length. /// String for use as white modules (whitespace). In case of string make sure darkColorString has the same length. /// End of line separator. (Default: \n) /// public string GetGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString, string endOfLine = "\n") { return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString)); } /// /// Returns an array of strings that contains each line of the resulting QR code as ASCII chars. /// /// Number of repeated darkColorString/whiteSpaceString per module. /// public string[] GetLineByLineGraphic(int repeatPerModule) { return GetLineByLineGraphic(repeatPerModule, "██", " "); } /// /// Returns an array of strings that contains each line of the resulting QR code as ASCII chars. /// /// Number of repeated darkColorString/whiteSpaceString per module. /// String for use as dark color modules. In case of string make sure whiteSpaceString has the same length. /// String for use as white modules (whitespace). In case of string make sure darkColorString has the same length. /// public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString) { var qrCode = new List(); //We need to adjust the repeatPerModule based on number of characters in darkColorString //(we assume whiteSpaceString has the same number of characters) //to keep the QR code as square as possible. var adjustmentValueForNumberOfCharacters = darkColorString.Length / 2 != 1 ? darkColorString.Length / 2 : 0; var verticalNumberOfRepeats = repeatPerModule + adjustmentValueForNumberOfCharacters; var sideLength = QrCodeData.ModuleMatrix.Count * verticalNumberOfRepeats; for (var y = 0; y < sideLength; y++) { bool emptyLine = true; var lineBuilder = new StringBuilder(); for (var x = 0; x < QrCodeData.ModuleMatrix.Count; x++) { var module = QrCodeData.ModuleMatrix[x][(y + verticalNumberOfRepeats) / verticalNumberOfRepeats - 1]; for (var i = 0; i < repeatPerModule; i++) { lineBuilder.Append(module ? darkColorString : whiteSpaceString); } if (module) { emptyLine = false; } } if (!emptyLine) { qrCode.Add(lineBuilder.ToString()); } } return qrCode.ToArray(); } } public static class AsciiQRCodeHelper { public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorString, string whiteSpaceString, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, string endOfLine = "\n") { using (var qrGenerator = new QRCodeGenerator()) using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion)) using (var qrCode = new AsciiQRCode(qrCodeData)) return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, endOfLine); } } }