Binarizer.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. private $source;
  30. protected function __construct($source) {
  31. $this->source = $source;
  32. }
  33. public final function getLuminanceSource() {
  34. return $this->source;
  35. }
  36. /**
  37. * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  38. * cached data. Callers should assume this method is expensive and call it as seldom as possible.
  39. * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  40. * For callers which only examine one row of pixels at a time, the same BitArray should be reused
  41. * and passed in with each call for performance. However it is legal to keep more than one row
  42. * at a time if needed.
  43. *
  44. * @param y The row to fetch, which must be in [0, bitmap height)
  45. * @param row An optional preallocated array. If null or too small, it will be ignored.
  46. * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  47. * @return The array of bits for this row (true means black).
  48. * @throws NotFoundException if row can't be binarized
  49. */
  50. public abstract function getBlackRow($y, $row);
  51. /**
  52. * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive
  53. * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  54. * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  55. * fetched using getBlackRow(), so don't mix and match between them.
  56. *
  57. * @return The 2D array of bits for the image (true means black).
  58. * @throws NotFoundException if image can't be binarized to make a matrix
  59. */
  60. public abstract function getBlackMatrix();
  61. /**
  62. * Creates a new object with the same type as this Binarizer implementation, but with pristine
  63. * state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache
  64. * of 1 bit data. See Effective Java for why we can't use Java's clone() method.
  65. *
  66. * @param source The LuminanceSource this Binarizer will operate on.
  67. * @return A new concrete Binarizer implementation object.
  68. */
  69. public abstract function createBinarizer($source);
  70. public final function getWidth() {
  71. return $this->source->getWidth();
  72. }
  73. public final function getHeight() {
  74. return $this->source->getHeight();
  75. }
  76. }