QRCodeReader.php 7.9 KB

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