GridSampler.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. * Implementations of this class can, given locations of finder patterns for a QR code in an
  21. * image, sample the right points in the image to reconstruct the QR code, accounting for
  22. * perspective distortion. It is abstracted since it is relatively expensive and should be allowed
  23. * to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
  24. * Imaging library, but which may not be available in other environments such as J2ME, and vice
  25. * versa.
  26. *
  27. * The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
  28. * with an instance of a class which implements this interface.
  29. *
  30. * @author Sean Owen
  31. */
  32. abstract class GridSampler {
  33. private static $gridSampler;
  34. /**
  35. * Sets the implementation of GridSampler used by the library. One global
  36. * instance is stored, which may sound problematic. But, the implementation provided
  37. * ought to be appropriate for the entire platform, and all uses of this library
  38. * in the whole lifetime of the JVM. For instance, an Android activity can swap in
  39. * an implementation that takes advantage of native platform libraries.
  40. *
  41. * @param newGridSampler The platform-specific object to install.
  42. */
  43. public static function setGridSampler($newGridSampler) {
  44. self::$gridSampler = $newGridSampler;
  45. }
  46. /**
  47. * @return the current implementation of GridSampler
  48. */
  49. public static function getInstance() {
  50. if(!self::$gridSampler){
  51. self::$gridSampler = new DefaultGridSampler();
  52. }
  53. return self::$gridSampler;
  54. }
  55. /**
  56. * Samples an image for a rectangular matrix of bits of the given dimension. The sampling
  57. * transformation is determined by the coordinates of 4 points, in the original and transformed
  58. * image space.
  59. *
  60. * @param image image to sample
  61. * @param dimensionX width of {@link BitMatrix} to sample from image
  62. * @param dimensionY height of {@link BitMatrix} to sample from image
  63. * @param p1ToX point 1 preimage X
  64. * @param p1ToY point 1 preimage Y
  65. * @param p2ToX point 2 preimage X
  66. * @param p2ToY point 2 preimage Y
  67. * @param p3ToX point 3 preimage X
  68. * @param p3ToY point 3 preimage Y
  69. * @param p4ToX point 4 preimage X
  70. * @param p4ToY point 4 preimage Y
  71. * @param p1FromX point 1 image X
  72. * @param p1FromY point 1 image Y
  73. * @param p2FromX point 2 image X
  74. * @param p2FromY point 2 image Y
  75. * @param p3FromX point 3 image X
  76. * @param p3FromY point 3 image Y
  77. * @param p4FromX point 4 image X
  78. * @param p4FromY point 4 image Y
  79. * @return {@link BitMatrix} representing a grid of points sampled from the image within a region
  80. * defined by the "from" parameters
  81. * @throws NotFoundException if image can't be sampled, for example, if the transformation defined
  82. * by the given points is invalid or results in sampling outside the image boundaries
  83. */
  84. public abstract function sampleGrid($image,
  85. $dimensionX,
  86. $dimensionY,
  87. $p1ToX, $p1ToY,
  88. $p2ToX, $p2ToY,
  89. $p3ToX, $p3ToY,
  90. $p4ToX, $p4ToY,
  91. $p1FromX, $p1FromY,
  92. $p2FromX, $p2FromY,
  93. $p3FromX, $p3FromY,
  94. $p4FromX, $p4FromY);
  95. public abstract function sampleGrid_($image,
  96. $dimensionX,
  97. $dimensionY,
  98. $transform);
  99. /**
  100. * <p>Checks a set of points that have been transformed to sample points on an image against
  101. * the image's dimensions to see if the point are even within the image.</p>
  102. *
  103. * <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
  104. * barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
  105. * patterns in an image where the QR Code runs all the way to the image border.</p>
  106. *
  107. * <p>For efficiency, the method will check points from either end of the line until one is found
  108. * to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
  109. *
  110. * @param image image into which the points should map
  111. * @param points actual points in x1,y1,...,xn,yn form
  112. * @throws NotFoundException if an endpoint is lies outside the image boundaries
  113. */
  114. protected static function checkAndNudgePoints($image,
  115. $points) {
  116. $width = $image->getWidth();
  117. $height = $image->getHeight();
  118. // Check and nudge points from start until we see some that are OK:
  119. $nudged = true;
  120. for ($offset = 0; $offset < count($points) && $nudged; $offset += 2) {
  121. $x = (int) $points[$offset];
  122. $y = (int) $points[$offset + 1];
  123. if ($x < -1 || $x > $width || $y < -1 || $y > $height) {
  124. throw NotFoundException::getNotFoundInstance();
  125. }
  126. $nudged = false;
  127. if ($x == -1) {
  128. $points[$offset] = 0.0;
  129. $nudged = true;
  130. } else if ($x == $width) {
  131. $points[$offset] = $width - 1;
  132. $nudged = true;
  133. }
  134. if ($y == -1) {
  135. $points[$offset + 1] = 0.0;
  136. $nudged = true;
  137. } else if ($y == $height) {
  138. $points[$offset + 1] = $height - 1;
  139. $nudged = true;
  140. }
  141. }
  142. // Check and nudge points from end:
  143. $nudged = true;
  144. for ($offset = count($points) - 2; $offset >= 0 && $nudged; $offset -= 2) {
  145. $x = (int) $points[$offset];
  146. $y = (int) $points[$offset + 1];
  147. if ($x < -1 || $x > $width || $y < -1 || $y > $height) {
  148. throw NotFoundException::getNotFoundInstance();
  149. }
  150. $nudged = false;
  151. if ($x == -1) {
  152. $points[$offset] = 0.0;
  153. $nudged = true;
  154. } else if ($x == $width) {
  155. $points[$offset] = $width - 1;
  156. $nudged = true;
  157. }
  158. if ($y == -1) {
  159. $points[$offset + 1] = 0.0;
  160. $nudged = true;
  161. } else if ($y == $height) {
  162. $points[$offset + 1] = $height - 1;
  163. $nudged = true;
  164. }
  165. }
  166. }
  167. }