GridSampler.php 7.0 KB

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