BinaryBitmap.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
  22. * accept a BinaryBitmap and attempt to decode it.
  23. *
  24. * @author dswitkin@google.com (Daniel Switkin)
  25. */
  26. final class BinaryBitmap {
  27. private $binarizer;
  28. private $matrix;
  29. public function __construct($binarizer) {
  30. if ($binarizer == null) {
  31. throw new \InvalidArgumentException("Binarizer must be non-null.");
  32. }
  33. $this->binarizer = $binarizer;
  34. }
  35. /**
  36. * @return The width of the bitmap.
  37. */
  38. public function getWidth() {
  39. return $this->binarizer->getWidth();
  40. }
  41. /**
  42. * @return The height of the bitmap.
  43. */
  44. public function getHeight() {
  45. return $this->binarizer->getHeight();
  46. }
  47. /**
  48. * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
  49. * cached data. Callers should assume this method is expensive and call it as seldom as possible.
  50. * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
  51. *
  52. * @param y The row to fetch, which must be in [0, bitmap height)
  53. * @param row An optional preallocated array. If null or too small, it will be ignored.
  54. * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
  55. * @return The array of bits for this row (true means black).
  56. * @throws NotFoundException if row can't be binarized
  57. */
  58. public function getBlackRow($y, $row) {
  59. return $this->binarizer->getBlackRow($y, $row);
  60. }
  61. /**
  62. * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
  63. * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
  64. * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
  65. * fetched using getBlackRow(), so don't mix and match between them.
  66. *
  67. * @return The 2D array of bits for the image (true means black).
  68. * @throws NotFoundException if image can't be binarized to make a matrix
  69. */
  70. public function getBlackMatrix(){
  71. // The matrix is created on demand the first time it is requested, then cached. There are two
  72. // reasons for this:
  73. // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
  74. // 1D Reader finds a barcode before the 2D Readers run.
  75. // 2. This work will only be done once even if the caller installs multiple 2D Readers.
  76. if ($this->matrix == null) {
  77. $this->matrix = $this->binarizer->getBlackMatrix();
  78. }
  79. return $this->matrix;
  80. }
  81. /**
  82. * @return Whether this bitmap can be cropped.
  83. */
  84. public function isCropSupported() {
  85. return $this->binarizer->getLuminanceSource()->isCropSupported();
  86. }
  87. /**
  88. * Returns a new object with cropped image data. Implementations may keep a reference to the
  89. * original data rather than a copy. Only callable if isCropSupported() is true.
  90. *
  91. * @param left The left coordinate, which must be in [0,getWidth())
  92. * @param top The top coordinate, which must be in [0,getHeight())
  93. * @param width The width of the rectangle to crop.
  94. * @param height The height of the rectangle to crop.
  95. * @return A cropped version of this object.
  96. */
  97. public function crop($left, $top, $width, $height) {
  98. $newSource = $this->binarizer->getLuminanceSource()->crop($left, $top, $width, $height);
  99. return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
  100. }
  101. /**
  102. * @return Whether this bitmap supports counter-clockwise rotation.
  103. */
  104. public function isRotateSupported() {
  105. return $this->binarizer->getLuminanceSource()->isRotateSupported();
  106. }
  107. /**
  108. * Returns a new object with rotated image data by 90 degrees counterclockwise.
  109. * Only callable if {@link #isRotateSupported()} is true.
  110. *
  111. * @return A rotated version of this object.
  112. */
  113. public function rotateCounterClockwise() {
  114. $newSource = $this->binarizer->getLuminanceSource()->rotateCounterClockwise();
  115. return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
  116. }
  117. /**
  118. * Returns a new object with rotated image data by 45 degrees counterclockwise.
  119. * Only callable if {@link #isRotateSupported()} is true.
  120. *
  121. * @return A rotated version of this object.
  122. */
  123. public function rotateCounterClockwise45() {
  124. $newSource = $this->binarizer->getLuminanceSource()->rotateCounterClockwise45();
  125. return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
  126. }
  127. //@Override
  128. public function toString() {
  129. try {
  130. return $this->getBlackMatrix()->toString();
  131. } catch (NotFoundException $e) {
  132. return "";
  133. }
  134. }
  135. }