QRCodeReader.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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;
  18. use Zxing\BinaryBitmap;
  19. use Zxing\ChecksumException;
  20. use Zxing\FormatException;
  21. use Zxing\NotFoundException;
  22. use Zxing\Reader;
  23. use Zxing\Result;
  24. use Zxing\Common\BitMatrix;
  25. use Zxing\Qrcode\Decoder\Decoder;
  26. use Zxing\Qrcode\Detector\Detector;
  27. /**
  28. * This implementation can detect and decode QR Codes in an image.
  29. *
  30. * @author Sean Owen
  31. */
  32. class QRCodeReader implements Reader
  33. {
  34. private static $NO_POINTS = [];
  35. private $decoder;
  36. public function __construct()
  37. {
  38. $this->decoder = new Decoder();
  39. }
  40. /**
  41. * @param BinaryBitmap $image
  42. * @param null $hints
  43. *
  44. * @return Result
  45. * @throws \Zxing\FormatException
  46. * @throws \Zxing\NotFoundException
  47. */
  48. public function decode(BinaryBitmap $image, $hints = null)
  49. {
  50. $decoderResult = null;
  51. if ($hints !== null && $hints['PURE_BARCODE']) {
  52. $bits = self::extractPureBits($image->getBlackMatrix());
  53. $decoderResult = $this->decoder->decode($bits, $hints);
  54. $points = self::$NO_POINTS;
  55. } else {
  56. $detector = new Detector($image->getBlackMatrix());
  57. $detectorResult = $detector->detect($hints);
  58. $decoderResult = $this->decoder->decode($detectorResult->getBits(), $hints);
  59. $points = $detectorResult->getPoints();
  60. }
  61. $result = new Result($decoderResult->getText(), $decoderResult->getRawBytes(), $points, 'QR_CODE');//BarcodeFormat.QR_CODE
  62. $byteSegments = $decoderResult->getByteSegments();
  63. if ($byteSegments !== null) {
  64. $result->putMetadata('BYTE_SEGMENTS', $byteSegments);//ResultMetadataType.BYTE_SEGMENTS
  65. }
  66. $ecLevel = $decoderResult->getECLevel();
  67. if ($ecLevel !== null) {
  68. $result->putMetadata('ERROR_CORRECTION_LEVEL', $ecLevel);//ResultMetadataType.ERROR_CORRECTION_LEVEL
  69. }
  70. if ($decoderResult->hasStructuredAppend()) {
  71. $result->putMetadata(
  72. 'STRUCTURED_APPEND_SEQUENCE',//ResultMetadataType.STRUCTURED_APPEND_SEQUENCE
  73. $decoderResult->getStructuredAppendSequenceNumber()
  74. );
  75. $result->putMetadata(
  76. 'STRUCTURED_APPEND_PARITY',//ResultMetadataType.STRUCTURED_APPEND_PARITY
  77. $decoderResult->getStructuredAppendParity()
  78. );
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Locates and decodes a QR code in an image.
  84. *
  85. * @return a String representing the content encoded by the QR code
  86. * @throws NotFoundException if a QR code cannot be found
  87. * @throws FormatException if a QR code cannot be decoded
  88. * @throws ChecksumException if error correction fails
  89. */
  90. /**
  91. * This method detects a code in a "pure" image -- that is, pure monochrome image
  92. * which contains only an unrotated, unskewed, image of a code, with some white border
  93. * around it. This is a specialized method that works exceptionally fast in this special
  94. * case.
  95. *
  96. * @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
  97. */
  98. private static function extractPureBits(BitMatrix $image)
  99. {
  100. $leftTopBlack = $image->getTopLeftOnBit();
  101. $rightBottomBlack = $image->getBottomRightOnBit();
  102. if ($leftTopBlack === null || $rightBottomBlack == null) {
  103. throw NotFoundException::getNotFoundInstance();
  104. }
  105. $moduleSize = self::moduleSize($leftTopBlack, $image);
  106. $top = $leftTopBlack[1];
  107. $bottom = $rightBottomBlack[1];
  108. $left = $leftTopBlack[0];
  109. $right = $rightBottomBlack[0];
  110. // Sanity check!
  111. if ($left >= $right || $top >= $bottom) {
  112. throw NotFoundException::getNotFoundInstance();
  113. }
  114. if ($bottom - $top != $right - $left) {
  115. // Special case, where bottom-right module wasn't black so we found something else in the last row
  116. // Assume it's a square, so use height as the width
  117. $right = $left + ($bottom - $top);
  118. }
  119. $matrixWidth = round(($right - $left + 1) / $moduleSize);
  120. $matrixHeight = round(($bottom - $top + 1) / $moduleSize);
  121. if ($matrixWidth <= 0 || $matrixHeight <= 0) {
  122. throw NotFoundException::getNotFoundInstance();
  123. }
  124. if ($matrixHeight != $matrixWidth) {
  125. // Only possibly decode square regions
  126. throw NotFoundException::getNotFoundInstance();
  127. }
  128. // Push in the "border" by half the module width so that we start
  129. // sampling in the middle of the module. Just in case the image is a
  130. // little off, this will help recover.
  131. $nudge = (int)($moduleSize / 2.0);// $nudge = (int) ($moduleSize / 2.0f);
  132. $top += $nudge;
  133. $left += $nudge;
  134. // But careful that this does not sample off the edge
  135. // "right" is the farthest-right valid pixel location -- right+1 is not necessarily
  136. // This is positive by how much the inner x loop below would be too large
  137. $nudgedTooFarRight = $left + (int)(($matrixWidth - 1) * $moduleSize) - $right;
  138. if ($nudgedTooFarRight > 0) {
  139. if ($nudgedTooFarRight > $nudge) {
  140. // Neither way fits; abort
  141. throw NotFoundException::getNotFoundInstance();
  142. }
  143. $left -= $nudgedTooFarRight;
  144. }
  145. // See logic above
  146. $nudgedTooFarDown = $top + (int)(($matrixHeight - 1) * $moduleSize) - $bottom;
  147. if ($nudgedTooFarDown > 0) {
  148. if ($nudgedTooFarDown > $nudge) {
  149. // Neither way fits; abort
  150. throw NotFoundException::getNotFoundInstance();
  151. }
  152. $top -= $nudgedTooFarDown;
  153. }
  154. // Now just read off the bits
  155. $bits = new BitMatrix($matrixWidth, $matrixHeight);
  156. for ($y = 0; $y < $matrixHeight; $y++) {
  157. $iOffset = $top + (int)($y * $moduleSize);
  158. for ($x = 0; $x < $matrixWidth; $x++) {
  159. if ($image->get($left + (int)($x * $moduleSize), $iOffset)) {
  160. $bits->set($x, $y);
  161. }
  162. }
  163. }
  164. return $bits;
  165. }
  166. private static function moduleSize($leftTopBlack, BitMatrix $image)
  167. {
  168. $height = $image->getHeight();
  169. $width = $image->getWidth();
  170. $x = $leftTopBlack[0];
  171. $y = $leftTopBlack[1];
  172. /*$x = $leftTopBlack[0];
  173. $y = $leftTopBlack[1];*/
  174. $inBlack = true;
  175. $transitions = 0;
  176. while ($x < $width && $y < $height) {
  177. if ($inBlack != $image->get($x, $y)) {
  178. if (++$transitions == 5) {
  179. break;
  180. }
  181. $inBlack = !$inBlack;
  182. }
  183. $x++;
  184. $y++;
  185. }
  186. if ($x == $width || $y == $height) {
  187. throw NotFoundException::getNotFoundInstance();
  188. }
  189. return ($x - $leftTopBlack[0]) / 7.0; //return ($x - $leftTopBlack[0]) / 7.0f;
  190. }
  191. public function reset()
  192. {
  193. // do nothing
  194. }
  195. protected final function getDecoder()
  196. {
  197. return $this->decoder;
  198. }
  199. }