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.

BinaryBitmap.cs 6.7 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2009 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. using System;
  17. using ZXing.Common;
  18. namespace ZXing
  19. {
  20. /// <summary> This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
  21. /// accept a BinaryBitmap and attempt to decode it.
  22. ///
  23. /// </summary>
  24. /// <author> dswitkin@google.com (Daniel Switkin)
  25. /// </author>
  26. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  27. /// </author>
  28. public sealed class BinaryBitmap
  29. {
  30. private Binarizer binarizer;
  31. private BitMatrix matrix;
  32. public BinaryBitmap(Binarizer binarizer)
  33. {
  34. if (binarizer == null)
  35. {
  36. throw new ArgumentException("Binarizer must be non-null.");
  37. }
  38. this.binarizer = binarizer;
  39. }
  40. /// <returns> The width of the bitmap.
  41. /// </returns>
  42. public int Width
  43. {
  44. get
  45. {
  46. return binarizer.Width;
  47. }
  48. }
  49. /// <returns> The height of the bitmap.
  50. /// </returns>
  51. public int Height
  52. {
  53. get
  54. {
  55. return binarizer.Height;
  56. }
  57. }
  58. /// <summary> Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  59. /// cached data. Callers should assume this method is expensive and call it as seldom as possible.
  60. /// This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  61. ///
  62. /// </summary>
  63. /// <param name="y">The row to fetch, 0 &lt;= y &lt; bitmap height.
  64. /// </param>
  65. /// <param name="row">An optional preallocated array. If null or too small, it will be ignored.
  66. /// If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  67. /// </param>
  68. /// <returns> The array of bits for this row (true means black).
  69. /// </returns>
  70. public BitArray getBlackRow(int y, BitArray row)
  71. {
  72. return binarizer.getBlackRow(y, row);
  73. }
  74. /// <summary> Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
  75. /// and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  76. /// may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  77. /// fetched using getBlackRow(), so don't mix and match between them.
  78. ///
  79. /// </summary>
  80. /// <returns> The 2D array of bits for the image (true means black).
  81. /// </returns>
  82. public BitMatrix BlackMatrix
  83. {
  84. get
  85. {
  86. // The matrix is created on demand the first time it is requested, then cached. There are two
  87. // reasons for this:
  88. // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
  89. // 1D Reader finds a barcode before the 2D Readers run.
  90. // 2. This work will only be done once even if the caller installs multiple 2D Readers.
  91. if (matrix == null)
  92. {
  93. matrix = binarizer.BlackMatrix;
  94. }
  95. return matrix;
  96. }
  97. }
  98. /// <returns> Whether this bitmap can be cropped.
  99. /// </returns>
  100. public bool CropSupported
  101. {
  102. get
  103. {
  104. return binarizer.LuminanceSource.CropSupported;
  105. }
  106. }
  107. /// <summary> Returns a new object with cropped image data. Implementations may keep a reference to the
  108. /// original data rather than a copy. Only callable if isCropSupported() is true.
  109. ///
  110. /// </summary>
  111. /// <param name="left">The left coordinate, 0 &lt;= left &lt; getWidth().
  112. /// </param>
  113. /// <param name="top">The top coordinate, 0 &lt;= top &lt;= getHeight().
  114. /// </param>
  115. /// <param name="width">The width of the rectangle to crop.
  116. /// </param>
  117. /// <param name="height">The height of the rectangle to crop.
  118. /// </param>
  119. /// <returns> A cropped version of this object.
  120. /// </returns>
  121. public BinaryBitmap crop(int left, int top, int width, int height)
  122. {
  123. var newSource = binarizer.LuminanceSource.crop(left, top, width, height);
  124. return new BinaryBitmap(binarizer.createBinarizer(newSource));
  125. }
  126. /// <returns> Whether this bitmap supports counter-clockwise rotation.
  127. /// </returns>
  128. public bool RotateSupported
  129. {
  130. get
  131. {
  132. return binarizer.LuminanceSource.RotateSupported;
  133. }
  134. }
  135. /// <summary>
  136. /// Returns a new object with rotated image data by 90 degrees counterclockwise.
  137. /// Only callable if {@link #isRotateSupported()} is true.
  138. /// </summary>
  139. /// <returns> A rotated version of this object.
  140. /// </returns>
  141. public BinaryBitmap rotateCounterClockwise()
  142. {
  143. var newSource = binarizer.LuminanceSource.rotateCounterClockwise();
  144. return new BinaryBitmap(binarizer.createBinarizer(newSource));
  145. }
  146. /// <summary>
  147. /// Returns a new object with rotated image data by 45 degrees counterclockwise.
  148. /// Only callable if {@link #isRotateSupported()} is true.
  149. /// </summary>
  150. /// <returns>A rotated version of this object.</returns>
  151. public BinaryBitmap rotateCounterClockwise45()
  152. {
  153. LuminanceSource newSource = binarizer.LuminanceSource.rotateCounterClockwise45();
  154. return new BinaryBitmap(binarizer.createBinarizer(newSource));
  155. }
  156. /// <summary>
  157. /// Returns a <see cref="System.String"/> that represents this instance.
  158. /// </summary>
  159. /// <returns>
  160. /// A <see cref="System.String"/> that represents this instance.
  161. /// </returns>
  162. public override string ToString()
  163. {
  164. var blackMatrix = BlackMatrix;
  165. return blackMatrix != null ? blackMatrix.ToString() : String.Empty;
  166. }
  167. }
  168. }