BitMatrixParser.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. use Zxing\Common\BitMatrix;
  19. use Zxing\FormatException;
  20. /**
  21. * @author Sean Owen
  22. */
  23. final class BitMatrixParser
  24. {
  25. private $bitMatrix;
  26. /**
  27. * @var mixed|null
  28. */
  29. private $parsedVersion;
  30. private $parsedFormatInfo;
  31. private $mirror;
  32. /**
  33. * @param $bitMatrix {@link BitMatrix} to parse
  34. *
  35. * @throws FormatException if dimension is not >= 21 and 1 mod 4
  36. */
  37. public function __construct($bitMatrix)
  38. {
  39. $dimension = $bitMatrix->getHeight();
  40. if ($dimension < 21 || ($dimension & 0x03) != 1) {
  41. throw FormatException::getFormatInstance();
  42. }
  43. $this->bitMatrix = $bitMatrix;
  44. }
  45. /**
  46. * <p>Reads the bits in the {@link BitMatrix} representing the finder pattern in the
  47. * correct order in order to reconstruct the codewords bytes contained within the
  48. * QR Code.</p>
  49. *
  50. * @return bytes encoded within the QR Code
  51. * @throws FormatException if the exact number of bytes expected is not read
  52. */
  53. public function readCodewords()
  54. {
  55. $formatInfo = $this->readFormatInformation();
  56. $version = $this->readVersion();
  57. // Get the data mask for the format used in this QR Code. This will exclude
  58. // some bits from reading as we wind through the bit matrix.
  59. $dataMask = DataMask::forReference($formatInfo->getDataMask());
  60. $dimension = $this->bitMatrix->getHeight();
  61. $dataMask->unmaskBitMatrix($this->bitMatrix, $dimension);
  62. $functionPattern = $version->buildFunctionPattern();
  63. $readingUp = true;
  64. if ($version->getTotalCodewords()) {
  65. $result = fill_array(0, $version->getTotalCodewords(), 0);
  66. } else {
  67. $result = [];
  68. }
  69. $resultOffset = 0;
  70. $currentByte = 0;
  71. $bitsRead = 0;
  72. // Read columns in pairs, from right to left
  73. for ($j = $dimension - 1; $j > 0; $j -= 2) {
  74. if ($j == 6) {
  75. // Skip whole column with vertical alignment pattern;
  76. // saves time and makes the other code proceed more cleanly
  77. $j--;
  78. }
  79. // Read alternatingly from bottom to top then top to bottom
  80. for ($count = 0; $count < $dimension; $count++) {
  81. $i = $readingUp ? $dimension - 1 - $count : $count;
  82. for ($col = 0; $col < 2; $col++) {
  83. // Ignore bits covered by the function pattern
  84. if (!$functionPattern->get($j - $col, $i)) {
  85. // Read a bit
  86. $bitsRead++;
  87. $currentByte <<= 1;
  88. if ($this->bitMatrix->get($j - $col, $i)) {
  89. $currentByte |= 1;
  90. }
  91. // If we've made a whole byte, save it off
  92. if ($bitsRead == 8) {
  93. $result[$resultOffset++] = $currentByte; //(byte)
  94. $bitsRead = 0;
  95. $currentByte = 0;
  96. }
  97. }
  98. }
  99. }
  100. $readingUp ^= true; // readingUp = !readingUp; // switch directions
  101. }
  102. if ($resultOffset != $version->getTotalCodewords()) {
  103. throw FormatException::getFormatInstance();
  104. }
  105. return $result;
  106. }
  107. /**
  108. * <p>Reads format information from one of its two locations within the QR Code.</p>
  109. *
  110. * @return {@link FormatInformation} encapsulating the QR Code's format info
  111. * @throws FormatException if both format information locations cannot be parsed as
  112. * the valid encoding of format information
  113. */
  114. public function readFormatInformation()
  115. {
  116. if ($this->parsedFormatInfo != null) {
  117. return $this->parsedFormatInfo;
  118. }
  119. // Read top-left format info bits
  120. $formatInfoBits1 = 0;
  121. for ($i = 0; $i < 6; $i++) {
  122. $formatInfoBits1 = $this->copyBit($i, 8, $formatInfoBits1);
  123. }
  124. // .. and skip a bit in the timing pattern ...
  125. $formatInfoBits1 = $this->copyBit(7, 8, $formatInfoBits1);
  126. $formatInfoBits1 = $this->copyBit(8, 8, $formatInfoBits1);
  127. $formatInfoBits1 = $this->copyBit(8, 7, $formatInfoBits1);
  128. // .. and skip a bit in the timing pattern ...
  129. for ($j = 5; $j >= 0; $j--) {
  130. $formatInfoBits1 = $this->copyBit(8, $j, $formatInfoBits1);
  131. }
  132. // Read the top-right/bottom-left pattern too
  133. $dimension = $this->bitMatrix->getHeight();
  134. $formatInfoBits2 = 0;
  135. $jMin = $dimension - 7;
  136. for ($j = $dimension - 1; $j >= $jMin; $j--) {
  137. $formatInfoBits2 = $this->copyBit(8, $j, $formatInfoBits2);
  138. }
  139. for ($i = $dimension - 8; $i < $dimension; $i++) {
  140. $formatInfoBits2 = $this->copyBit($i, 8, $formatInfoBits2);
  141. }
  142. $parsedFormatInfo = FormatInformation::decodeFormatInformation($formatInfoBits1, $formatInfoBits2);
  143. if ($parsedFormatInfo != null) {
  144. return $parsedFormatInfo;
  145. }
  146. throw FormatException::getFormatInstance();
  147. }
  148. private function copyBit($i, $j, $versionBits)
  149. {
  150. $bit = $this->mirror ? $this->bitMatrix->get($j, $i) : $this->bitMatrix->get($i, $j);
  151. return $bit ? ($versionBits << 1) | 0x1 : $versionBits << 1;
  152. }
  153. /**
  154. * <p>Reads version information from one of its two locations within the QR Code.</p>
  155. *
  156. * @return {@link Version} encapsulating the QR Code's version
  157. * @throws FormatException if both version information locations cannot be parsed as
  158. * the valid encoding of version information
  159. */
  160. public function readVersion()
  161. {
  162. if ($this->parsedVersion != null) {
  163. return $this->parsedVersion;
  164. }
  165. $dimension = $this->bitMatrix->getHeight();
  166. $provisionalVersion = ($dimension - 17) / 4;
  167. if ($provisionalVersion <= 6) {
  168. return Version::getVersionForNumber($provisionalVersion);
  169. }
  170. // Read top-right version info: 3 wide by 6 tall
  171. $versionBits = 0;
  172. $ijMin = $dimension - 11;
  173. for ($j = 5; $j >= 0; $j--) {
  174. for ($i = $dimension - 9; $i >= $ijMin; $i--) {
  175. $versionBits = $this->copyBit($i, $j, $versionBits);
  176. }
  177. }
  178. $theParsedVersion = Version::decodeVersionInformation($versionBits);
  179. if ($theParsedVersion != null && $theParsedVersion->getDimensionForVersion() == $dimension) {
  180. $this->parsedVersion = $theParsedVersion;
  181. return $theParsedVersion;
  182. }
  183. // Hmm, failed. Try bottom left: 6 wide by 3 tall
  184. $versionBits = 0;
  185. for ($i = 5; $i >= 0; $i--) {
  186. for ($j = $dimension - 9; $j >= $ijMin; $j--) {
  187. $versionBits = $this->copyBit($i, $j, $versionBits);
  188. }
  189. }
  190. $theParsedVersion = Version::decodeVersionInformation($versionBits);
  191. if ($theParsedVersion != null && $theParsedVersion->getDimensionForVersion() == $dimension) {
  192. $this->parsedVersion = $theParsedVersion;
  193. return $theParsedVersion;
  194. }
  195. throw FormatException::getFormatInstance();
  196. }
  197. /**
  198. * Revert the mask removal done while reading the code words. The bit matrix should revert to its original state.
  199. */
  200. public function remask(): void
  201. {
  202. if ($this->parsedFormatInfo == null) {
  203. return; // We have no format information, and have no data mask
  204. }
  205. $dataMask = DataMask::forReference($this->parsedFormatInfo->getDataMask());
  206. $dimension = $this->bitMatrix->getHeight();
  207. $dataMask->unmaskBitMatrix($this->bitMatrix, $dimension);
  208. }
  209. /**
  210. * Prepare the parser for a mirrored operation.
  211. * This flag has effect only on the {@link #readFormatInformation()} and the
  212. * {@link #readVersion()}. Before proceeding with {@link #readCodewords()} the
  213. * {@link #mirror()} method should be called.
  214. *
  215. * @param Whether $mirror to read version and format information mirrored.
  216. */
  217. public function setMirror($mirror): void
  218. {
  219. $parsedVersion = null;
  220. $parsedFormatInfo = null;
  221. $this->mirror = $mirror;
  222. }
  223. /** Mirror the bit matrix in order to attempt a second reading. */
  224. public function mirror(): void
  225. {
  226. for ($x = 0; $x < $this->bitMatrix->getWidth(); $x++) {
  227. for ($y = $x + 1; $y < $this->bitMatrix->getHeight(); $y++) {
  228. if ($this->bitMatrix->get($x, $y) != $this->bitMatrix->get($y, $x)) {
  229. $this->bitMatrix->flip($y, $x);
  230. $this->bitMatrix->flip($x, $y);
  231. }
  232. }
  233. }
  234. }
  235. }