DefaultGridSampler.php 3.5 KB

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