DataBlock.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //byte[]
  28. private function __construct(private $numDataCodewords, private $codewords)
  29. {
  30. }
  31. /**
  32. * <p>When QR Codes use multiple data blocks, they are actually interleaved.
  33. * That is, the first byte of data block 1 to n is written, then the second bytes, and so on. This
  34. * method will separate the data into original blocks.</p>
  35. *
  36. * @param bytes $rawCodewords as read directly from the QR Code
  37. * @param version $version of the QR Code
  38. * @param error $ecLevel-correction level of the QR Code
  39. *
  40. * @return array DataBlocks containing original bytes, "de-interleaved" from representation in the
  41. * QR Code
  42. */
  43. public static function getDataBlocks(
  44. $rawCodewords,
  45. $version,
  46. $ecLevel
  47. )
  48. {
  49. if ((is_countable($rawCodewords) ? count($rawCodewords) : 0) != $version->getTotalCodewords()) {
  50. throw new \InvalidArgumentException();
  51. }
  52. // Figure out the number and size of data blocks used by this version and
  53. // error correction level
  54. $ecBlocks = $version->getECBlocksForLevel($ecLevel);
  55. // First count the total number of data blocks
  56. $totalBlocks = 0;
  57. $ecBlockArray = $ecBlocks->getECBlocks();
  58. foreach ($ecBlockArray as $ecBlock) {
  59. $totalBlocks += $ecBlock->getCount();
  60. }
  61. // Now establish DataBlocks of the appropriate size and number of data codewords
  62. $result = [];//new DataBlock[$totalBlocks];
  63. $numResultBlocks = 0;
  64. foreach ($ecBlockArray as $ecBlock) {
  65. $ecBlockCount = $ecBlock->getCount();
  66. for ($i = 0; $i < $ecBlockCount; $i++) {
  67. $numDataCodewords = $ecBlock->getDataCodewords();
  68. $numBlockCodewords = $ecBlocks->getECCodewordsPerBlock() + $numDataCodewords;
  69. $result[$numResultBlocks++] = new DataBlock($numDataCodewords, fill_array(0, $numBlockCodewords, 0));
  70. }
  71. }
  72. // All blocks have the same amount of data, except that the last n
  73. // (where n may be 0) have 1 more byte. Figure out where these start.
  74. $shorterBlocksTotalCodewords = is_countable($result[0]->codewords) ? count($result[0]->codewords) : 0;
  75. $longerBlocksStartAt = count($result) - 1;
  76. while ($longerBlocksStartAt >= 0) {
  77. $numCodewords = is_countable($result[$longerBlocksStartAt]->codewords) ? count($result[$longerBlocksStartAt]->codewords) : 0;
  78. if ($numCodewords == $shorterBlocksTotalCodewords) {
  79. break;
  80. }
  81. $longerBlocksStartAt--;
  82. }
  83. $longerBlocksStartAt++;
  84. $shorterBlocksNumDataCodewords = $shorterBlocksTotalCodewords - $ecBlocks->getECCodewordsPerBlock();
  85. // The last elements of result may be 1 element longer;
  86. // first fill out as many elements as all of them have
  87. $rawCodewordsOffset = 0;
  88. for ($i = 0; $i < $shorterBlocksNumDataCodewords; $i++) {
  89. for ($j = 0; $j < $numResultBlocks; $j++) {
  90. $result[$j]->codewords[$i] = $rawCodewords[$rawCodewordsOffset++];
  91. }
  92. }
  93. // Fill out the last data block in the longer ones
  94. for ($j = $longerBlocksStartAt; $j < $numResultBlocks; $j++) {
  95. $result[$j]->codewords[$shorterBlocksNumDataCodewords] = $rawCodewords[$rawCodewordsOffset++];
  96. }
  97. // Now add in error correction blocks
  98. $max = is_countable($result[0]->codewords) ? count($result[0]->codewords) : 0;
  99. for ($i = $shorterBlocksNumDataCodewords; $i < $max; $i++) {
  100. for ($j = 0; $j < $numResultBlocks; $j++) {
  101. $iOffset = $j < $longerBlocksStartAt ? $i : $i + 1;
  102. $result[$j]->codewords[$iOffset] = $rawCodewords[$rawCodewordsOffset++];
  103. }
  104. }
  105. return $result;
  106. }
  107. public function getNumDataCodewords()
  108. {
  109. return $this->numDataCodewords;
  110. }
  111. public function getCodewords()
  112. {
  113. return $this->codewords;
  114. }
  115. }