Detector.php 11 KB

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