Skip to content

Commit e91953e

Browse files
committed
Added GetGraphicSmall to ASCIIQRCode.
Credits for qrcode-terminal.
1 parent cecf206 commit e91953e

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

QRCoder/ASCIIQRCode.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,61 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
6262
}
6363
return qrCode.ToArray();
6464
}
65+
66+
/// <summary>
67+
/// Returns a strings that contains the resulting QR code as ASCII chars.
68+
/// </summary>
69+
/// <returns></returns>
70+
public string GetGraphicSmall(bool drawQuietZones = true)
71+
{
72+
string endOfLine = "\n";
73+
bool BLACK = true, WHITE = false;
74+
75+
var platte = new
76+
{
77+
WHITE_ALL = "\u2588",
78+
WHITE_BLACK = "\u2580",
79+
BLACK_WHITE = "\u2584",
80+
BLACK_ALL = " ",
81+
};
82+
83+
var moduleData = QrCodeData.ModuleMatrix;
84+
var lineBuilder = new StringBuilder();
85+
86+
var quietZonesModifier = (drawQuietZones ? 4 : 8);
87+
var quietZonesOffset = (int)(quietZonesModifier * 0.5);
88+
var sideLength = (QrCodeData.ModuleMatrix.Count - quietZonesModifier);
89+
90+
for (var row = 0; row < sideLength; row = row + 2)
91+
{
92+
for (var col = 0; col < moduleData.Count - quietZonesModifier; col++)
93+
{
94+
try
95+
{
96+
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset];
97+
var next = moduleData[col + quietZonesOffset][(row + 1) + quietZonesOffset];
98+
if (current == WHITE && next == WHITE)
99+
lineBuilder.Append(platte.WHITE_ALL);
100+
else if (current == WHITE && next == BLACK)
101+
lineBuilder.Append(platte.WHITE_BLACK);
102+
else if (current == BLACK && next == WHITE)
103+
lineBuilder.Append(platte.BLACK_WHITE);
104+
else
105+
lineBuilder.Append(platte.BLACK_ALL);
106+
}
107+
catch (Exception)
108+
{
109+
if (drawQuietZones)
110+
lineBuilder.Append(platte.WHITE_BLACK);
111+
else
112+
lineBuilder.Append(platte.BLACK_ALL);
113+
}
114+
115+
}
116+
lineBuilder.Append(endOfLine);
117+
}
118+
return lineBuilder.ToString();
119+
}
65120
}
66121

67122

QRCoderTests/AsciiQRCodeRendererTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ public void can_render_ascii_qrcode()
2424
asciiCode.ShouldBe(targetCode);
2525
}
2626

27+
[Fact]
28+
[Category("QRRenderer/AsciiQRCode")]
29+
public void can_render_small_ascii_qrcode()
30+
{
31+
var targetCode = "█████████████████████████\n██ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ██\n██ █ █ █▄█ █▄█ █ █ ██\n██ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ██\n██▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄██\n██ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀██\n██▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄███\n███▄▄▄▄█▄▄▄████▀▀ █▄█▄██\n██ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄██\n██ █ █ █ ▀ █▄▀█ ██▄█▄██\n██ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄██\n██▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄██\n█████████████████████████\n";
32+
33+
//Create QR code
34+
var gen = new QRCodeGenerator();
35+
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
36+
var asciiCode = new AsciiQRCode(data).GetGraphicSmall();
37+
38+
asciiCode.ShouldBe(targetCode);
39+
}
40+
2741
[Fact]
2842
[Category("QRRenderer/AsciiQRCode")]
2943
public void can_render_ascii_qrcode_without_quietzones()

0 commit comments

Comments
 (0)