BitMatrixParser.php 9.1 KB

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