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.

LuminanceSource.cs 6.8 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 System.Text;
  18. namespace ZXing
  19. {
  20. /// <summary> The purpose of this class hierarchy is to abstract different bitmap implementations across
  21. /// platforms into a standard interface for requesting greyscale luminance values. The interface
  22. /// only provides immutable methods; therefore crop and rotation create copies. This is to ensure
  23. /// that one Reader does not modify the original luminance source and leave it in an unknown state
  24. /// for other Readers in the chain.
  25. /// </summary>
  26. /// <author> dswitkin@google.com (Daniel Switkin)
  27. /// </author>
  28. /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
  29. /// </author>
  30. public abstract class LuminanceSource
  31. {
  32. private int width;
  33. private int height;
  34. protected LuminanceSource(int width, int height)
  35. {
  36. this.width = width;
  37. this.height = height;
  38. }
  39. /// <summary> Fetches one row of luminance data from the underlying platform's bitmap. Values range from
  40. /// 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have
  41. /// to bitwise and with 0xff for each value. It is preferable for implementations of this method
  42. /// to only fetch this row rather than the whole image, since no 2D Readers may be installed and
  43. /// getMatrix() may never be called.
  44. ///
  45. /// </summary>
  46. /// <param name="y">The row to fetch, 0 &lt;= y &lt; Height.
  47. /// </param>
  48. /// <param name="row">An optional preallocated array. If null or too small, it will be ignored.
  49. /// Always use the returned object, and ignore the .length of the array.
  50. /// </param>
  51. /// <returns> An array containing the luminance data.
  52. /// </returns>
  53. public abstract byte[] getRow(int y, byte[] row);
  54. /// <summary> Fetches luminance data for the underlying bitmap. Values should be fetched using:
  55. /// int luminance = array[y * width + x] &amp; 0xff;
  56. ///
  57. /// </summary>
  58. /// <returns> A row-major 2D array of luminance values. Do not use result.length as it may be
  59. /// larger than width * height bytes on some platforms. Do not modify the contents
  60. /// of the result.
  61. /// </returns>
  62. public abstract byte[] Matrix { get; }
  63. /// <returns> The width of the bitmap.</returns>
  64. virtual public int Width
  65. {
  66. get
  67. {
  68. return width;
  69. }
  70. protected set
  71. {
  72. width = value;
  73. }
  74. }
  75. /// <returns> The height of the bitmap.</returns>
  76. virtual public int Height
  77. {
  78. get
  79. {
  80. return height;
  81. }
  82. protected set
  83. {
  84. height = value;
  85. }
  86. }
  87. /// <returns> Whether this subclass supports cropping.</returns>
  88. virtual public bool CropSupported
  89. {
  90. get
  91. {
  92. return false;
  93. }
  94. }
  95. /// <summary> Returns a new object with cropped image data. Implementations may keep a reference to the
  96. /// original data rather than a copy. Only callable if CropSupported is true.
  97. ///
  98. /// </summary>
  99. /// <param name="left">The left coordinate, 0 &lt;= left &lt; Width.
  100. /// </param>
  101. /// <param name="top">The top coordinate, 0 &lt;= top &lt;= Height.
  102. /// </param>
  103. /// <param name="width">The width of the rectangle to crop.
  104. /// </param>
  105. /// <param name="height">The height of the rectangle to crop.
  106. /// </param>
  107. /// <returns> A cropped version of this object.
  108. /// </returns>
  109. public virtual LuminanceSource crop(int left, int top, int width, int height)
  110. {
  111. throw new NotSupportedException("This luminance source does not support cropping.");
  112. }
  113. /// <returns> Whether this subclass supports counter-clockwise rotation.</returns>
  114. virtual public bool RotateSupported
  115. {
  116. get
  117. {
  118. return false;
  119. }
  120. }
  121. /// <summary>
  122. /// Returns a new object with rotated image data by 90 degrees counterclockwise.
  123. /// Only callable if {@link #isRotateSupported()} is true.
  124. /// </summary>
  125. /// <returns> A rotated version of this object.
  126. /// </returns>
  127. public virtual LuminanceSource rotateCounterClockwise()
  128. {
  129. throw new NotSupportedException("This luminance source does not support rotation.");
  130. }
  131. /// <summary>
  132. /// Returns a new object with rotated image data by 45 degrees counterclockwise.
  133. /// Only callable if {@link #isRotateSupported()} is true.
  134. /// </summary>
  135. /// <returns>A rotated version of this object.</returns>
  136. public virtual LuminanceSource rotateCounterClockwise45()
  137. {
  138. throw new NotSupportedException("This luminance source does not support rotation by 45 degrees.");
  139. }
  140. /// <summary>
  141. /// </summary>
  142. /// <returns>Whether this subclass supports invertion.</returns>
  143. virtual public bool InversionSupported
  144. {
  145. get
  146. {
  147. return false;
  148. }
  149. }
  150. override public String ToString()
  151. {
  152. var row = new byte[width];
  153. var result = new StringBuilder(height * (width + 1));
  154. for (int y = 0; y < height; y++)
  155. {
  156. row = getRow(y, row);
  157. for (int x = 0; x < width; x++)
  158. {
  159. int luminance = row[x] & 0xFF;
  160. char c;
  161. if (luminance < 0x40)
  162. {
  163. c = '#';
  164. }
  165. else if (luminance < 0x80)
  166. {
  167. c = '+';
  168. }
  169. else if (luminance < 0xC0)
  170. {
  171. c = '.';
  172. }
  173. else
  174. {
  175. c = ' ';
  176. }
  177. result.Append(c);
  178. }
  179. result.Append('\n');
  180. }
  181. return result.ToString();
  182. }
  183. }
  184. }