BitMatrixParser.php 9.3 KB

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