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.

DecodedBitStreamParser.cs 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 System.Collections.Generic;
  18. using System.Text;
  19. using ZXing.Common;
  20. namespace ZXing.QrCode.Internal
  21. {
  22. /// <summary> <p>QR Codes can encode text as bits in one of several modes, and can use multiple modes
  23. /// in one QR Code. This class decodes the bits back into text.</p>
  24. ///
  25. /// <p>See ISO 18004:2006, 6.4.3 - 6.4.7</p>
  26. /// <author>Sean Owen</author>
  27. /// </summary>
  28. internal static class DecodedBitStreamParser
  29. {
  30. /// <summary>
  31. /// See ISO 18004:2006, 6.4.4 Table 5
  32. /// </summary>
  33. private static readonly char[] ALPHANUMERIC_CHARS = {
  34. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
  35. 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  36. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  37. ' ', '$', '%', '*', '+', '-', '.', '/', ':'
  38. };
  39. private const int GB2312_SUBSET = 1;
  40. internal static DecoderResult decode(byte[] bytes,
  41. Version version,
  42. ErrorCorrectionLevel ecLevel,
  43. IDictionary<DecodeHintType, object> hints)
  44. {
  45. var bits = new BitSource(bytes);
  46. var result = new StringBuilder(50);
  47. var byteSegments = new List<byte[]>(1);
  48. var symbolSequence = -1;
  49. var parityData = -1;
  50. try
  51. {
  52. //CharacterSetECI currentCharacterSetECI = null;
  53. bool fc1InEffect = false;
  54. Mode mode;
  55. do
  56. {
  57. // While still another segment to read...
  58. if (bits.available() < 4)
  59. {
  60. // OK, assume we're done. Really, a TERMINATOR mode should have been recorded here
  61. mode = Mode.TERMINATOR;
  62. }
  63. else
  64. {
  65. try
  66. {
  67. mode = Mode.forBits(bits.readBits(4)); // mode is encoded by 4 bits
  68. }
  69. catch (ArgumentException)
  70. {
  71. return null;
  72. }
  73. }
  74. if (mode != Mode.TERMINATOR)
  75. {
  76. if (mode == Mode.FNC1_FIRST_POSITION || mode == Mode.FNC1_SECOND_POSITION)
  77. {
  78. // We do little with FNC1 except alter the parsed result a bit according to the spec
  79. fc1InEffect = true;
  80. }
  81. else if (mode == Mode.STRUCTURED_APPEND)
  82. {
  83. if (bits.available() < 16)
  84. {
  85. return null;
  86. }
  87. // not really supported; but sequence number and parity is added later to the result metadata
  88. // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
  89. symbolSequence = bits.readBits(8);
  90. parityData = bits.readBits(8);
  91. }
  92. else
  93. {
  94. // First handle Hanzi mode which does not start with character count
  95. if (mode == Mode.HANZI)
  96. {
  97. //chinese mode contains a sub set indicator right after mode indicator
  98. //int subset = bits.readBits(4);
  99. //int countHanzi = bits.readBits(mode.getCharacterCountBits(version));
  100. }
  101. else
  102. {
  103. // "Normal" QR code modes:
  104. // How many characters will follow, encoded in this mode?
  105. int count = bits.readBits(mode.getCharacterCountBits(version));
  106. if (mode == Mode.NUMERIC)
  107. {
  108. if (!decodeNumericSegment(bits, result, count))
  109. return null;
  110. }
  111. else if (mode == Mode.ALPHANUMERIC)
  112. {
  113. if (!decodeAlphanumericSegment(bits, result, count, fc1InEffect))
  114. return null;
  115. }
  116. else
  117. {
  118. return null;
  119. }
  120. }
  121. }
  122. }
  123. } while (mode != Mode.TERMINATOR);
  124. }
  125. catch (ArgumentException)
  126. {
  127. // from readBits() calls
  128. return null;
  129. }
  130. #if WindowsCE
  131. var resultString = result.ToString().Replace("\n", "\r\n");
  132. #else
  133. var resultString = result.ToString().Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
  134. #endif
  135. return new DecoderResult(bytes,
  136. resultString,
  137. byteSegments.Count == 0 ? null : byteSegments,
  138. ecLevel == null ? null : ecLevel.ToString(),
  139. symbolSequence, parityData);
  140. }
  141. private static char toAlphaNumericChar(int value)
  142. {
  143. if (value >= ALPHANUMERIC_CHARS.Length)
  144. {
  145. //throw FormatException.Instance;
  146. }
  147. return ALPHANUMERIC_CHARS[value];
  148. }
  149. private static bool decodeAlphanumericSegment(BitSource bits,
  150. StringBuilder result,
  151. int count,
  152. bool fc1InEffect)
  153. {
  154. // Read two characters at a time
  155. int start = result.Length;
  156. while (count > 1)
  157. {
  158. if (bits.available() < 11)
  159. {
  160. return false;
  161. }
  162. int nextTwoCharsBits = bits.readBits(11);
  163. result.Append(toAlphaNumericChar(nextTwoCharsBits / 45));
  164. result.Append(toAlphaNumericChar(nextTwoCharsBits % 45));
  165. count -= 2;
  166. }
  167. if (count == 1)
  168. {
  169. // special case: one character left
  170. if (bits.available() < 6)
  171. {
  172. return false;
  173. }
  174. result.Append(toAlphaNumericChar(bits.readBits(6)));
  175. }
  176. // See section 6.4.8.1, 6.4.8.2
  177. if (fc1InEffect)
  178. {
  179. // We need to massage the result a bit if in an FNC1 mode:
  180. for (int i = start; i < result.Length; i++)
  181. {
  182. if (result[i] == '%')
  183. {
  184. if (i < result.Length - 1 && result[i + 1] == '%')
  185. {
  186. // %% is rendered as %
  187. result.Remove(i + 1, 1);
  188. }
  189. else
  190. {
  191. // In alpha mode, % should be converted to FNC1 separator 0x1D
  192. result.Remove(i, 1);
  193. result.Insert(i, new[] { (char)0x1D });
  194. }
  195. }
  196. }
  197. }
  198. return true;
  199. }
  200. private static bool decodeNumericSegment(BitSource bits,
  201. StringBuilder result,
  202. int count)
  203. {
  204. // Read three digits at a time
  205. while (count >= 3)
  206. {
  207. // Each 10 bits encodes three digits
  208. if (bits.available() < 10)
  209. {
  210. return false;
  211. }
  212. int threeDigitsBits = bits.readBits(10);
  213. if (threeDigitsBits >= 1000)
  214. {
  215. return false;
  216. }
  217. result.Append(toAlphaNumericChar(threeDigitsBits / 100));
  218. result.Append(toAlphaNumericChar((threeDigitsBits / 10) % 10));
  219. result.Append(toAlphaNumericChar(threeDigitsBits % 10));
  220. count -= 3;
  221. }
  222. if (count == 2)
  223. {
  224. // Two digits left over to read, encoded in 7 bits
  225. if (bits.available() < 7)
  226. {
  227. return false;
  228. }
  229. int twoDigitsBits = bits.readBits(7);
  230. if (twoDigitsBits >= 100)
  231. {
  232. return false;
  233. }
  234. result.Append(toAlphaNumericChar(twoDigitsBits / 10));
  235. result.Append(toAlphaNumericChar(twoDigitsBits % 10));
  236. }
  237. else if (count == 1)
  238. {
  239. // One digit left over to read
  240. if (bits.available() < 4)
  241. {
  242. return false;
  243. }
  244. int digitBits = bits.readBits(4);
  245. if (digitBits >= 10)
  246. {
  247. return false;
  248. }
  249. result.Append(toAlphaNumericChar(digitBits));
  250. }
  251. return true;
  252. }
  253. private static int parseECIValue(BitSource bits)
  254. {
  255. int firstByte = bits.readBits(8);
  256. if ((firstByte & 0x80) == 0)
  257. {
  258. // just one byte
  259. return firstByte & 0x7F;
  260. }
  261. if ((firstByte & 0xC0) == 0x80)
  262. {
  263. // two bytes
  264. int secondByte = bits.readBits(8);
  265. return ((firstByte & 0x3F) << 8) | secondByte;
  266. }
  267. if ((firstByte & 0xE0) == 0xC0)
  268. {
  269. // three bytes
  270. int secondThirdBytes = bits.readBits(16);
  271. return ((firstByte & 0x1F) << 16) | secondThirdBytes;
  272. }
  273. throw new ArgumentException("Bad ECI bits starting with byte " + firstByte);
  274. }
  275. }
  276. }