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