Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASCIQRCode Small #384

Merged
merged 8 commits into from
Apr 22, 2024
Merged
55 changes: 55 additions & 0 deletions QRCoder/ASCIIQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,61 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
}
return qrCode.ToArray();
}

/// <summary>
/// Returns a strings that contains the resulting QR code as ASCII chars.
/// </summary>
/// <returns></returns>
public string GetGraphicSmall(bool drawQuietZones = true)
{
string endOfLine = "\n";
bool BLACK = true, WHITE = false;

var platte = new
{
WHITE_ALL = "\u2588",
WHITE_BLACK = "\u2580",
BLACK_WHITE = "\u2584",
BLACK_ALL = " ",
};

var moduleData = QrCodeData.ModuleMatrix;
var lineBuilder = new StringBuilder();

var quietZonesModifier = (drawQuietZones ? 4 : 8);
var quietZonesOffset = (int)(quietZonesModifier * 0.5);
var sideLength = (QrCodeData.ModuleMatrix.Count - quietZonesModifier);

for (var row = 0; row < sideLength; row = row + 2)
{
for (var col = 0; col < moduleData.Count - quietZonesModifier; col++)
{
try
{
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset];
var next = moduleData[col + quietZonesOffset][(row + 1) + quietZonesOffset];
if (current == WHITE && next == WHITE)
lineBuilder.Append(platte.WHITE_ALL);
else if (current == WHITE && next == BLACK)
lineBuilder.Append(platte.WHITE_BLACK);
else if (current == BLACK && next == WHITE)
lineBuilder.Append(platte.BLACK_WHITE);
else
lineBuilder.Append(platte.BLACK_ALL);
}
catch (Exception)
{
if (drawQuietZones)
lineBuilder.Append(platte.WHITE_BLACK);
else
lineBuilder.Append(platte.BLACK_ALL);
}

}
lineBuilder.Append(endOfLine);
}
return lineBuilder.ToString();
}
}


Expand Down
14 changes: 14 additions & 0 deletions QRCoderTests/AsciiQRCodeRendererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public void can_render_ascii_qrcode()
asciiCode.ShouldBe(targetCode);
}

[Fact]
[Category("QRRenderer/AsciiQRCode")]
public void can_render_small_ascii_qrcode()
{
var targetCode = "█████████████████████████\n██ ▄▄▄▄▄ █▀▄█ ▀█ ▄▄▄▄▄ ██\n██ █ █ █▄█ █▄█ █ █ ██\n██ █▄▄▄█ █▄▀▀▀▀█ █▄▄▄█ ██\n██▄▄▄▄▄▄▄█ █ ▀▄█▄▄▄▄▄▄▄██\n██ ▄▄ █▄ ██▀ ▄▄▄▀ ▀ ▄▀██\n██▀█▄█ █▄ ▄ ▀▄▀ █▄█▄▄███\n███▄▄▄▄█▄▄▄████▀▀ █▄█▄██\n██ ▄▄▄▄▄ █▄▄█▄▄▀ ▀ ▄█▄▄██\n██ █ █ █ ▀ █▄▀█ ██▄█▄██\n██ █▄▄▄█ █ ▀▄▀ █▄█▄ █ ▄██\n██▄▄▄▄▄▄▄█▄▄▄█████▄█▄▄▄██\n█████████████████████████\n";

//Create QR code
var gen = new QRCodeGenerator();
var data = gen.CreateQrCode("A05", QRCodeGenerator.ECCLevel.Q);
var asciiCode = new AsciiQRCode(data).GetGraphicSmall();

asciiCode.ShouldBe(targetCode);
}

[Fact]
[Category("QRRenderer/AsciiQRCode")]
public void can_render_ascii_qrcode_without_quietzones()
Expand Down