Skip to content

Commit 9a06a55

Browse files
committed
Added new parameter "drawQuietZones"
Added new parameter "drawQuietZones" which enables to suppress the drawing of the quietzones (white border) around the QR code graphic
1 parent e41c2b0 commit 9a06a55

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

QRCoder/ASCIIQRCode.cs

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@ public AsciiQRCode() { }
1414

1515
public AsciiQRCode(QRCodeData data) : base(data) { }
1616

17-
/// <summary>
18-
/// Returns a strings that contains the resulting QR code as ASCII chars.
19-
/// </summary>
20-
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
21-
/// <returns></returns>
22-
public string GetGraphic(int repeatPerModule)
23-
{
24-
return string.Join("\n", GetLineByLineGraphic(repeatPerModule));
25-
}
26-
27-
17+
2818
/// <summary>
2919
/// Returns a strings that contains the resulting QR code as ASCII chars.
3020
/// </summary>
@@ -33,22 +23,11 @@ public string GetGraphic(int repeatPerModule)
3323
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
3424
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
3525
/// <returns></returns>
36-
public string GetGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString, string endOfLine = "\n")
37-
{
38-
return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString));
39-
}
40-
41-
42-
/// <summary>
43-
/// Returns an array of strings that contains each line of the resulting QR code as ASCII chars.
44-
/// </summary>
45-
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
46-
/// <returns></returns>
47-
public string[] GetLineByLineGraphic(int repeatPerModule)
26+
public string GetGraphic(int repeatPerModule, string darkColorString = "██", string whiteSpaceString = " ", bool drawQuietZones = true, string endOfLine = "\n")
4827
{
49-
return GetLineByLineGraphic(repeatPerModule, "██", " ");
28+
return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString, drawQuietZones));
5029
}
51-
30+
5231

5332
/// <summary>
5433
/// Returns an array of strings that contains each line of the resulting QR code as ASCII chars.
@@ -57,23 +36,23 @@ public string[] GetLineByLineGraphic(int repeatPerModule)
5736
/// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
5837
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
5938
/// <returns></returns>
60-
public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString)
39+
public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString = "██", string whiteSpaceString = " ", bool drawQuietZones = true)
6140
{
6241
var qrCode = new List<string>();
6342
//We need to adjust the repeatPerModule based on number of characters in darkColorString
6443
//(we assume whiteSpaceString has the same number of characters)
6544
//to keep the QR code as square as possible.
45+
var quietZonesModifier = (drawQuietZones ? 0 : 8);
46+
var quietZonesOffset = (int)(quietZonesModifier * 0.5);
6647
var adjustmentValueForNumberOfCharacters = darkColorString.Length / 2 != 1 ? darkColorString.Length / 2 : 0;
6748
var verticalNumberOfRepeats = repeatPerModule + adjustmentValueForNumberOfCharacters;
68-
var sideLength = QrCodeData.ModuleMatrix.Count * verticalNumberOfRepeats;
49+
var sideLength = (QrCodeData.ModuleMatrix.Count - quietZonesModifier) * verticalNumberOfRepeats;
6950
for (var y = 0; y < sideLength; y++)
7051
{
7152
var lineBuilder = new StringBuilder();
72-
73-
for (var x = 0; x < QrCodeData.ModuleMatrix.Count; x++)
53+
for (var x = 0; x < QrCodeData.ModuleMatrix.Count - quietZonesModifier; x++)
7454
{
75-
var module = QrCodeData.ModuleMatrix[x][(y + verticalNumberOfRepeats) / verticalNumberOfRepeats - 1];
76-
55+
var module = QrCodeData.ModuleMatrix[x + quietZonesOffset][((y + verticalNumberOfRepeats) / verticalNumberOfRepeats - 1)+quietZonesOffset];
7756
for (var i = 0; i < repeatPerModule; i++)
7857
{
7958
lineBuilder.Append(module ? darkColorString : whiteSpaceString);
@@ -85,14 +64,15 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
8564
}
8665
}
8766

67+
8868
public static class AsciiQRCodeHelper
8969
{
90-
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")
70+
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", bool drawQuietZones = true)
9171
{
9272
using (var qrGenerator = new QRCodeGenerator())
9373
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
9474
using (var qrCode = new AsciiQRCode(qrCodeData))
95-
return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, endOfLine);
75+
return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, drawQuietZones, endOfLine);
9676
}
9777
}
9878
}

0 commit comments

Comments
 (0)