|
| 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