You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridSampler.cs 8.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. namespace ZXing.Common
  17. {
  18. /// <summary> Implementations of this class can, given locations of finder patterns for a QR code in an
  19. /// image, sample the right points in the image to reconstruct the QR code, accounting for
  20. /// perspective distortion. It is abstracted since it is relatively expensive and should be allowed
  21. /// to take advantage of platform-specific optimized implementations, like Sun's Java Advanced
  22. /// Imaging library, but which may not be available in other environments such as J2ME, and vice
  23. /// versa.
  24. ///
  25. /// The implementation used can be controlled by calling {@link #setGridSampler(GridSampler)}
  26. /// with an instance of a class which implements this interface.
  27. ///
  28. /// </summary>
  29. /// <author> Sean Owen
  30. /// </author>
  31. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  32. /// </author>
  33. public abstract class GridSampler
  34. {
  35. /// <returns> the current implementation of {@link GridSampler}
  36. /// </returns>
  37. public static GridSampler Instance
  38. {
  39. get
  40. {
  41. return gridSampler;
  42. }
  43. }
  44. private static GridSampler gridSampler = new DefaultGridSampler();
  45. /// <summary> Sets the implementation of {@link GridSampler} used by the library. One global
  46. /// instance is stored, which may sound problematic. But, the implementation provided
  47. /// ought to be appropriate for the entire platform, and all uses of this library
  48. /// in the whole lifetime of the JVM. For instance, an Android activity can swap in
  49. /// an implementation that takes advantage of native platform libraries.
  50. ///
  51. /// </summary>
  52. /// <param name="newGridSampler">The platform-specific object to install.
  53. /// </param>
  54. public static void setGridSampler(GridSampler newGridSampler)
  55. {
  56. if (newGridSampler == null)
  57. {
  58. throw new System.ArgumentException();
  59. }
  60. gridSampler = newGridSampler;
  61. }
  62. /// <summary> <p>Samples an image for a square matrix of bits of the given dimension. This is used to extract
  63. /// the black/white modules of a 2D barcode like a QR Code found in an image. Because this barcode
  64. /// may be rotated or perspective-distorted, the caller supplies four points in the source image
  65. /// that define known points in the barcode, so that the image may be sampled appropriately.</p>
  66. ///
  67. /// <p>The last eight "from" parameters are four X/Y coordinate pairs of locations of points in
  68. /// the image that define some significant points in the image to be sample. For example,
  69. /// these may be the location of finder pattern in a QR Code.</p>
  70. ///
  71. /// <p>The first eight "to" parameters are four X/Y coordinate pairs measured in the destination
  72. /// {@link BitMatrix}, from the top left, where the known points in the image given by the "from"
  73. /// parameters map to.</p>
  74. ///
  75. /// <p>These 16 parameters define the transformation needed to sample the image.</p>
  76. ///
  77. /// </summary>
  78. /// <param name="image">image to sample
  79. /// </param>
  80. /// <param name="dimension">width/height of {@link BitMatrix} to sample from image
  81. /// </param>
  82. /// <returns> {@link BitMatrix} representing a grid of points sampled from the image within a region
  83. /// defined by the "from" parameters
  84. /// </returns>
  85. /// <throws> ReaderException if image can't be sampled, for example, if the transformation defined </throws>
  86. /// <summary> by the given points is invalid or results in sampling outside the image boundaries
  87. /// </summary>
  88. public abstract BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, float p1ToX, float p1ToY, float p2ToX, float p2ToY, float p3ToX, float p3ToY, float p4ToX, float p4ToY, float p1FromX, float p1FromY, float p2FromX, float p2FromY, float p3FromX, float p3FromY, float p4FromX, float p4FromY);
  89. public virtual BitMatrix sampleGrid(BitMatrix image, int dimensionX, int dimensionY, PerspectiveTransform transform)
  90. {
  91. throw new System.NotSupportedException();
  92. }
  93. /// <summary> <p>Checks a set of points that have been transformed to sample points on an image against
  94. /// the image's dimensions to see if the point are even within the image.</p>
  95. ///
  96. /// <p>This method will actually "nudge" the endpoints back onto the image if they are found to be
  97. /// barely (less than 1 pixel) off the image. This accounts for imperfect detection of finder
  98. /// patterns in an image where the QR Code runs all the way to the image border.</p>
  99. ///
  100. /// <p>For efficiency, the method will check points from either end of the line until one is found
  101. /// to be within the image. Because the set of points are assumed to be linear, this is valid.</p>
  102. ///
  103. /// </summary>
  104. /// <param name="image">image into which the points should map
  105. /// </param>
  106. /// <param name="points">actual points in x1,y1,...,xn,yn form
  107. /// </param>
  108. protected internal static bool checkAndNudgePoints(BitMatrix image, float[] points)
  109. {
  110. int width = image.Width;
  111. int height = image.Height;
  112. // Check and nudge points from start until we see some that are OK:
  113. bool nudged = true;
  114. for (int offset = 0; offset < points.Length && nudged; offset += 2)
  115. {
  116. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  117. int x = (int)points[offset];
  118. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  119. int y = (int)points[offset + 1];
  120. if (x < -1 || x > width || y < -1 || y > height)
  121. {
  122. return false;
  123. }
  124. nudged = false;
  125. if (x == -1)
  126. {
  127. points[offset] = 0.0f;
  128. nudged = true;
  129. }
  130. else if (x == width)
  131. {
  132. points[offset] = width - 1;
  133. nudged = true;
  134. }
  135. if (y == -1)
  136. {
  137. points[offset + 1] = 0.0f;
  138. nudged = true;
  139. }
  140. else if (y == height)
  141. {
  142. points[offset + 1] = height - 1;
  143. nudged = true;
  144. }
  145. }
  146. // Check and nudge points from end:
  147. nudged = true;
  148. for (int offset = points.Length - 2; offset >= 0 && nudged; offset -= 2)
  149. {
  150. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  151. int x = (int)points[offset];
  152. //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
  153. int y = (int)points[offset + 1];
  154. if (x < -1 || x > width || y < -1 || y > height)
  155. {
  156. return false;
  157. }
  158. nudged = false;
  159. if (x == -1)
  160. {
  161. points[offset] = 0.0f;
  162. nudged = true;
  163. }
  164. else if (x == width)
  165. {
  166. points[offset] = width - 1;
  167. nudged = true;
  168. }
  169. if (y == -1)
  170. {
  171. points[offset + 1] = 0.0f;
  172. nudged = true;
  173. }
  174. else if (y == height)
  175. {
  176. points[offset + 1] = height - 1;
  177. nudged = true;
  178. }
  179. }
  180. return true;
  181. }
  182. }
  183. }