DataBlock.php 4.8 KB

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