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.

Binarizer.cs 4.4 kB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 hierarchy provides a set of methods to convert luminance data to 1 bit data.
  21. /// It allows the algorithm to vary polymorphically, for example allowing a very expensive
  22. /// thresholding technique for servers and a fast one for mobile. It also permits the implementation
  23. /// to vary, e.g. a JNI version for Android and a Java fallback version for other platforms.
  24. ///
  25. /// <author>dswitkin@google.com (Daniel Switkin)</author>
  26. /// </summary>
  27. public abstract class Binarizer
  28. {
  29. private readonly LuminanceSource source;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="Binarizer"/> class.
  32. /// </summary>
  33. /// <param name="source">The source.</param>
  34. protected internal Binarizer(LuminanceSource source)
  35. {
  36. if (source == null)
  37. {
  38. throw new ArgumentException("Source must be non-null.");
  39. }
  40. this.source = source;
  41. }
  42. /// <summary>
  43. /// Gets the luminance source object.
  44. /// </summary>
  45. virtual public LuminanceSource LuminanceSource
  46. {
  47. get
  48. {
  49. return source;
  50. }
  51. }
  52. /// <summary> Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  53. /// cached data. Callers should assume this method is expensive and call it as seldom as possible.
  54. /// This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  55. /// For callers which only examine one row of pixels at a time, the same BitArray should be reused
  56. /// and passed in with each call for performance. However it is legal to keep more than one row
  57. /// at a time if needed.
  58. /// </summary>
  59. /// <param name="y">The row to fetch, 0 &lt;= y &lt; bitmap height.</param>
  60. /// <param name="row">An optional preallocated array. If null or too small, it will be ignored.
  61. /// If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  62. /// </param>
  63. /// <returns> The array of bits for this row (true means black).</returns>
  64. public abstract BitArray getBlackRow(int y, BitArray row);
  65. /// <summary> Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive
  66. /// and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  67. /// may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  68. /// fetched using getBlackRow(), so don't mix and match between them.
  69. /// </summary>
  70. /// <returns> The 2D array of bits for the image (true means black).</returns>
  71. public abstract BitMatrix BlackMatrix { get; }
  72. /// <summary> Creates a new object with the same type as this Binarizer implementation, but with pristine
  73. /// state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache
  74. /// of 1 bit data. See Effective Java for why we can't use Java's clone() method.
  75. /// </summary>
  76. /// <param name="source">The LuminanceSource this Binarizer will operate on.</param>
  77. /// <returns> A new concrete Binarizer implementation object.</returns>
  78. public abstract Binarizer createBinarizer(LuminanceSource source);
  79. /// <summary>
  80. /// Gets the width of the luminance source object.
  81. /// </summary>
  82. public int Width
  83. {
  84. get { return source.Width; }
  85. }
  86. /// <summary>
  87. /// Gets the height of the luminance source object.
  88. /// </summary>
  89. public int Height
  90. {
  91. get { return source.Height; }
  92. }
  93. }
  94. }