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.

FormatInformation.cs 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. namespace ZXing.QrCode.Internal
  18. {
  19. /// <summary> <p>Encapsulates a QR Code's format information, including the data mask used and
  20. /// error correction level.</p>
  21. ///
  22. /// </summary>
  23. /// <author> Sean Owen
  24. /// </author>
  25. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  26. /// </author>
  27. /// <seealso cref="DataMask">
  28. /// </seealso>
  29. /// <seealso cref="ErrorCorrectionLevel">
  30. /// </seealso>
  31. sealed class FormatInformation
  32. {
  33. private const int FORMAT_INFO_MASK_QR = 0x5412;
  34. /// <summary> See ISO 18004:2006, Annex C, Table C.1</summary>
  35. private static readonly int[][] FORMAT_INFO_DECODE_LOOKUP = new int[][]
  36. {
  37. new [] { 0x5412, 0x00 },
  38. new [] { 0x5125, 0x01 },
  39. new [] { 0x5E7C, 0x02 },
  40. new [] { 0x5B4B, 0x03 },
  41. new [] { 0x45F9, 0x04 },
  42. new [] { 0x40CE, 0x05 },
  43. new [] { 0x4F97, 0x06 },
  44. new [] { 0x4AA0, 0x07 },
  45. new [] { 0x77C4, 0x08 },
  46. new [] { 0x72F3, 0x09 },
  47. new [] { 0x7DAA, 0x0A },
  48. new [] { 0x789D, 0x0B },
  49. new [] { 0x662F, 0x0C },
  50. new [] { 0x6318, 0x0D },
  51. new [] { 0x6C41, 0x0E },
  52. new [] { 0x6976, 0x0F },
  53. new [] { 0x1689, 0x10 },
  54. new [] { 0x13BE, 0x11 },
  55. new [] { 0x1CE7, 0x12 },
  56. new [] { 0x19D0, 0x13 },
  57. new [] { 0x0762, 0x14 },
  58. new [] { 0x0255, 0x15 },
  59. new [] { 0x0D0C, 0x16 },
  60. new [] { 0x083B, 0x17 },
  61. new [] { 0x355F, 0x18 },
  62. new [] { 0x3068, 0x19 },
  63. new [] { 0x3F31, 0x1A },
  64. new [] { 0x3A06, 0x1B },
  65. new [] { 0x24B4, 0x1C },
  66. new [] { 0x2183, 0x1D },
  67. new [] { 0x2EDA, 0x1E },
  68. new [] { 0x2BED, 0x1F }
  69. };
  70. /// <summary> Offset i holds the number of 1 bits in the binary representation of i</summary>
  71. private static readonly int[] BITS_SET_IN_HALF_BYTE = new []
  72. { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
  73. private readonly ErrorCorrectionLevel errorCorrectionLevel;
  74. private readonly byte dataMask;
  75. private FormatInformation(int formatInfo)
  76. {
  77. // Bits 3,4
  78. errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
  79. // Bottom 3 bits
  80. dataMask = (byte)(formatInfo & 0x07);
  81. }
  82. internal static int numBitsDiffering(int a, int b)
  83. {
  84. a ^= b; // a now has a 1 bit exactly where its bit differs with b's
  85. // Count bits set quickly with a series of lookups:
  86. return BITS_SET_IN_HALF_BYTE[a & 0x0F] +
  87. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 4)) & 0x0F)] +
  88. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 8)) & 0x0F)] +
  89. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 12)) & 0x0F)] +
  90. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 16)) & 0x0F)] +
  91. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 20)) & 0x0F)] +
  92. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 24)) & 0x0F)] +
  93. BITS_SET_IN_HALF_BYTE[(((int)((uint)a >> 28)) & 0x0F)];
  94. }
  95. /// <summary>
  96. /// Decodes the format information.
  97. /// </summary>
  98. /// <param name="maskedFormatInfo1">format info indicator, with mask still applied</param>
  99. /// <param name="maskedFormatInfo2">The masked format info2.</param>
  100. /// <returns>
  101. /// information about the format it specifies, or <code>null</code>
  102. /// if doesn't seem to match any known pattern
  103. /// </returns>
  104. internal static FormatInformation decodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2)
  105. {
  106. FormatInformation formatInfo = doDecodeFormatInformation(maskedFormatInfo1, maskedFormatInfo2);
  107. if (formatInfo != null)
  108. {
  109. return formatInfo;
  110. }
  111. // Should return null, but, some QR codes apparently
  112. // do not mask this info. Try again by actually masking the pattern
  113. // first
  114. return doDecodeFormatInformation(maskedFormatInfo1 ^ FORMAT_INFO_MASK_QR,
  115. maskedFormatInfo2 ^ FORMAT_INFO_MASK_QR);
  116. }
  117. private static FormatInformation doDecodeFormatInformation(int maskedFormatInfo1, int maskedFormatInfo2)
  118. {
  119. // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
  120. int bestDifference = Int32.MaxValue;
  121. int bestFormatInfo = 0;
  122. foreach (var decodeInfo in FORMAT_INFO_DECODE_LOOKUP)
  123. {
  124. int targetInfo = decodeInfo[0];
  125. if (targetInfo == maskedFormatInfo1 || targetInfo == maskedFormatInfo2)
  126. {
  127. // Found an exact match
  128. return new FormatInformation(decodeInfo[1]);
  129. }
  130. int bitsDifference = numBitsDiffering(maskedFormatInfo1, targetInfo);
  131. if (bitsDifference < bestDifference)
  132. {
  133. bestFormatInfo = decodeInfo[1];
  134. bestDifference = bitsDifference;
  135. }
  136. if (maskedFormatInfo1 != maskedFormatInfo2)
  137. {
  138. // also try the other option
  139. bitsDifference = numBitsDiffering(maskedFormatInfo2, targetInfo);
  140. if (bitsDifference < bestDifference)
  141. {
  142. bestFormatInfo = decodeInfo[1];
  143. bestDifference = bitsDifference;
  144. }
  145. }
  146. }
  147. // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
  148. // differing means we found a match
  149. if (bestDifference <= 3)
  150. {
  151. return new FormatInformation(bestFormatInfo);
  152. }
  153. return null;
  154. }
  155. internal ErrorCorrectionLevel ErrorCorrectionLevel
  156. {
  157. get
  158. {
  159. return errorCorrectionLevel;
  160. }
  161. }
  162. internal byte DataMask
  163. {
  164. get
  165. {
  166. return dataMask;
  167. }
  168. }
  169. public override int GetHashCode()
  170. {
  171. return (errorCorrectionLevel.ordinal() << 3) | dataMask;
  172. }
  173. public override bool Equals(Object o)
  174. {
  175. if (!(o is FormatInformation))
  176. {
  177. return false;
  178. }
  179. var other = (FormatInformation)o;
  180. return errorCorrectionLevel == other.errorCorrectionLevel && dataMask == other.dataMask;
  181. }
  182. }
  183. }