Detector.php 11 KB

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