DefaultGridSampler.php 3.2 KB

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