You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitMatrixParser.cs 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using ZXing.Common;
  17. namespace ZXing.QrCode.Internal
  18. {
  19. /// <author>Sean Owen</author>
  20. sealed class BitMatrixParser
  21. {
  22. private readonly BitMatrix bitMatrix;
  23. private Version parsedVersion;
  24. private FormatInformation parsedFormatInfo;
  25. private bool mirrored;
  26. /// <param name="bitMatrix">{@link BitMatrix} to parse</param>
  27. /// <throws>ReaderException if dimension is not >= 21 and 1 mod 4</throws>
  28. internal static BitMatrixParser createBitMatrixParser(BitMatrix bitMatrix)
  29. {
  30. int dimension = bitMatrix.Height;
  31. if (dimension < 21 || (dimension & 0x03) != 1)
  32. {
  33. return null;
  34. }
  35. return new BitMatrixParser(bitMatrix);
  36. }
  37. private BitMatrixParser(BitMatrix bitMatrix)
  38. {
  39. // Should only be called from createBitMatrixParser with the important checks before
  40. this.bitMatrix = bitMatrix;
  41. }
  42. /// <summary> <p>Reads format information from one of its two locations within the QR Code.</p>
  43. ///
  44. /// </summary>
  45. /// <returns> {@link FormatInformation} encapsulating the QR Code's format info
  46. /// </returns>
  47. /// <throws> ReaderException if both format information locations cannot be parsed as </throws>
  48. /// <summary> the valid encoding of format information
  49. /// </summary>
  50. internal FormatInformation readFormatInformation()
  51. {
  52. if (parsedFormatInfo != null)
  53. {
  54. return parsedFormatInfo;
  55. }
  56. // Read top-left format info bits
  57. int formatInfoBits1 = 0;
  58. for (int i = 0; i < 6; i++)
  59. {
  60. formatInfoBits1 = copyBit(i, 8, formatInfoBits1);
  61. }
  62. // .. and skip a bit in the timing pattern ...
  63. formatInfoBits1 = copyBit(7, 8, formatInfoBits1);
  64. formatInfoBits1 = copyBit(8, 8, formatInfoBits1);
  65. formatInfoBits1 = copyBit(8, 7, formatInfoBits1);
  66. // .. and skip a bit in the timing pattern ...
  67. for (int j = 5; j >= 0; j--)
  68. {
  69. formatInfoBits1 = copyBit(8, j, formatInfoBits1);
  70. }
  71. // Read the top-right/bottom-left pattern too
  72. int dimension = bitMatrix.Height;
  73. int formatInfoBits2 = 0;
  74. int jMin = dimension - 7;
  75. for (int j = dimension - 1; j >= jMin; j--)
  76. {
  77. formatInfoBits2 = copyBit(8, j, formatInfoBits2);
  78. }
  79. for (int i = dimension - 8; i < dimension; i++)
  80. {
  81. formatInfoBits2 = copyBit(i, 8, formatInfoBits2);
  82. }
  83. parsedFormatInfo = FormatInformation.decodeFormatInformation(formatInfoBits1, formatInfoBits2);
  84. if (parsedFormatInfo != null)
  85. {
  86. return parsedFormatInfo;
  87. }
  88. return null;
  89. }
  90. /// <summary> <p>Reads version information from one of its two locations within the QR Code.</p>
  91. ///
  92. /// </summary>
  93. /// <returns> {@link Version} encapsulating the QR Code's version
  94. /// </returns>
  95. /// <throws> ReaderException if both version information locations cannot be parsed as </throws>
  96. /// <summary> the valid encoding of version information
  97. /// </summary>
  98. internal Version readVersion()
  99. {
  100. if (parsedVersion != null)
  101. {
  102. return parsedVersion;
  103. }
  104. int dimension = bitMatrix.Height;
  105. int provisionalVersion = (dimension - 17) >> 2;
  106. if (provisionalVersion <= 6)
  107. {
  108. return Version.getVersionForNumber(provisionalVersion);
  109. }
  110. // Read top-right version info: 3 wide by 6 tall
  111. int versionBits = 0;
  112. int ijMin = dimension - 11;
  113. for (int j = 5; j >= 0; j--)
  114. {
  115. for (int i = dimension - 9; i >= ijMin; i--)
  116. {
  117. versionBits = copyBit(i, j, versionBits);
  118. }
  119. }
  120. parsedVersion = Version.decodeVersionInformation(versionBits);
  121. if (parsedVersion != null && parsedVersion.DimensionForVersion == dimension)
  122. {
  123. return parsedVersion;
  124. }
  125. // Hmm, failed. Try bottom left: 6 wide by 3 tall
  126. versionBits = 0;
  127. for (int i = 5; i >= 0; i--)
  128. {
  129. for (int j = dimension - 9; j >= ijMin; j--)
  130. {
  131. versionBits = copyBit(i, j, versionBits);
  132. }
  133. }
  134. parsedVersion = Version.decodeVersionInformation(versionBits);
  135. if (parsedVersion != null && parsedVersion.DimensionForVersion == dimension)
  136. {
  137. return parsedVersion;
  138. }
  139. return null;
  140. }
  141. private int copyBit(int i, int j, int versionBits)
  142. {
  143. bool bit = mirrored ? bitMatrix[j, i] : bitMatrix[i, j];
  144. return bit ? (versionBits << 1) | 0x1 : versionBits << 1;
  145. }
  146. /// <summary> <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the
  147. /// correct order in order to reconstruct the codewords bytes contained within the
  148. /// QR Code.</p>
  149. ///
  150. /// </summary>
  151. /// <returns> bytes encoded within the QR Code
  152. /// </returns>
  153. /// <throws> ReaderException if the exact number of bytes expected is not read </throws>
  154. internal byte[] readCodewords()
  155. {
  156. FormatInformation formatInfo = readFormatInformation();
  157. if (formatInfo == null)
  158. return null;
  159. Version version = readVersion();
  160. if (version == null)
  161. return null;
  162. // Get the data mask for the format used in this QR Code. This will exclude
  163. // some bits from reading as we wind through the bit matrix.
  164. DataMask dataMask = DataMask.forReference(formatInfo.DataMask);
  165. int dimension = bitMatrix.Height;
  166. dataMask.unmaskBitMatrix(bitMatrix, dimension);
  167. BitMatrix functionPattern = version.buildFunctionPattern();
  168. bool readingUp = true;
  169. byte[] result = new byte[version.TotalCodewords];
  170. int resultOffset = 0;
  171. int currentByte = 0;
  172. int bitsRead = 0;
  173. // Read columns in pairs, from right to left
  174. for (int j = dimension - 1; j > 0; j -= 2)
  175. {
  176. if (j == 6)
  177. {
  178. // Skip whole column with vertical alignment pattern;
  179. // saves time and makes the other code proceed more cleanly
  180. j--;
  181. }
  182. // Read alternatingly from bottom to top then top to bottom
  183. for (int count = 0; count < dimension; count++)
  184. {
  185. int i = readingUp ? dimension - 1 - count : count;
  186. for (int col = 0; col < 2; col++)
  187. {
  188. // Ignore bits covered by the function pattern
  189. if (!functionPattern[j - col, i])
  190. {
  191. // Read a bit
  192. bitsRead++;
  193. currentByte <<= 1;
  194. if (bitMatrix[j - col, i])
  195. {
  196. currentByte |= 1;
  197. }
  198. // If we've made a whole byte, save it off
  199. if (bitsRead == 8)
  200. {
  201. result[resultOffset++] = (byte)currentByte;
  202. bitsRead = 0;
  203. currentByte = 0;
  204. }
  205. }
  206. }
  207. }
  208. readingUp ^= true; // readingUp = !readingUp; // switch directions
  209. }
  210. if (resultOffset != version.TotalCodewords)
  211. {
  212. return null;
  213. }
  214. return result;
  215. }
  216. /**
  217. * Revert the mask removal done while reading the code words. The bit matrix should revert to its original state.
  218. */
  219. internal void remask()
  220. {
  221. if (parsedFormatInfo == null)
  222. {
  223. return; // We have no format information, and have no data mask
  224. }
  225. DataMask dataMask = DataMask.forReference(parsedFormatInfo.DataMask);
  226. int dimension = bitMatrix.Height;
  227. dataMask.unmaskBitMatrix(bitMatrix, dimension);
  228. }
  229. /**
  230. * Prepare the parser for a mirrored operation.
  231. * This flag has effect only on the {@link #readFormatInformation()} and the
  232. * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the
  233. * {@link #mirror()} method should be called.
  234. *
  235. * @param mirror Whether to read version and format information mirrored.
  236. */
  237. internal void setMirror(bool mirror)
  238. {
  239. parsedVersion = null;
  240. parsedFormatInfo = null;
  241. mirrored = mirror;
  242. }
  243. /** Mirror the bit matrix in order to attempt a second reading. */
  244. internal void mirror()
  245. {
  246. for (int x = 0; x < bitMatrix.Width; x++)
  247. {
  248. for (int y = x + 1; y < bitMatrix.Height; y++)
  249. {
  250. if (bitMatrix[x, y] != bitMatrix[y, x])
  251. {
  252. bitMatrix.flip(y, x);
  253. bitMatrix.flip(x, y);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. }