2727 *
2828 * @author [email protected] (Daniel Switkin) 2929 */
30- final class BinaryBitmap {
30+ final class BinaryBitmap
31+ {
3132
32- private $ binarizer ;
33- private $ matrix ;
33+ private $ binarizer ;
34+ private $ matrix ;
3435
35- public function __construct ($ binarizer ) {
36+ public function __construct ($ binarizer )
37+ {
3638 if ($ binarizer == null ) {
3739 throw new \InvalidArgumentException ("Binarizer must be non-null. " );
3840 }
@@ -42,14 +44,16 @@ public function __construct($binarizer) {
4244 /**
4345 * @return The width of the bitmap.
4446 */
45- public function getWidth () {
47+ public function getWidth ()
48+ {
4649 return $ this ->binarizer ->getWidth ();
4750 }
4851
4952 /**
5053 * @return The height of the bitmap.
5154 */
52- public function getHeight () {
55+ public function getHeight ()
56+ {
5357 return $ this ->binarizer ->getHeight ();
5458 }
5559
@@ -58,63 +62,49 @@ public function getHeight() {
5862 * cached data. Callers should assume this method is expensive and call it as seldom as possible.
5963 * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
6064 *
61- * @param y The row to fetch, which must be in [0, bitmap height)
65+ * @param y The row to fetch, which must be in [0, bitmap height)
6266 * @param row An optional preallocated array. If null or too small, it will be ignored.
6367 * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
68+ *
6469 * @return The array of bits for this row (true means black).
6570 * @throws NotFoundException if row can't be binarized
6671 */
67- public function getBlackRow ($ y , $ row ) {
72+ public function getBlackRow ($ y , $ row )
73+ {
6874 return $ this ->binarizer ->getBlackRow ($ y , $ row );
6975 }
7076
71- /**
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-
9277 /**
9378 * @return Whether this bitmap can be cropped.
9479 */
95- public function isCropSupported () {
80+ public function isCropSupported ()
81+ {
9682 return $ this ->binarizer ->getLuminanceSource ()->isCropSupported ();
9783 }
9884
9985 /**
10086 * Returns a new object with cropped image data. Implementations may keep a reference to the
10187 * original data rather than a copy. Only callable if isCropSupported() is true.
10288 *
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.
89+ * @param left The left coordinate, which must be in [0,getWidth())
90+ * @param top The top coordinate, which must be in [0,getHeight())
91+ * @param width The width of the rectangle to crop.
10692 * @param height The height of the rectangle to crop.
93+ *
10794 * @return A cropped version of this object.
10895 */
109- public function crop ($ left , $ top , $ width , $ height ) {
96+ public function crop ($ left , $ top , $ width , $ height )
97+ {
11098 $ newSource = $ this ->binarizer ->getLuminanceSource ()->crop ($ left , $ top , $ width , $ height );
99+
111100 return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
112101 }
113102
114103 /**
115104 * @return Whether this bitmap supports counter-clockwise rotation.
116105 */
117- public function isRotateSupported () {
106+ public function isRotateSupported ()
107+ {
118108 return $ this ->binarizer ->getLuminanceSource ()->isRotateSupported ();
119109 }
120110
@@ -124,8 +114,10 @@ public function isRotateSupported() {
124114 *
125115 * @return A rotated version of this object.
126116 */
127- public function rotateCounterClockwise () {
117+ public function rotateCounterClockwise ()
118+ {
128119 $ newSource = $ this ->binarizer ->getLuminanceSource ()->rotateCounterClockwise ();
120+
129121 return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
130122 }
131123
@@ -135,18 +127,44 @@ public function rotateCounterClockwise() {
135127 *
136128 * @return A rotated version of this object.
137129 */
138- public function rotateCounterClockwise45 () {
130+ public function rotateCounterClockwise45 ()
131+ {
139132 $ newSource = $ this ->binarizer ->getLuminanceSource ()->rotateCounterClockwise45 ();
133+
140134 return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
141135 }
142136
143- //@Override
144- public function toString () {
137+ public function toString ()
138+ {
145139 try {
146140 return $ this ->getBlackMatrix ()->toString ();
147141 } catch (NotFoundException $ e ) {
148142 return "" ;
149143 }
150144 }
151145
146+ //@Override
147+
148+ /**
149+ * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
150+ * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
151+ * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
152+ * fetched using getBlackRow(), so don't mix and match between them.
153+ *
154+ * @return The 2D array of bits for the image (true means black).
155+ * @throws NotFoundException if image can't be binarized to make a matrix
156+ */
157+ public function getBlackMatrix ()
158+ {
159+ // The matrix is created on demand the first time it is requested, then cached. There are two
160+ // reasons for this:
161+ // 1. This work will never be done if the caller only installs 1D Reader objects, or if a
162+ // 1D Reader finds a barcode before the 2D Readers run.
163+ // 2. This work will only be done once even if the caller installs multiple 2D Readers.
164+ if ($ this ->matrix == null ) {
165+ $ this ->matrix = $ this ->binarizer ->getBlackMatrix ();
166+ }
167+
168+ return $ this ->matrix ;
169+ }
152170}
0 commit comments