@@ -14,17 +14,7 @@ public AsciiQRCode() { }
14
14
15
15
public AsciiQRCode ( QRCodeData data ) : base ( data ) { }
16
16
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
+
28
18
/// <summary>
29
19
/// Returns a strings that contains the resulting QR code as ASCII chars.
30
20
/// </summary>
@@ -33,22 +23,11 @@ public string GetGraphic(int repeatPerModule)
33
23
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
34
24
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
35
25
/// <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 " )
48
27
{
49
- return GetLineByLineGraphic ( repeatPerModule , "██" , " " ) ;
28
+ return string . Join ( endOfLine , GetLineByLineGraphic ( repeatPerModule , darkColorString , whiteSpaceString , drawQuietZones ) ) ;
50
29
}
51
-
30
+
52
31
53
32
/// <summary>
54
33
/// 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)
57
36
/// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
58
37
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
59
38
/// <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 )
61
40
{
62
41
var qrCode = new List < string > ( ) ;
63
42
//We need to adjust the repeatPerModule based on number of characters in darkColorString
64
43
//(we assume whiteSpaceString has the same number of characters)
65
44
//to keep the QR code as square as possible.
45
+ var quietZonesModifier = ( drawQuietZones ? 0 : 8 ) ;
46
+ var quietZonesOffset = ( int ) ( quietZonesModifier * 0.5 ) ;
66
47
var adjustmentValueForNumberOfCharacters = darkColorString . Length / 2 != 1 ? darkColorString . Length / 2 : 0 ;
67
48
var verticalNumberOfRepeats = repeatPerModule + adjustmentValueForNumberOfCharacters ;
68
- var sideLength = QrCodeData . ModuleMatrix . Count * verticalNumberOfRepeats ;
49
+ var sideLength = ( QrCodeData . ModuleMatrix . Count - quietZonesModifier ) * verticalNumberOfRepeats ;
69
50
for ( var y = 0 ; y < sideLength ; y ++ )
70
51
{
71
52
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 ++ )
74
54
{
75
- var module = QrCodeData . ModuleMatrix [ x ] [ ( y + verticalNumberOfRepeats ) / verticalNumberOfRepeats - 1 ] ;
76
-
55
+ var module = QrCodeData . ModuleMatrix [ x + quietZonesOffset ] [ ( ( y + verticalNumberOfRepeats ) / verticalNumberOfRepeats - 1 ) + quietZonesOffset ] ;
77
56
for ( var i = 0 ; i < repeatPerModule ; i ++ )
78
57
{
79
58
lineBuilder . Append ( module ? darkColorString : whiteSpaceString ) ;
@@ -85,14 +64,15 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
85
64
}
86
65
}
87
66
67
+
88
68
public static class AsciiQRCodeHelper
89
69
{
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 )
91
71
{
92
72
using ( var qrGenerator = new QRCodeGenerator ( ) )
93
73
using ( var qrCodeData = qrGenerator . CreateQrCode ( plainText , eccLevel , forceUtf8 , utf8BOM , eciMode , requestedVersion ) )
94
74
using ( var qrCode = new AsciiQRCode ( qrCodeData ) )
95
- return qrCode . GetGraphic ( pixelsPerModule , darkColorString , whiteSpaceString , endOfLine ) ;
75
+ return qrCode . GetGraphic ( pixelsPerModule , darkColorString , whiteSpaceString , drawQuietZones , endOfLine ) ;
96
76
}
97
77
}
98
78
}
0 commit comments