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.

DataBlock.cs 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. namespace ZXing.QrCode.Internal
  17. {
  18. /// <summary> <p>Encapsulates a block of data within a QR Code. QR Codes may split their data into
  19. /// multiple blocks, each of which is a unit of data and error-correction codewords. Each
  20. /// is represented by an instance of this class.</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. internal sealed class DataBlock
  28. {
  29. private readonly int numDataCodewords;
  30. private readonly byte[] codewords;
  31. private DataBlock(int numDataCodewords, byte[] codewords)
  32. {
  33. this.numDataCodewords = numDataCodewords;
  34. this.codewords = codewords;
  35. }
  36. /// <summary> <p>When QR Codes use multiple data blocks, they are actually interleaved.
  37. /// That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
  38. /// method will separate the data into original blocks.</p>
  39. ///
  40. /// </summary>
  41. /// <param name="rawCodewords">bytes as read directly from the QR Code
  42. /// </param>
  43. /// <param name="version">version of the QR Code
  44. /// </param>
  45. /// <param name="ecLevel">error-correction level of the QR Code
  46. /// </param>
  47. /// <returns> {@link DataBlock}s containing original bytes, "de-interleaved" from representation in the
  48. /// QR Code
  49. /// </returns>
  50. internal static DataBlock[] getDataBlocks(byte[] rawCodewords, Version version, ErrorCorrectionLevel ecLevel)
  51. {
  52. if (rawCodewords.Length != version.TotalCodewords)
  53. {
  54. throw new System.ArgumentException();
  55. }
  56. // Figure out the number and size of data blocks used by this version and
  57. // error correction level
  58. Version.ECBlocks ecBlocks = version.getECBlocksForLevel(ecLevel);
  59. // First count the total number of data blocks
  60. int totalBlocks = 0;
  61. Version.ECB[] ecBlockArray = ecBlocks.getECBlocks();
  62. foreach (var ecBlock in ecBlockArray)
  63. {
  64. totalBlocks += ecBlock.Count;
  65. }
  66. // Now establish DataBlocks of the appropriate size and number of data codewords
  67. DataBlock[] result = new DataBlock[totalBlocks];
  68. int numResultBlocks = 0;
  69. foreach (var ecBlock in ecBlockArray)
  70. {
  71. for (int i = 0; i < ecBlock.Count; i++)
  72. {
  73. int numDataCodewords = ecBlock.DataCodewords;
  74. int numBlockCodewords = ecBlocks.ECCodewordsPerBlock + numDataCodewords;
  75. result[numResultBlocks++] = new DataBlock(numDataCodewords, new byte[numBlockCodewords]);
  76. }
  77. }
  78. // All blocks have the same amount of data, except that the last n
  79. // (where n may be 0) have 1 more byte. Figure out where these start.
  80. int shorterBlocksTotalCodewords = result[0].codewords.Length;
  81. int longerBlocksStartAt = result.Length - 1;
  82. while (longerBlocksStartAt >= 0)
  83. {
  84. int numCodewords = result[longerBlocksStartAt].codewords.Length;
  85. if (numCodewords == shorterBlocksTotalCodewords)
  86. {
  87. break;
  88. }
  89. longerBlocksStartAt--;
  90. }
  91. longerBlocksStartAt++;
  92. int shorterBlocksNumDataCodewords = shorterBlocksTotalCodewords - ecBlocks.ECCodewordsPerBlock;
  93. // The last elements of result may be 1 element longer;
  94. // first fill out as many elements as all of them have
  95. int rawCodewordsOffset = 0;
  96. for (int i = 0; i < shorterBlocksNumDataCodewords; i++)
  97. {
  98. for (int j = 0; j < numResultBlocks; j++)
  99. {
  100. result[j].codewords[i] = rawCodewords[rawCodewordsOffset++];
  101. }
  102. }
  103. // Fill out the last data block in the longer ones
  104. for (int j = longerBlocksStartAt; j < numResultBlocks; j++)
  105. {
  106. result[j].codewords[shorterBlocksNumDataCodewords] = rawCodewords[rawCodewordsOffset++];
  107. }
  108. // Now add in error correction blocks
  109. int max = result[0].codewords.Length;
  110. for (int i = shorterBlocksNumDataCodewords; i < max; i++)
  111. {
  112. for (int j = 0; j < numResultBlocks; j++)
  113. {
  114. int iOffset = j < longerBlocksStartAt ? i : i + 1;
  115. result[j].codewords[iOffset] = rawCodewords[rawCodewordsOffset++];
  116. }
  117. }
  118. return result;
  119. }
  120. internal int NumDataCodewords
  121. {
  122. get
  123. {
  124. return numDataCodewords;
  125. }
  126. }
  127. internal byte[] Codewords
  128. {
  129. get
  130. {
  131. return codewords;
  132. }
  133. }
  134. }
  135. }