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.

Version.cs 13 kB

10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 System;
  17. using ZXing.Common;
  18. namespace ZXing.QrCode.Internal
  19. {
  20. /// <summary>
  21. /// See ISO 18004:2006 Annex D
  22. /// </summary>
  23. /// <author>Sean Owen</author>
  24. public sealed class Version
  25. {
  26. private static readonly Version[] VERSIONS = buildVersions();
  27. private readonly int versionNumber;
  28. private readonly int[] alignmentPatternCenters;
  29. private readonly ECBlocks[] ecBlocks;
  30. private readonly int totalCodewords;
  31. private Version(int versionNumber, int[] alignmentPatternCenters, params ECBlocks[] ecBlocks)
  32. {
  33. this.versionNumber = versionNumber;
  34. this.alignmentPatternCenters = alignmentPatternCenters;
  35. this.ecBlocks = ecBlocks;
  36. int total = 0;
  37. int ecCodewords = ecBlocks[0].ECCodewordsPerBlock;
  38. ECB[] ecbArray = ecBlocks[0].getECBlocks();
  39. foreach (var ecBlock in ecbArray)
  40. {
  41. total += ecBlock.Count * (ecBlock.DataCodewords + ecCodewords);
  42. }
  43. this.totalCodewords = total;
  44. }
  45. /// <summary>
  46. /// Gets the version number.
  47. /// </summary>
  48. public int VersionNumber
  49. {
  50. get
  51. {
  52. return versionNumber;
  53. }
  54. }
  55. /// <summary>
  56. /// Gets the total codewords.
  57. /// </summary>
  58. public int TotalCodewords
  59. {
  60. get
  61. {
  62. return totalCodewords;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets the dimension for version.
  67. /// </summary>
  68. public int DimensionForVersion
  69. {
  70. get
  71. {
  72. return 17 + 4 * versionNumber;
  73. }
  74. }
  75. /// <summary>
  76. /// Gets the EC blocks for level.
  77. /// </summary>
  78. /// <param name="ecLevel">The ec level.</param>
  79. /// <returns></returns>
  80. public ECBlocks getECBlocksForLevel(ErrorCorrectionLevel ecLevel)
  81. {
  82. return ecBlocks[ecLevel.ordinal()];
  83. }
  84. /// <summary>
  85. /// Gets the version for number.
  86. /// </summary>
  87. /// <param name="versionNumber">The version number.</param>
  88. /// <returns></returns>
  89. public static Version getVersionForNumber(int versionNumber)
  90. {
  91. if (versionNumber < 1 || versionNumber > 40)
  92. {
  93. throw new ArgumentException();
  94. }
  95. return VERSIONS[versionNumber - 1];
  96. }
  97. /// <summary> <p>Encapsulates a set of error-correction blocks in one symbol version. Most versions will
  98. /// use blocks of differing sizes within one version, so, this encapsulates the parameters for
  99. /// each set of blocks. It also holds the number of error-correction codewords per block since it
  100. /// will be the same across all blocks within one version.</p>
  101. /// </summary>
  102. public sealed class ECBlocks
  103. {
  104. private readonly int ecCodewordsPerBlock;
  105. private readonly ECB[] ecBlocks;
  106. internal ECBlocks(int ecCodewordsPerBlock, params ECB[] ecBlocks)
  107. {
  108. this.ecCodewordsPerBlock = ecCodewordsPerBlock;
  109. this.ecBlocks = ecBlocks;
  110. }
  111. /// <summary>
  112. /// Gets the EC codewords per block.
  113. /// </summary>
  114. public int ECCodewordsPerBlock
  115. {
  116. get
  117. {
  118. return ecCodewordsPerBlock;
  119. }
  120. }
  121. /// <summary>
  122. /// Gets the num blocks.
  123. /// </summary>
  124. public int NumBlocks
  125. {
  126. get
  127. {
  128. int total = 0;
  129. foreach (var ecBlock in ecBlocks)
  130. {
  131. total += ecBlock.Count;
  132. }
  133. return total;
  134. }
  135. }
  136. /// <summary>
  137. /// Gets the total EC codewords.
  138. /// </summary>
  139. public int TotalECCodewords
  140. {
  141. get
  142. {
  143. return ecCodewordsPerBlock * NumBlocks;
  144. }
  145. }
  146. /// <summary>
  147. /// Gets the EC blocks.
  148. /// </summary>
  149. /// <returns></returns>
  150. public ECB[] getECBlocks()
  151. {
  152. return ecBlocks;
  153. }
  154. }
  155. /// <summary> <p>Encapsualtes the parameters for one error-correction block in one symbol version.
  156. /// This includes the number of data codewords, and the number of times a block with these
  157. /// parameters is used consecutively in the QR code version's format.</p>
  158. /// </summary>
  159. public sealed class ECB
  160. {
  161. private readonly int count;
  162. private readonly int dataCodewords;
  163. internal ECB(int count, int dataCodewords)
  164. {
  165. this.count = count;
  166. this.dataCodewords = dataCodewords;
  167. }
  168. /// <summary>
  169. /// Gets the count.
  170. /// </summary>
  171. public int Count
  172. {
  173. get
  174. {
  175. return count;
  176. }
  177. }
  178. /// <summary>
  179. /// Gets the data codewords.
  180. /// </summary>
  181. public int DataCodewords
  182. {
  183. get
  184. {
  185. return dataCodewords;
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// Returns a <see cref="System.String"/> that represents this instance.
  191. /// </summary>
  192. /// <returns>
  193. /// A <see cref="System.String"/> that represents this instance.
  194. /// </returns>
  195. public override String ToString()
  196. {
  197. return Convert.ToString(versionNumber);
  198. }
  199. /// <summary> See ISO 18004:2006 6.5.1 Table 9</summary>
  200. private static Version[] buildVersions()
  201. {
  202. return new Version[]
  203. {
  204. new Version(1, new int[] {},
  205. new ECBlocks(7, new ECB(1, 19)),
  206. new ECBlocks(10, new ECB(1, 16)),
  207. new ECBlocks(13, new ECB(1, 13)),
  208. new ECBlocks(17, new ECB(1, 9))),
  209. new Version(2, new int[] {6, 18},
  210. new ECBlocks(10, new ECB(1, 34)),
  211. new ECBlocks(16, new ECB(1, 28)),
  212. new ECBlocks(22, new ECB(1, 22)),
  213. new ECBlocks(28, new ECB(1, 16))),
  214. new Version(3, new int[] {6, 22},
  215. new ECBlocks(15, new ECB(1, 55)),
  216. new ECBlocks(26, new ECB(1, 44)),
  217. new ECBlocks(18, new ECB(2, 17)),
  218. new ECBlocks(22, new ECB(2, 13))),
  219. new Version(4, new int[] {6, 26},
  220. new ECBlocks(20, new ECB(1, 80)),
  221. new ECBlocks(18, new ECB(2, 32)),
  222. new ECBlocks(26, new ECB(2, 24)),
  223. new ECBlocks(16, new ECB(4, 9))),
  224. new Version(5, new int[] {6, 30},
  225. new ECBlocks(26, new ECB(1, 108)),
  226. new ECBlocks(24, new ECB(2, 43)),
  227. new ECBlocks(18, new ECB(2, 15),
  228. new ECB(2, 16)),
  229. new ECBlocks(22, new ECB(2, 11),
  230. new ECB(2, 12))),
  231. new Version(6, new int[] {6, 34},
  232. new ECBlocks(18, new ECB(2, 68)),
  233. new ECBlocks(16, new ECB(4, 27)),
  234. new ECBlocks(24, new ECB(4, 19)),
  235. new ECBlocks(28, new ECB(4, 15))),
  236. new Version(7, new int[] {6, 22, 38},
  237. new ECBlocks(20, new ECB(2, 78)),
  238. new ECBlocks(18, new ECB(4, 31)),
  239. new ECBlocks(18, new ECB(2, 14),
  240. new ECB(4, 15)),
  241. new ECBlocks(26, new ECB(4, 13),
  242. new ECB(1, 14))),
  243. new Version(8, new int[] {6, 24, 42},
  244. new ECBlocks(24, new ECB(2, 97)),
  245. new ECBlocks(22, new ECB(2, 38),
  246. new ECB(2, 39)),
  247. new ECBlocks(22, new ECB(4, 18),
  248. new ECB(2, 19)),
  249. new ECBlocks(26, new ECB(4, 14),
  250. new ECB(2, 15))),
  251. new Version(9, new int[] {6, 26, 46},
  252. new ECBlocks(30, new ECB(2, 116)),
  253. new ECBlocks(22, new ECB(3, 36),
  254. new ECB(2, 37)),
  255. new ECBlocks(20, new ECB(4, 16),
  256. new ECB(4, 17)),
  257. new ECBlocks(24, new ECB(4, 12),
  258. new ECB(4, 13))),
  259. new Version(10, new int[] {6, 28, 50},
  260. new ECBlocks(18, new ECB(2, 68),
  261. new ECB(2, 69)),
  262. new ECBlocks(26, new ECB(4, 43),
  263. new ECB(1, 44)),
  264. new ECBlocks(24, new ECB(6, 19),
  265. new ECB(2, 20)),
  266. new ECBlocks(28, new ECB(6, 15),
  267. new ECB(2, 16))),
  268. new Version(11, new int[] {6, 30, 54},
  269. new ECBlocks(20, new ECB(4, 81)),
  270. new ECBlocks(30, new ECB(1, 50),
  271. new ECB(4, 51)),
  272. new ECBlocks(28, new ECB(4, 22),
  273. new ECB(4, 23)),
  274. new ECBlocks(24, new ECB(3, 12),
  275. new ECB(8, 13))),
  276. new Version(12, new int[] {6, 32, 58},
  277. new ECBlocks(24, new ECB(2, 92),
  278. new ECB(2, 93)),
  279. new ECBlocks(22, new ECB(6, 36),
  280. new ECB(2, 37)),
  281. new ECBlocks(26, new ECB(4, 20),
  282. new ECB(6, 21)),
  283. new ECBlocks(28, new ECB(7, 14),
  284. new ECB(4, 15))),
  285. new Version(13, new int[] {6, 34, 62},
  286. new ECBlocks(26, new ECB(4, 107)),
  287. new ECBlocks(22, new ECB(8, 37),
  288. new ECB(1, 38)),
  289. new ECBlocks(24, new ECB(8, 20),
  290. new ECB(4, 21)),
  291. new ECBlocks(22, new ECB(12, 11),
  292. new ECB(4, 12))),
  293. new Version(14, new int[] {6, 26, 46, 66},
  294. new ECBlocks(30, new ECB(3, 115),
  295. new ECB(1, 116)),
  296. new ECBlocks(24, new ECB(4, 40),
  297. new ECB(5, 41)),
  298. new ECBlocks(20, new ECB(11, 16),
  299. new ECB(5, 17)),
  300. new ECBlocks(24, new ECB(11, 12),
  301. new ECB(5, 13))),
  302. new Version(15, new int[] {6, 26, 48, 70},
  303. new ECBlocks(22, new ECB(5, 87),
  304. new ECB(1, 88)),
  305. new ECBlocks(24, new ECB(5, 41),
  306. new ECB(5, 42)),
  307. new ECBlocks(30, new ECB(5, 24),
  308. new ECB(7, 25)),
  309. new ECBlocks(24, new ECB(11, 12),
  310. new ECB(7, 13)))
  311. };
  312. }
  313. }
  314. }