Binarizer.php 3.6 KB

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