Detector.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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\Detector;
  18. use Zxing\DecodeHintType;
  19. use Zxing\FormatException;
  20. use Zxing\NotFoundException;
  21. use Zxing\ResultPoint;
  22. use Zxing\ResultPointCallback;
  23. use Zxing\Common\BitMatrix;
  24. use Zxing\Common\DetectorResult;
  25. use Zxing\Common\GridSampler;
  26. use Zxing\Common\PerspectiveTransform;
  27. use Zxing\Common\Detector\MathUtils;
  28. use Zxing\Qrcode\Decoder\Version;
  29. /**
  30. * <p>Encapsulates logic that can detect a QR Code in an image, even if the QR Code
  31. * is rotated or skewed, or partially obscured.</p>
  32. *
  33. * @author Sean Owen
  34. */
  35. ?>
  36. <?php
  37. class Detector {
  38. private $image;
  39. private $resultPointCallback;
  40. public function __construct($image) {
  41. $this->image = $image;
  42. }
  43. protected final function getImage() {
  44. return $this->image;
  45. }
  46. protected final function getResultPointCallback() {
  47. return $this->resultPointCallback;
  48. }
  49. /**
  50. * <p>Detects a QR Code in an image.</p>
  51. *
  52. * @return {@link DetectorResult} encapsulating results of detecting a QR Code
  53. * @throws NotFoundException if QR Code cannot be found
  54. * @throws FormatException if a QR Code cannot be decoded
  55. */
  56. /**
  57. * <p>Detects a QR Code in an image.</p>
  58. *
  59. * @param hints optional hints to detector
  60. * @return {@link DetectorResult} encapsulating results of detecting a QR Code
  61. * @throws NotFoundException if QR Code cannot be found
  62. * @throws FormatException if a QR Code cannot be decoded
  63. */
  64. public final function detect($hints=null){/*Map<DecodeHintType,?>*/
  65. $resultPointCallback = $hints == null ? null :
  66. $hints->get('NEED_RESULT_POINT_CALLBACK');
  67. /* resultPointCallback = hints == null ? null :
  68. (ResultPointCallback) hints.get(DecodeHintType.NEED_RESULT_POINT_CALLBACK);*/
  69. $finder = new FinderPatternFinder($this->image, $resultPointCallback);
  70. $info = $finder->find($hints);
  71. return $this->processFinderPatternInfo($info);
  72. }
  73. protected final function processFinderPatternInfo($info){
  74. $topLeft = $info->getTopLeft();
  75. $topRight = $info->getTopRight();
  76. $bottomLeft = $info->getBottomLeft();
  77. $moduleSize = (float) $this->calculateModuleSize($topLeft, $topRight, $bottomLeft);
  78. if ($moduleSize < 1.0) {
  79. throw NotFoundException::getNotFoundInstance();
  80. }
  81. $dimension =(int) $this->computeDimension($topLeft, $topRight, $bottomLeft, $moduleSize);
  82. $provisionalVersion = Version::getProvisionalVersionForDimension($dimension);
  83. $modulesBetweenFPCenters = $provisionalVersion->getDimensionForVersion() - 7;
  84. $alignmentPattern = null;
  85. // Anything above version 1 has an alignment pattern
  86. if (count($provisionalVersion->getAlignmentPatternCenters())> 0) {
  87. // Guess where a "bottom right" finder pattern would have been
  88. $bottomRightX = $topRight->getX() - $topLeft->getX() + $bottomLeft->getX();
  89. $bottomRightY = $topRight->getY() - $topLeft->getY() + $bottomLeft->getY();
  90. // Estimate that alignment pattern is closer by 3 modules
  91. // from "bottom right" to known top left location
  92. $correctionToTopLeft = 1.0 - 3.0 / (float) $modulesBetweenFPCenters;
  93. $estAlignmentX = (int) ($topLeft->getX() + $correctionToTopLeft * ($bottomRightX - $topLeft->getX()));
  94. $estAlignmentY = (int) ($topLeft->getY() + $correctionToTopLeft * ($bottomRightY - $topLeft->getY()));
  95. // Kind of arbitrary -- expand search radius before giving up
  96. for ($i = 4; $i <= 16; $i <<= 1) {//??????????
  97. try {
  98. $alignmentPattern = $this->findAlignmentInRegion($moduleSize,
  99. $estAlignmentX,
  100. $estAlignmentY,
  101. (float) $i);
  102. break;
  103. } catch (NotFoundException $re) {
  104. // try next round
  105. }
  106. }
  107. // If we didn't find alignment pattern... well try anyway without it
  108. }
  109. $transform =
  110. $this->createTransform($topLeft, $topRight, $bottomLeft, $alignmentPattern, $dimension);
  111. $bits = $this->sampleGrid($this->image, $transform, $dimension);
  112. $points = array();
  113. if ($alignmentPattern == null) {
  114. $points = array($bottomLeft, $topLeft, $topRight);
  115. } else {
  116. // die('$points = new ResultPoint[]{bottomLeft, topLeft, topRight, alignmentPattern};');
  117. $points = array($bottomLeft, $topLeft, $topRight, $alignmentPattern);
  118. }
  119. return new DetectorResult($bits, $points);
  120. }
  121. private static function createTransform($topLeft,
  122. $topRight,
  123. $bottomLeft,
  124. $alignmentPattern,
  125. $dimension) {
  126. $dimMinusThree = (float) $dimension - 3.5;
  127. $bottomRightX = 0.0;
  128. $bottomRightY = 0.0;
  129. $sourceBottomRightX = 0.0;
  130. $sourceBottomRightY = 0.0;
  131. if ($alignmentPattern != null) {
  132. $bottomRightX = $alignmentPattern->getX();
  133. $bottomRightY = $alignmentPattern->getY();
  134. $sourceBottomRightX = $dimMinusThree - 3.0;
  135. $sourceBottomRightY = $sourceBottomRightX;
  136. } else {
  137. // Don't have an alignment pattern, just make up the bottom-right point
  138. $bottomRightX = ($topRight->getX() - $topLeft->getX()) + $bottomLeft->getX();
  139. $bottomRightY = ($topRight->getY() - $topLeft->getY()) + $bottomLeft->getY();
  140. $sourceBottomRightX = $dimMinusThree;
  141. $sourceBottomRightY = $dimMinusThree;
  142. }
  143. return PerspectiveTransform::quadrilateralToQuadrilateral(
  144. 3.5,
  145. 3.5,
  146. $dimMinusThree,
  147. 3.5,
  148. $sourceBottomRightX,
  149. $sourceBottomRightY,
  150. 3.5,
  151. $dimMinusThree,
  152. $topLeft->getX(),
  153. $topLeft->getY(),
  154. $topRight->getX(),
  155. $topRight->getY(),
  156. $bottomRightX,
  157. $bottomRightY,
  158. $bottomLeft->getX(),
  159. $bottomLeft->getY());
  160. }
  161. private static function sampleGrid($image, $transform,
  162. $dimension) {
  163. $sampler = GridSampler::getInstance();
  164. return $sampler->sampleGrid_($image, $dimension, $dimension, $transform);
  165. }
  166. /**
  167. * <p>Computes the dimension (number of modules on a size) of the QR Code based on the position
  168. * of the finder patterns and estimated module size.</p>
  169. */
  170. private static function computeDimension($topLeft,
  171. $topRight,
  172. $bottomLeft,
  173. $moduleSize) {
  174. $tltrCentersDimension = MathUtils::round(ResultPoint::distance($topLeft, $topRight) / $moduleSize);
  175. $tlblCentersDimension = MathUtils::round(ResultPoint::distance($topLeft, $bottomLeft) / $moduleSize);
  176. $dimension = (($tltrCentersDimension + $tlblCentersDimension) / 2) + 7;
  177. switch ($dimension & 0x03) { // mod 4
  178. case 0:
  179. $dimension++;
  180. break;
  181. // 1? do nothing
  182. case 2:
  183. $dimension--;
  184. break;
  185. case 3:
  186. throw NotFoundException::getNotFoundInstance();
  187. }
  188. return $dimension;
  189. }
  190. /**
  191. * <p>Computes an average estimated module size based on estimated derived from the positions
  192. * of the three finder patterns.</p>
  193. *
  194. * @param topLeft detected top-left finder pattern center
  195. * @param topRight detected top-right finder pattern center
  196. * @param bottomLeft detected bottom-left finder pattern center
  197. * @return estimated module size
  198. */
  199. protected final function calculateModuleSize($topLeft,
  200. $topRight,
  201. $bottomLeft) {
  202. // Take the average
  203. return ($this->calculateModuleSizeOneWay($topLeft, $topRight) +
  204. $this->calculateModuleSizeOneWay($topLeft, $bottomLeft)) / 2.0;
  205. }
  206. /**
  207. * <p>Estimates module size based on two finder patterns -- it uses
  208. * {@link #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int)} to figure the
  209. * width of each, measuring along the axis between their centers.</p>
  210. */
  211. private function calculateModuleSizeOneWay($pattern, $otherPattern) {
  212. $moduleSizeEst1 = $this->sizeOfBlackWhiteBlackRunBothWays($pattern->getX(),
  213. (int) $pattern->getY(),
  214. (int) $otherPattern->getX(),
  215. (int) $otherPattern->getY());
  216. $moduleSizeEst2 = $this->sizeOfBlackWhiteBlackRunBothWays((int) $otherPattern->getX(),
  217. (int) $otherPattern->getY(),
  218. (int) $pattern->getX(),
  219. (int) $pattern->getY());
  220. if (is_nan($moduleSizeEst1)) {
  221. return $moduleSizeEst2 / 7.0;
  222. }
  223. if (is_nan($moduleSizeEst2)) {
  224. return $moduleSizeEst1 / 7.0;
  225. }
  226. // Average them, and divide by 7 since we've counted the width of 3 black modules,
  227. // and 1 white and 1 black module on either side. Ergo, divide sum by 14.
  228. return ($moduleSizeEst1 + $moduleSizeEst2) / 14.0;
  229. }
  230. /**
  231. * See {@link #sizeOfBlackWhiteBlackRun(int, int, int, int)}; computes the total width of
  232. * a finder pattern by looking for a black-white-black run from the center in the direction
  233. * of another po$(another finder pattern center), and in the opposite direction too.</p>
  234. */
  235. private function sizeOfBlackWhiteBlackRunBothWays($fromX, $fromY, $toX, $toY) {
  236. $result = $this->sizeOfBlackWhiteBlackRun($fromX, $fromY, $toX, $toY);
  237. // Now count other way -- don't run off image though of course
  238. $scale = 1.0;
  239. $otherToX = $fromX - ($toX - $fromX);
  240. if ($otherToX < 0) {
  241. $scale = (float) $fromX / (float) ($fromX - $otherToX);
  242. $otherToX = 0;
  243. } else if ($otherToX >= $this->image->getWidth()) {
  244. $scale = (float) ($this->image->getWidth() - 1 - $fromX) / (float) ($otherToX - $fromX);
  245. $otherToX = $this->image->getWidth() - 1;
  246. }
  247. $otherToY = (int) ($fromY - ($toY - $fromY) * $scale);
  248. $scale = 1.0;
  249. if ($otherToY < 0) {
  250. $scale = (float) $fromY / (float) ($fromY - $otherToY);
  251. $otherToY = 0;
  252. } else if ($otherToY >= $this->image->getHeight()) {
  253. $scale = (float) ($this->image->getHeight() - 1 - $fromY) / (float) ($otherToY - $fromY);
  254. $otherToY = $this->image->getHeight() - 1;
  255. }
  256. $otherToX = (int) ($fromX + ($otherToX - $fromX) * $scale);
  257. $result += $this->sizeOfBlackWhiteBlackRun($fromX, $fromY, $otherToX, $otherToY);
  258. // Middle pixel is double-counted this way; subtract 1
  259. return $result - 1.0;
  260. }
  261. /**
  262. * <p>This method traces a line from a po$in the image, in the direction towards another point.
  263. * It begins in a black region, and keeps going until it finds white, then black, then white again.
  264. * It reports the distance from the start to this point.</p>
  265. *
  266. * <p>This is used when figuring out how wide a finder pattern is, when the finder pattern
  267. * may be skewed or rotated.</p>
  268. */
  269. private function sizeOfBlackWhiteBlackRun($fromX, $fromY, $toX, $toY) {
  270. // Mild variant of Bresenham's algorithm;
  271. // see http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
  272. $steep = abs($toY - $fromY) > abs($toX - $fromX);
  273. if ($steep) {
  274. $temp = $fromX;
  275. $fromX = $fromY;
  276. $fromY = $temp;
  277. $temp = $toX;
  278. $toX = $toY;
  279. $toY = $temp;
  280. }
  281. $dx = abs($toX - $fromX);
  282. $dy = abs($toY - $fromY);
  283. $error = -$dx / 2;
  284. $xstep = $fromX < $toX ? 1 : -1;
  285. $ystep = $fromY < $toY ? 1 : -1;
  286. // In black pixels, looking for white, first or second time.
  287. $state = 0;
  288. // Loop up until x == toX, but not beyond
  289. $xLimit = $toX + $xstep;
  290. for ($x = $fromX, $y = $fromY; $x != $xLimit; $x += $xstep) {
  291. $realX = $steep ? $y : $x;
  292. $realY = $steep ? $x : $y;
  293. // Does current pixel mean we have moved white to black or vice versa?
  294. // Scanning black in state 0,2 and white in state 1, so if we find the wrong
  295. // color, advance to next state or end if we are in state 2 already
  296. if (($state == 1) == $this->image->get($realX, $realY)) {
  297. if ($state == 2) {
  298. return MathUtils::distance($x, $y, $fromX, $fromY);
  299. }
  300. $state++;
  301. }
  302. $error += $dy;
  303. if ($error > 0) {
  304. if ($y == $toY) {
  305. break;
  306. }
  307. $y += $ystep;
  308. $error -= $dx;
  309. }
  310. }
  311. // Found black-white-black; give the benefit of the doubt that the next pixel outside the image
  312. // is "white" so this last po$at (toX+xStep,toY) is the right ending. This is really a
  313. // small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this.
  314. if ($state == 2) {
  315. return MathUtils::distance($toX + $xstep, $toY, $fromX, $fromY);
  316. }
  317. // else we didn't find even black-white-black; no estimate is really possible
  318. return NAN;
  319. }
  320. /**
  321. * <p>Attempts to locate an alignment pattern in a limited region of the image, which is
  322. * guessed to contain it. This method uses {@link AlignmentPattern}.</p>
  323. *
  324. * @param overallEstModuleSize estimated module size so far
  325. * @param estAlignmentX x coordinate of center of area probably containing alignment pattern
  326. * @param estAlignmentY y coordinate of above
  327. * @param allowanceFactor number of pixels in all directions to search from the center
  328. * @return {@link AlignmentPattern} if found, or null otherwise
  329. * @throws NotFoundException if an unexpected error occurs during detection
  330. */
  331. protected final function findAlignmentInRegion($overallEstModuleSize,
  332. $estAlignmentX,
  333. $estAlignmentY,
  334. $allowanceFactor)
  335. {
  336. // Look for an alignment pattern (3 modules in size) around where it
  337. // should be
  338. $allowance = (int) ($allowanceFactor * $overallEstModuleSize);
  339. $alignmentAreaLeftX = max(0, $estAlignmentX - $allowance);
  340. $alignmentAreaRightX = min($this->image->getWidth() - 1, $estAlignmentX + $allowance);
  341. if ($alignmentAreaRightX - $alignmentAreaLeftX < $overallEstModuleSize * 3) {
  342. throw NotFoundException::getNotFoundInstance();
  343. }
  344. $alignmentAreaTopY = max(0, $estAlignmentY - $allowance);
  345. $alignmentAreaBottomY = min($this->image->getHeight() - 1, $estAlignmentY + $allowance);
  346. if ($alignmentAreaBottomY - $alignmentAreaTopY < $overallEstModuleSize * 3) {
  347. throw NotFoundException::getNotFoundInstance();
  348. }
  349. $alignmentFinder =
  350. new AlignmentPatternFinder(
  351. $this->image,
  352. $alignmentAreaLeftX,
  353. $alignmentAreaTopY,
  354. $alignmentAreaRightX - $alignmentAreaLeftX,
  355. $alignmentAreaBottomY - $alignmentAreaTopY,
  356. $overallEstModuleSize,
  357. $this->resultPointCallback);
  358. return $alignmentFinder->find();
  359. }
  360. }