DefaultGridSampler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Common;
  18. use Zxing\NotFoundException;
  19. /**
  20. * @author Sean Owen
  21. */
  22. final class DefaultGridSampler extends GridSampler
  23. {
  24. //@Override
  25. public function sampleGrid(
  26. $image,
  27. $dimensionX,
  28. $dimensionY,
  29. $p1ToX,
  30. $p1ToY,
  31. $p2ToX,
  32. $p2ToY,
  33. $p3ToX,
  34. $p3ToY,
  35. $p4ToX,
  36. $p4ToY,
  37. $p1FromX,
  38. $p1FromY,
  39. $p2FromX,
  40. $p2FromY,
  41. $p3FromX,
  42. $p3FromY,
  43. $p4FromX,
  44. $p4FromY
  45. ) {
  46. $transform = PerspectiveTransform::quadrilateralToQuadrilateral(
  47. $p1ToX,
  48. $p1ToY,
  49. $p2ToX,
  50. $p2ToY,
  51. $p3ToX,
  52. $p3ToY,
  53. $p4ToX,
  54. $p4ToY,
  55. $p1FromX,
  56. $p1FromY,
  57. $p2FromX,
  58. $p2FromY,
  59. $p3FromX,
  60. $p3FromY,
  61. $p4FromX,
  62. $p4FromY
  63. );
  64. return $this->sampleGrid_($image, $dimensionX, $dimensionY, $transform);
  65. }
  66. //@Override
  67. public function sampleGrid_(
  68. $image,
  69. $dimensionX,
  70. $dimensionY,
  71. $transform
  72. ) {
  73. if ($dimensionX <= 0 || $dimensionY <= 0) {
  74. throw NotFoundException::getNotFoundInstance();
  75. }
  76. $bits = new BitMatrix($dimensionX, $dimensionY);
  77. $points = fill_array(0, 2 * $dimensionX, 0.0);
  78. for ($y = 0; $y < $dimensionY; $y++) {
  79. $max = is_countable($points) ? count($points) : 0;
  80. $iValue = (float)$y + 0.5;
  81. for ($x = 0; $x < $max; $x += 2) {
  82. $points[$x] = (float)($x / 2) + 0.5;
  83. $points[$x + 1] = $iValue;
  84. }
  85. $transform->transformPoints($points);
  86. // Quick check to see if points transformed to something inside the image;
  87. // sufficient to check the endpoints
  88. self::checkAndNudgePoints($image, $points);
  89. try {
  90. for ($x = 0; $x < $max; $x += 2) {
  91. if ($image->get((int)$points[$x], (int)$points[$x + 1])) {
  92. // Black(-ish) pixel
  93. $bits->set($x / 2, $y);
  94. }
  95. }
  96. } catch (\Exception) {//ArrayIndexOutOfBoundsException
  97. // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
  98. // transform gets "twisted" such that it maps a straight line of points to a set of points
  99. // whose endpoints are in bounds, but others are not. There is probably some mathematical
  100. // way to detect this about the transformation that I don't know yet.
  101. // This results in an ugly runtime exception despite our clever checks above -- can't have
  102. // that. We could check each point's coordinates but that feels duplicative. We settle for
  103. // catching and wrapping ArrayIndexOutOfBoundsException.
  104. throw NotFoundException::getNotFoundInstance();
  105. }
  106. }
  107. return $bits;
  108. }
  109. }