Skip to content

Commit e7a7eb2

Browse files
authored
Merge pull request #516 from Shane32/code_separation
Refactor QRCodeGenerator into separate files
2 parents e36d34d + 307e6aa commit e7a7eb2

15 files changed

+735
-646
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace QRCoder
4+
{
5+
public partial class QRCodeGenerator
6+
{
7+
private struct AlignmentPattern
8+
{
9+
public int Version;
10+
public List<Point> PatternPositions;
11+
}
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace QRCoder
2+
{
3+
public partial class QRCodeGenerator
4+
{
5+
private struct CodewordBlock
6+
{
7+
public CodewordBlock(byte[] codeWords, byte[] eccWords)
8+
{
9+
this.CodeWords = codeWords;
10+
this.ECCWords = eccWords;
11+
}
12+
13+
public byte[] CodeWords { get; }
14+
public byte[] ECCWords { get; }
15+
}
16+
}
17+
}

QRCoder/QRCodeGenerator.ECCInfo.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace QRCoder
2+
{
3+
public partial class QRCodeGenerator
4+
{
5+
private struct ECCInfo
6+
{
7+
public ECCInfo(int version, ECCLevel errorCorrectionLevel, int totalDataCodewords, int eccPerBlock, int blocksInGroup1,
8+
int codewordsInGroup1, int blocksInGroup2, int codewordsInGroup2)
9+
{
10+
this.Version = version;
11+
this.ErrorCorrectionLevel = errorCorrectionLevel;
12+
this.TotalDataCodewords = totalDataCodewords;
13+
this.ECCPerBlock = eccPerBlock;
14+
this.BlocksInGroup1 = blocksInGroup1;
15+
this.CodewordsInGroup1 = codewordsInGroup1;
16+
this.BlocksInGroup2 = blocksInGroup2;
17+
this.CodewordsInGroup2 = codewordsInGroup2;
18+
}
19+
public int Version { get; }
20+
public ECCLevel ErrorCorrectionLevel { get; }
21+
public int TotalDataCodewords { get; }
22+
public int ECCPerBlock { get; }
23+
public int BlocksInGroup1 { get; }
24+
public int CodewordsInGroup1 { get; }
25+
public int BlocksInGroup2 { get; }
26+
public int CodewordsInGroup2 { get; }
27+
}
28+
}
29+
}

QRCoder/QRCodeGenerator.ECCLevel.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace QRCoder
2+
{
3+
public partial class QRCodeGenerator
4+
{
5+
/// <summary>
6+
/// Error correction level. These define the tolerance levels for how much of the code can be lost before the code cannot be recovered.
7+
/// </summary>
8+
public enum ECCLevel
9+
{
10+
/// <summary>
11+
/// 7% may be lost before recovery is not possible
12+
/// </summary>
13+
L,
14+
/// <summary>
15+
/// 15% may be lost before recovery is not possible
16+
/// </summary>
17+
M,
18+
/// <summary>
19+
/// 25% may be lost before recovery is not possible
20+
/// </summary>
21+
Q,
22+
/// <summary>
23+
/// 30% may be lost before recovery is not possible
24+
/// </summary>
25+
H
26+
}
27+
}
28+
}

QRCoder/QRCodeGenerator.EciMode.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace QRCoder
2+
{
3+
public partial class QRCodeGenerator
4+
{
5+
public enum EciMode
6+
{
7+
Default = 0,
8+
Iso8859_1 = 3,
9+
Iso8859_2 = 4,
10+
Utf8 = 26
11+
}
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace QRCoder
2+
{
3+
public partial class QRCodeGenerator
4+
{
5+
private enum EncodingMode
6+
{
7+
Numeric = 1,
8+
Alphanumeric = 2,
9+
Byte = 4,
10+
Kanji = 8,
11+
ECI = 7
12+
}
13+
}
14+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace QRCoder
5+
{
6+
public partial class QRCodeGenerator
7+
{
8+
private static partial class ModulePlacer
9+
{
10+
private static class MaskPattern
11+
{
12+
public static readonly Dictionary<int, Func<int, int, bool>> Patterns =
13+
new Dictionary<int, Func<int, int, bool>>(8) {
14+
{ 1, MaskPattern.Pattern1 }, {2, MaskPattern.Pattern2 }, {3, MaskPattern.Pattern3 }, {4, MaskPattern.Pattern4 },
15+
{ 5, MaskPattern.Pattern5 }, {6, MaskPattern.Pattern6 }, {7, MaskPattern.Pattern7 }, {8, MaskPattern.Pattern8 }
16+
};
17+
18+
public static bool Pattern1(int x, int y)
19+
{
20+
return (x + y) % 2 == 0;
21+
}
22+
23+
public static bool Pattern2(int x, int y)
24+
{
25+
return y % 2 == 0;
26+
}
27+
28+
public static bool Pattern3(int x, int y)
29+
{
30+
return x % 3 == 0;
31+
}
32+
33+
public static bool Pattern4(int x, int y)
34+
{
35+
return (x + y) % 3 == 0;
36+
}
37+
38+
public static bool Pattern5(int x, int y)
39+
{
40+
return ((int)(Math.Floor(y / 2d) + Math.Floor(x / 3d)) % 2) == 0;
41+
}
42+
43+
public static bool Pattern6(int x, int y)
44+
{
45+
return ((x * y) % 2) + ((x * y) % 3) == 0;
46+
}
47+
48+
public static bool Pattern7(int x, int y)
49+
{
50+
return (((x * y) % 2) + ((x * y) % 3)) % 2 == 0;
51+
}
52+
53+
public static bool Pattern8(int x, int y)
54+
{
55+
return (((x + y) % 2) + ((x * y) % 3)) % 2 == 0;
56+
}
57+
58+
public static int Score(QRCodeData qrCode)
59+
{
60+
int score1 = 0,
61+
score2 = 0,
62+
score3 = 0,
63+
score4 = 0;
64+
var size = qrCode.ModuleMatrix.Count;
65+
66+
//Penalty 1
67+
for (var y = 0; y < size; y++)
68+
{
69+
var modInRow = 0;
70+
var modInColumn = 0;
71+
var lastValRow = qrCode.ModuleMatrix[y][0];
72+
var lastValColumn = qrCode.ModuleMatrix[0][y];
73+
for (var x = 0; x < size; x++)
74+
{
75+
if (qrCode.ModuleMatrix[y][x] == lastValRow)
76+
modInRow++;
77+
else
78+
modInRow = 1;
79+
if (modInRow == 5)
80+
score1 += 3;
81+
else if (modInRow > 5)
82+
score1++;
83+
lastValRow = qrCode.ModuleMatrix[y][x];
84+
85+
86+
if (qrCode.ModuleMatrix[x][y] == lastValColumn)
87+
modInColumn++;
88+
else
89+
modInColumn = 1;
90+
if (modInColumn == 5)
91+
score1 += 3;
92+
else if (modInColumn > 5)
93+
score1++;
94+
lastValColumn = qrCode.ModuleMatrix[x][y];
95+
}
96+
}
97+
98+
99+
//Penalty 2
100+
for (var y = 0; y < size - 1; y++)
101+
{
102+
for (var x = 0; x < size - 1; x++)
103+
{
104+
if (qrCode.ModuleMatrix[y][x] == qrCode.ModuleMatrix[y][x + 1] &&
105+
qrCode.ModuleMatrix[y][x] == qrCode.ModuleMatrix[y + 1][x] &&
106+
qrCode.ModuleMatrix[y][x] == qrCode.ModuleMatrix[y + 1][x + 1])
107+
score2 += 3;
108+
}
109+
}
110+
111+
//Penalty 3
112+
for (var y = 0; y < size; y++)
113+
{
114+
for (var x = 0; x < size - 10; x++)
115+
{
116+
if ((qrCode.ModuleMatrix[y][x] &&
117+
!qrCode.ModuleMatrix[y][x + 1] &&
118+
qrCode.ModuleMatrix[y][x + 2] &&
119+
qrCode.ModuleMatrix[y][x + 3] &&
120+
qrCode.ModuleMatrix[y][x + 4] &&
121+
!qrCode.ModuleMatrix[y][x + 5] &&
122+
qrCode.ModuleMatrix[y][x + 6] &&
123+
!qrCode.ModuleMatrix[y][x + 7] &&
124+
!qrCode.ModuleMatrix[y][x + 8] &&
125+
!qrCode.ModuleMatrix[y][x + 9] &&
126+
!qrCode.ModuleMatrix[y][x + 10]) ||
127+
(!qrCode.ModuleMatrix[y][x] &&
128+
!qrCode.ModuleMatrix[y][x + 1] &&
129+
!qrCode.ModuleMatrix[y][x + 2] &&
130+
!qrCode.ModuleMatrix[y][x + 3] &&
131+
qrCode.ModuleMatrix[y][x + 4] &&
132+
!qrCode.ModuleMatrix[y][x + 5] &&
133+
qrCode.ModuleMatrix[y][x + 6] &&
134+
qrCode.ModuleMatrix[y][x + 7] &&
135+
qrCode.ModuleMatrix[y][x + 8] &&
136+
!qrCode.ModuleMatrix[y][x + 9] &&
137+
qrCode.ModuleMatrix[y][x + 10]))
138+
{
139+
score3 += 40;
140+
}
141+
142+
if ((qrCode.ModuleMatrix[x][y] &&
143+
!qrCode.ModuleMatrix[x + 1][y] &&
144+
qrCode.ModuleMatrix[x + 2][y] &&
145+
qrCode.ModuleMatrix[x + 3][y] &&
146+
qrCode.ModuleMatrix[x + 4][y] &&
147+
!qrCode.ModuleMatrix[x + 5][y] &&
148+
qrCode.ModuleMatrix[x + 6][y] &&
149+
!qrCode.ModuleMatrix[x + 7][y] &&
150+
!qrCode.ModuleMatrix[x + 8][y] &&
151+
!qrCode.ModuleMatrix[x + 9][y] &&
152+
!qrCode.ModuleMatrix[x + 10][y]) ||
153+
(!qrCode.ModuleMatrix[x][y] &&
154+
!qrCode.ModuleMatrix[x + 1][y] &&
155+
!qrCode.ModuleMatrix[x + 2][y] &&
156+
!qrCode.ModuleMatrix[x + 3][y] &&
157+
qrCode.ModuleMatrix[x + 4][y] &&
158+
!qrCode.ModuleMatrix[x + 5][y] &&
159+
qrCode.ModuleMatrix[x + 6][y] &&
160+
qrCode.ModuleMatrix[x + 7][y] &&
161+
qrCode.ModuleMatrix[x + 8][y] &&
162+
!qrCode.ModuleMatrix[x + 9][y] &&
163+
qrCode.ModuleMatrix[x + 10][y]))
164+
{
165+
score3 += 40;
166+
}
167+
}
168+
}
169+
170+
//Penalty 4
171+
int blackModules = 0;
172+
foreach (var bitArray in qrCode.ModuleMatrix)
173+
for (var x = 0; x < size; x++)
174+
if (bitArray[x])
175+
blackModules++;
176+
177+
var percentDiv5 = blackModules * 20 / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count);
178+
var prevMultipleOf5 = Math.Abs(percentDiv5 - 10);
179+
var nextMultipleOf5 = Math.Abs(percentDiv5 - 9);
180+
score4 = Math.Min(prevMultipleOf5, nextMultipleOf5) * 10;
181+
182+
return (score1 + score2) + (score3 + score4);
183+
}
184+
}
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)