AlignmentPattern.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 an alignment pattern, which are the smaller square patterns found in
  21. * all but the simplest QR Codes.</p>
  22. *
  23. * @author Sean Owen
  24. */
  25. final class AlignmentPattern extends ResultPoint
  26. {
  27. private $estimatedModuleSize;
  28. public function __construct($posX, $posY, $estimatedModuleSize)
  29. {
  30. parent::__construct($posX, $posY);
  31. $this->estimatedModuleSize = $estimatedModuleSize;
  32. }
  33. /**
  34. * <p>Determines if this alignment pattern "about equals" an alignment pattern at the stated
  35. * position and size -- meaning, it is at nearly the same center with nearly the same size.</p>
  36. */
  37. public function aboutEquals($moduleSize, $i, $j)
  38. {
  39. if (abs($i - $this->getY()) <= $moduleSize && abs($j - $this->getX()) <= $moduleSize) {
  40. $moduleSizeDiff = abs($moduleSize - $this->estimatedModuleSize);
  41. return $moduleSizeDiff <= 1.0 || $moduleSizeDiff <= $this->estimatedModuleSize;
  42. }
  43. return false;
  44. }
  45. /**
  46. * Combines this object's current estimate of a finder pattern position and module size
  47. * with a new estimate. It returns a new {@code FinderPattern} containing an average of the two.
  48. */
  49. public function combineEstimate($i, $j, $newModuleSize)
  50. {
  51. $combinedX = ($this->getX() + $j) / 2.0;
  52. $combinedY = ($this->getY() + $i) / 2.0;
  53. $combinedModuleSize = ($this->estimatedModuleSize + $newModuleSize) / 2.0;
  54. return new AlignmentPattern($combinedX, $combinedY, $combinedModuleSize);
  55. }
  56. }