1717
1818namespace Zxing ;
1919
20- use Zxing \Common \BitArray ;
2120use Zxing \Common \BitMatrix ;
2221
23-
2422/**
2523 * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
2624 * accept a BinaryBitmap and attempt to decode it.
2725 *
2826 * @author [email protected] (Daniel Switkin) 2927 */
30- final class BinaryBitmap {
31-
32- private $ binarizer ;
33- private $ matrix ;
34-
35- public function __construct ($ binarizer ) {
36- if ($ binarizer == null ) {
28+ final class BinaryBitmap
29+ {
30+ private $ binarizer ;
31+ private $ matrix ;
32+
33+ public function __construct (Binarizer $ binarizer )
34+ {
35+ if ($ binarizer === null ) {
3736 throw new \InvalidArgumentException ("Binarizer must be non-null. " );
3837 }
3938 $ this ->binarizer = $ binarizer ;
4039 }
4140
4241 /**
43- * @return The width of the bitmap.
42+ * @return int The width of the bitmap.
4443 */
45- public function getWidth () {
44+ public function getWidth ()
45+ {
4646 return $ this ->binarizer ->getWidth ();
4747 }
4848
4949 /**
50- * @return The height of the bitmap.
50+ * @return int The height of the bitmap.
5151 */
52- public function getHeight () {
52+ public function getHeight ()
53+ {
5354 return $ this ->binarizer ->getHeight ();
5455 }
5556
@@ -58,95 +59,108 @@ public function getHeight() {
5859 * cached data. Callers should assume this method is expensive and call it as seldom as possible.
5960 * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
6061 *
61- * @param y The row to fetch, which must be in [0, bitmap height)
62+ * @param y The row to fetch, which must be in [0, bitmap height)
6263 * @param row An optional preallocated array. If null or too small, it will be ignored.
6364 * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
64- * @return The array of bits for this row (true means black).
65+ *
66+ * @return array The array of bits for this row (true means black).
6567 * @throws NotFoundException if row can't be binarized
6668 */
67- public function getBlackRow ($ y , $ row ) {
69+ public function getBlackRow ($ y , $ row )
70+ {
6871 return $ this ->binarizer ->getBlackRow ($ y , $ row );
6972 }
7073
7174 /**
72- * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
73- * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
74- * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
75- * fetched using getBlackRow(), so don't mix and match between them.
76- *
77- * @return The 2D array of bits for the image (true means black).
78- * @throws NotFoundException if image can't be binarized to make a matrix
79- */
80- public function getBlackMatrix (){
81- // The matrix is created on demand the first time it is requested, then cached. There are two
82- // reasons for this:
83- // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
84- // 1D Reader finds a barcode before the 2D Readers run.
85- // 2. This work will only be done once even if the caller installs multiple 2D Readers.
86- if ($ this ->matrix == null ) {
87- $ this ->matrix = $ this ->binarizer ->getBlackMatrix ();
88- }
89- return $ this ->matrix ;
90- }
91-
92- /**
93- * @return Whether this bitmap can be cropped.
75+ * @return bool Whether this bitmap can be cropped.
9476 */
95- public function isCropSupported () {
77+ public function isCropSupported (): bool
78+ {
9679 return $ this ->binarizer ->getLuminanceSource ()->isCropSupported ();
9780 }
9881
9982 /**
10083 * Returns a new object with cropped image data. Implementations may keep a reference to the
10184 * original data rather than a copy. Only callable if isCropSupported() is true.
10285 *
103- * @param left The left coordinate, which must be in [0,getWidth())
104- * @param top The top coordinate, which must be in [0,getHeight())
105- * @param width The width of the rectangle to crop.
86+ * @param left The left coordinate, which must be in [0,getWidth())
87+ * @param top The top coordinate, which must be in [0,getHeight())
88+ * @param width The width of the rectangle to crop.
10689 * @param height The height of the rectangle to crop.
107- * @return A cropped version of this object.
90+ *
91+ * @return BinaryBitmap A cropped version of this object.
10892 */
109- public function crop ($ left , $ top , $ width , $ height ) {
93+ public function crop ($ left , $ top , $ width , $ height )
94+ {
11095 $ newSource = $ this ->binarizer ->getLuminanceSource ()->crop ($ left , $ top , $ width , $ height );
96+
11197 return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
11298 }
11399
114100 /**
115101 * @return Whether this bitmap supports counter-clockwise rotation.
116102 */
117- public function isRotateSupported () {
103+ public function isRotateSupported ()
104+ {
118105 return $ this ->binarizer ->getLuminanceSource ()->isRotateSupported ();
119106 }
120107
121108 /**
122109 * Returns a new object with rotated image data by 90 degrees counterclockwise.
123110 * Only callable if {@link #isRotateSupported()} is true.
124111 *
125- * @return A rotated version of this object.
112+ * @return BinaryBitmap A rotated version of this object.
126113 */
127- public function rotateCounterClockwise () {
114+ public function rotateCounterClockwise ()
115+ {
128116 $ newSource = $ this ->binarizer ->getLuminanceSource ()->rotateCounterClockwise ();
117+
129118 return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
130119 }
131120
132121 /**
133122 * Returns a new object with rotated image data by 45 degrees counterclockwise.
134123 * Only callable if {@link #isRotateSupported()} is true.
135124 *
136- * @return A rotated version of this object.
125+ * @return BinaryBitmap A rotated version of this object.
137126 */
138- public function rotateCounterClockwise45 () {
127+ public function rotateCounterClockwise45 ()
128+ {
139129 $ newSource = $ this ->binarizer ->getLuminanceSource ()->rotateCounterClockwise45 ();
130+
140131 return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
141132 }
142133
143- //@Override
144- public function toString () {
134+ public function toString ()
135+ {
145136 try {
146137 return $ this ->getBlackMatrix ()->toString ();
147138 } catch (NotFoundException $ e ) {
148- return "" ;
149139 }
140+
141+ return '' ;
150142 }
151143
144+ /**
145+ * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
146+ * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
147+ * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
148+ * fetched using getBlackRow(), so don't mix and match between them.
149+ *
150+ * @return BitMatrix The 2D array of bits for the image (true means black).
151+ * @throws NotFoundException if image can't be binarized to make a matrix
152+ */
153+ public function getBlackMatrix (): BitMatrix
154+ {
155+ // The matrix is created on demand the first time it is requested, then cached. There are two
156+ // reasons for this:
157+ // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
158+ // 1D Reader finds a barcode before the 2D Readers run.
159+ // 2. This work will only be done once even if the caller installs multiple 2D Readers.
160+ if ($ this ->matrix === null ) {
161+ $ this ->matrix = $ this ->binarizer ->getBlackMatrix ();
162+ }
163+
164+ return $ this ->matrix ;
165+ }
152166}
0 commit comments