AlignmentPatternFinder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\NotFoundException;
  19. use Zxing\ResultPointCallback;
  20. use Zxing\Common\BitMatrix;
  21. /**
  22. * <p>This class attempts to find alignment patterns in a QR Code. Alignment patterns look like finder
  23. * patterns but are smaller and appear at regular intervals throughout the image.</p>
  24. *
  25. * <p>At the moment this only looks for the bottom-right alignment pattern.</p>
  26. *
  27. * <p>This is mostly a simplified copy of {@link FinderPatternFinder}. It is copied,
  28. * pasted and stripped down here for maximum performance but does unfortunately duplicate
  29. * some code.</p>
  30. *
  31. * <p>This class is thread-safe but not reentrant. Each thread must allocate its own object.</p>
  32. *
  33. * @author Sean Owen
  34. */
  35. final class AlignmentPatternFinder {
  36. private $image;
  37. private $possibleCenters;
  38. private $startX;
  39. private $startY;
  40. private $width;
  41. private $height;
  42. private $moduleSize;
  43. private $crossCheckStateCount;
  44. private $resultPointCallback;
  45. /**
  46. * <p>Creates a finder that will look in a portion of the whole image.</p>
  47. *
  48. * @param image image to search
  49. * @param startX left column from which to start searching
  50. * @param startY top row from which to start searching
  51. * @param width width of region to search
  52. * @param height height of region to search
  53. * @param moduleSize estimated module size so far
  54. */
  55. function __construct($image,
  56. $startX,
  57. $startY,
  58. $width,
  59. $height,
  60. $moduleSize,
  61. $resultPointCallback) {
  62. $this->image = $image;
  63. $this->possibleCenters = array();
  64. $this->startX = $startX;
  65. $this->startY = $startY;
  66. $this->width = $width;
  67. $this->height = $height;
  68. $this->moduleSize = $moduleSize;
  69. $this->crossCheckStateCount = array();
  70. $this->resultPointCallback = $resultPointCallback;
  71. }
  72. /**
  73. * <p>This method attempts to find the bottom-right alignment pattern in the image. It is a bit messy since
  74. * it's pretty performance-critical and so is written to be fast foremost.</p>
  75. *
  76. * @return {@link AlignmentPattern} if found
  77. * @throws NotFoundException if not found
  78. */
  79. function find() {
  80. $startX = $this->startX;
  81. $height = $this->height;
  82. $maxJ = $startX + $this->width;
  83. $middleI = $this->startY + ($height / 2);
  84. // We are looking for black/white/black modules in 1:1:1 ratio;
  85. // this tracks the number of black/white/black modules seen so far
  86. $stateCount = array();
  87. for ($iGen = 0; $iGen < $height; $iGen++) {
  88. // Search from middle outwards
  89. $i = $middleI + (($iGen & 0x01) == 0 ? ($iGen + 1) / 2 : -(($iGen + 1) / 2));
  90. $i = intval($i);
  91. $stateCount[0] = 0;
  92. $stateCount[1] = 0;
  93. $stateCount[2] = 0;
  94. $j = $startX;
  95. // Burn off leading white pixels before anything else; if we start in the middle of
  96. // a white run, it doesn't make sense to count its length, since we don't know if the
  97. // white run continued to the left of the start point
  98. while ($j < $maxJ && !$this->image->get($j, $i)) {
  99. $j++;
  100. }
  101. $currentState = 0;
  102. while ($j < $maxJ) {
  103. if ($this->image->get($j, $i)) {
  104. // Black pixel
  105. if ($currentState == 1) { // Counting black pixels
  106. $stateCount[$currentState]++;
  107. } else { // Counting white pixels
  108. if ($currentState == 2) { // A winner?
  109. if ($this->foundPatternCross($stateCount)) { // Yes
  110. $confirmed = $this->handlePossibleCenter($stateCount, $i, $j);
  111. if ($confirmed != null) {
  112. return $confirmed;
  113. }
  114. }
  115. $stateCount[0] = $stateCount[2];
  116. $stateCount[1] = 1;
  117. $stateCount[2] = 0;
  118. $currentState = 1;
  119. } else {
  120. $stateCount[++$currentState]++;
  121. }
  122. }
  123. } else { // White pixel
  124. if ($currentState == 1) { // Counting black pixels
  125. $currentState++;
  126. }
  127. $stateCount[$currentState]++;
  128. }
  129. $j++;
  130. }
  131. if ($this->foundPatternCross($stateCount)) {
  132. $confirmed = $this->handlePossibleCenter($stateCount, $i, $maxJ);
  133. if ($confirmed != null) {
  134. return $confirmed;
  135. }
  136. }
  137. }
  138. // Hmm, nothing we saw was observed and confirmed twice. If we had
  139. // any guess at all, return it.
  140. if (count($this->possibleCenters)) {
  141. return $this->possibleCenters[0];
  142. }
  143. throw NotFoundException::getNotFoundInstance();
  144. }
  145. /**
  146. * Given a count of black/white/black pixels just seen and an end position,
  147. * figures the location of the center of this black/white/black run.
  148. */
  149. private static function centerFromEnd($stateCount, $end) {
  150. return (float) ($end - $stateCount[2]) - $stateCount[1] / 2.0;
  151. }
  152. /**
  153. * @param stateCount count of black/white/black pixels just read
  154. * @return true iff the proportions of the counts is close enough to the 1/1/1 ratios
  155. * used by alignment patterns to be considered a match
  156. */
  157. private function foundPatternCross($stateCount) {
  158. $moduleSize = $this->moduleSize;
  159. $maxVariance = $moduleSize / 2.0;
  160. for ($i = 0; $i < 3; $i++) {
  161. if (abs($moduleSize - $stateCount[$i]) >= $maxVariance) {
  162. return false;
  163. }
  164. }
  165. return true;
  166. }
  167. /**
  168. * <p>After a horizontal scan finds a potential alignment pattern, this method
  169. * "cross-checks" by scanning down vertically through the center of the possible
  170. * alignment pattern to see if the same proportion is detected.</p>
  171. *
  172. * @param startI row where an alignment pattern was detected
  173. * @param centerJ center of the section that appears to cross an alignment pattern
  174. * @param maxCount maximum reasonable number of modules that should be
  175. * observed in any reading state, based on the results of the horizontal scan
  176. * @return vertical center of alignment pattern, or {@link Float#NaN} if not found
  177. */
  178. private function crossCheckVertical($startI, $centerJ, $maxCount,
  179. $originalStateCountTotal) {
  180. $image = $this->image;
  181. $maxI = $image->getHeight();
  182. $stateCount = $this->crossCheckStateCount;
  183. $stateCount[0] = 0;
  184. $stateCount[1] = 0;
  185. $stateCount[2] = 0;
  186. // Start counting up from center
  187. $i = $startI;
  188. while ($i >= 0 && $image->get($centerJ, $i) && $stateCount[1] <= $maxCount) {
  189. $stateCount[1]++;
  190. $i--;
  191. }
  192. // If already too many modules in this state or ran off the edge:
  193. if ($i < 0 || $stateCount[1] > $maxCount) {
  194. return NAN;
  195. }
  196. while ($i >= 0 && !$image->get($centerJ, $i) && $stateCount[0] <= $maxCount) {
  197. $stateCount[0]++;
  198. $i--;
  199. }
  200. if ($stateCount[0] > $maxCount) {
  201. return NAN;
  202. }
  203. // Now also count down from center
  204. $i = $startI + 1;
  205. while ($i < $maxI && $image->get($centerJ, $i) && $stateCount[1] <= $maxCount) {
  206. $stateCount[1]++;
  207. $i++;
  208. }
  209. if ($i == $maxI || $stateCount[1] > $maxCount) {
  210. return NAN;
  211. }
  212. while ($i < $maxI && !$image->get($centerJ, $i) && $stateCount[2] <= $maxCount) {
  213. $stateCount[2]++;
  214. $i++;
  215. }
  216. if ($stateCount[2] > $maxCount) {
  217. return NAN;
  218. }
  219. $stateCountTotal = $stateCount[0] + $stateCount[1] + $stateCount[2];
  220. if (5 * abs($stateCountTotal - $originalStateCountTotal) >= 2 * $originalStateCountTotal) {
  221. return NAN;
  222. }
  223. return $this->foundPatternCross($stateCount) ? $this->centerFromEnd($stateCount, $i) : NAN;
  224. }
  225. /**
  226. * <p>This is called when a horizontal scan finds a possible alignment pattern. It will
  227. * cross check with a vertical scan, and if successful, will see if this pattern had been
  228. * found on a previous horizontal scan. If so, we consider it confirmed and conclude we have
  229. * found the alignment pattern.</p>
  230. *
  231. * @param stateCount reading state module counts from horizontal scan
  232. * @param i row where alignment pattern may be found
  233. * @param j end of possible alignment pattern in row
  234. * @return {@link AlignmentPattern} if we have found the same pattern twice, or null if not
  235. */
  236. private function handlePossibleCenter($stateCount, $i, $j) {
  237. $stateCountTotal = $stateCount[0] + $stateCount[1] + $stateCount[2];
  238. $centerJ = $this->centerFromEnd($stateCount, $j);
  239. $centerI = $this->crossCheckVertical($i, (int) $centerJ, 2 * $stateCount[1], $stateCountTotal);
  240. if (!is_nan($centerI)) {
  241. $estimatedModuleSize = (float) ($stateCount[0] + $stateCount[1] + $stateCount[2]) / 3.0;
  242. foreach ($this->possibleCenters as $center) {
  243. // Look for about the same center and module size:
  244. if ($center->aboutEquals($estimatedModuleSize, $centerI, $centerJ)) {
  245. return $center->combineEstimate($centerI, $centerJ, $estimatedModuleSize);
  246. }
  247. }
  248. // Hadn't found this before; save it
  249. $point = new AlignmentPattern($centerJ, $centerI, $estimatedModuleSize);
  250. $this->possibleCenters[] = $point;
  251. if ($this->resultPointCallback != null) {
  252. $this->resultPointCallback->foundPossibleResultPoint($point);
  253. }
  254. }
  255. return null;
  256. }
  257. }