GridSampler.php 6.3 KB

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