DataBlock.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * Copyright 2007 ZXing authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Zxing\Qrcode\Decoder;
  18. /**
  19. * <p>Encapsulates a block of data within a QR Code. QR Codes may split their data into
  20. * multiple blocks, each of which is a unit of data and error-correction codewords. Each
  21. * is represented by an instance of this class.</p>
  22. *
  23. * @author Sean Owen
  24. */
  25. final class DataBlock
  26. {
  27. private $numDataCodewords;
  28. private $codewords; //byte[]
  29. private function __construct($numDataCodewords, $codewords)
  30. {
  31. $this->numDataCodewords = $numDataCodewords;
  32. $this->codewords = $codewords;
  33. }
  34. /**
  35. * <p>When QR Codes use multiple data blocks, they are actually interleaved.
  36. * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
  37. * method will separate the data into original blocks.</p>
  38. *
  39. * @param rawCodewords bytes as read directly from the QR Code
  40. * @param version version of the QR Code
  41. * @param ecLevel error-correction level of the QR Code
  42. *
  43. * @return array DataBlocks containing original bytes, "de-interleaved" from representation in the
  44. * QR Code
  45. */
  46. public static function getDataBlocks($rawCodewords,
  47. $version,
  48. $ecLevel)
  49. {
  50. if (count($rawCodewords) != $version->getTotalCodewords()) {
  51. throw new \InvalidArgumentException();
  52. }
  53. // Figure out the number and size of data blocks used by this version and
  54. // error correction level
  55. $ecBlocks = $version->getECBlocksForLevel($ecLevel);
  56. // First count the total number of data blocks
  57. $totalBlocks = 0;
  58. $ecBlockArray = $ecBlocks->getECBlocks();
  59. foreach ($ecBlockArray as $ecBlock) {
  60. $totalBlocks += $ecBlock->getCount();
  61. }
  62. // Now establish DataBlocks of the appropriate size and number of data codewords
  63. $result = [];//new DataBlock[$totalBlocks];
  64. $numResultBlocks = 0;
  65. foreach ($ecBlockArray as $ecBlock) {
  66. $ecBlockCount = $ecBlock->getCount();
  67. for ($i = 0; $i < $ecBlockCount; $i++) {
  68. $numDataCodewords = $ecBlock->getDataCodewords();
  69. $numBlockCodewords = $ecBlocks->getECCodewordsPerBlock() + $numDataCodewords;
  70. $result[$numResultBlocks++] = new DataBlock($numDataCodewords, fill_array(0, $numBlockCodewords, 0));
  71. }
  72. }
  73. // All blocks have the same amount of data, except that the last n
  74. // (where n may be 0) have 1 more byte. Figure out where these start.
  75. $shorterBlocksTotalCodewords = count($result[0]->codewords);
  76. $longerBlocksStartAt = count($result) - 1;
  77. while ($longerBlocksStartAt >= 0) {
  78. $numCodewords = count($result[$longerBlocksStartAt]->codewords);
  79. if ($numCodewords == $shorterBlocksTotalCodewords) {
  80. break;
  81. }
  82. $longerBlocksStartAt--;
  83. }
  84. $longerBlocksStartAt++;
  85. $shorterBlocksNumDataCodewords = $shorterBlocksTotalCodewords - $ecBlocks->getECCodewordsPerBlock();
  86. // The last elements of result may be 1 element longer;
  87. // first fill out as many elements as all of them have
  88. $rawCodewordsOffset = 0;
  89. for ($i = 0; $i < $shorterBlocksNumDataCodewords; $i++) {
  90. for ($j = 0; $j < $numResultBlocks; $j++) {
  91. $result[$j]->codewords[$i] = $rawCodewords[$rawCodewordsOffset++];
  92. }
  93. }
  94. // Fill out the last data block in the longer ones
  95. for ($j = $longerBlocksStartAt; $j < $numResultBlocks; $j++) {
  96. $result[$j]->codewords[$shorterBlocksNumDataCodewords] = $rawCodewords[$rawCodewordsOffset++];
  97. }
  98. // Now add in error correction blocks
  99. $max = count($result[0]->codewords);
  100. for ($i = $shorterBlocksNumDataCodewords; $i < $max; $i++) {
  101. for ($j = 0; $j < $numResultBlocks; $j++) {
  102. $iOffset = $j < $longerBlocksStartAt ? $i : $i + 1;
  103. $result[$j]->codewords[$iOffset] = $rawCodewords[$rawCodewordsOffset++];
  104. }
  105. }
  106. return $result;
  107. }
  108. public function getNumDataCodewords()
  109. {
  110. return $this->numDataCodewords;
  111. }
  112. public function getCodewords()
  113. {
  114. return $this->codewords;
  115. }
  116. }