FinderPattern.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\ResultPoint;
  19. /**
  20. * <p>Encapsulates a finder pattern, which are the three square patterns found in
  21. * the corners of QR Codes. It also encapsulates a count of similar finder patterns,
  22. * as a convenience to the finder's bookkeeping.</p>
  23. *
  24. * @author Sean Owen
  25. */
  26. final class FinderPattern extends ResultPoint
  27. {
  28. private $estimatedModuleSize;
  29. private $count;
  30. public function __construct($posX, $posY, $estimatedModuleSize, $count = 1)
  31. {
  32. parent::__construct($posX, $posY);
  33. $this->estimatedModuleSize = $estimatedModuleSize;
  34. $this->count = $count;
  35. }
  36. public function getEstimatedModuleSize()
  37. {
  38. return $this->estimatedModuleSize;
  39. }
  40. public function getCount()
  41. {
  42. return $this->count;
  43. }
  44. /*
  45. void incrementCount() {
  46. this.count++;
  47. }
  48. */
  49. /**
  50. * <p>Determines if this finder pattern "about equals" a finder pattern at the stated
  51. * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
  52. */
  53. public function aboutEquals($moduleSize, $i, $j)
  54. {
  55. if (abs($i - $this->getY()) <= $moduleSize && abs($j - $this->getX()) <= $moduleSize) {
  56. $moduleSizeDiff = abs($moduleSize - $this->estimatedModuleSize);
  57. return $moduleSizeDiff <= 1.0 || $moduleSizeDiff <= $this->estimatedModuleSize;
  58. }
  59. return false;
  60. }
  61. /**
  62. * Combines this object's current estimate of a finder pattern position and module size
  63. * with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
  64. * based on count.
  65. */
  66. public function combineEstimate($i, $j, $newModuleSize)
  67. {
  68. $combinedCount = $this->count + 1;
  69. $combinedX = ($this->count * $this->getX() + $j) / $combinedCount;
  70. $combinedY = ($this->count * $this->getY() + $i) / $combinedCount;
  71. $combinedModuleSize = ($this->count * $this->estimatedModuleSize + $newModuleSize) / $combinedCount;
  72. return new FinderPattern($combinedX, $combinedY, $combinedModuleSize, $combinedCount);
  73. }
  74. }