17
17
18
18
namespace Zxing ;
19
19
20
- use Zxing \Common \BitArray ;
21
20
use Zxing \Common \BitMatrix ;
22
21
23
-
24
22
/**
25
23
* This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
26
24
* accept a BinaryBitmap and attempt to decode it.
27
25
*
28
26
* @author [email protected] (Daniel Switkin)
29
27
*/
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 ) {
37
36
throw new \InvalidArgumentException ("Binarizer must be non-null. " );
38
37
}
39
38
$ this ->binarizer = $ binarizer ;
40
39
}
41
40
42
41
/**
43
- * @return The width of the bitmap.
42
+ * @return int The width of the bitmap.
44
43
*/
45
- public function getWidth () {
44
+ public function getWidth ()
45
+ {
46
46
return $ this ->binarizer ->getWidth ();
47
47
}
48
48
49
49
/**
50
- * @return The height of the bitmap.
50
+ * @return int The height of the bitmap.
51
51
*/
52
- public function getHeight () {
52
+ public function getHeight ()
53
+ {
53
54
return $ this ->binarizer ->getHeight ();
54
55
}
55
56
@@ -58,95 +59,108 @@ public function getHeight() {
58
59
* cached data. Callers should assume this method is expensive and call it as seldom as possible.
59
60
* This method is intended for decoding 1D barcodes and may choose to apply sharpening.
60
61
*
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)
62
63
* @param row An optional preallocated array. If null or too small, it will be ignored.
63
64
* 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).
65
67
* @throws NotFoundException if row can't be binarized
66
68
*/
67
- public function getBlackRow ($ y , $ row ) {
69
+ public function getBlackRow ($ y , $ row )
70
+ {
68
71
return $ this ->binarizer ->getBlackRow ($ y , $ row );
69
72
}
70
73
71
74
/**
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.
94
76
*/
95
- public function isCropSupported () {
77
+ public function isCropSupported (): bool
78
+ {
96
79
return $ this ->binarizer ->getLuminanceSource ()->isCropSupported ();
97
80
}
98
81
99
82
/**
100
83
* Returns a new object with cropped image data. Implementations may keep a reference to the
101
84
* original data rather than a copy. Only callable if isCropSupported() is true.
102
85
*
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.
106
89
* @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.
108
92
*/
109
- public function crop ($ left , $ top , $ width , $ height ) {
93
+ public function crop ($ left , $ top , $ width , $ height )
94
+ {
110
95
$ newSource = $ this ->binarizer ->getLuminanceSource ()->crop ($ left , $ top , $ width , $ height );
96
+
111
97
return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
112
98
}
113
99
114
100
/**
115
101
* @return Whether this bitmap supports counter-clockwise rotation.
116
102
*/
117
- public function isRotateSupported () {
103
+ public function isRotateSupported ()
104
+ {
118
105
return $ this ->binarizer ->getLuminanceSource ()->isRotateSupported ();
119
106
}
120
107
121
108
/**
122
109
* Returns a new object with rotated image data by 90 degrees counterclockwise.
123
110
* Only callable if {@link #isRotateSupported()} is true.
124
111
*
125
- * @return A rotated version of this object.
112
+ * @return BinaryBitmap A rotated version of this object.
126
113
*/
127
- public function rotateCounterClockwise () {
114
+ public function rotateCounterClockwise ()
115
+ {
128
116
$ newSource = $ this ->binarizer ->getLuminanceSource ()->rotateCounterClockwise ();
117
+
129
118
return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
130
119
}
131
120
132
121
/**
133
122
* Returns a new object with rotated image data by 45 degrees counterclockwise.
134
123
* Only callable if {@link #isRotateSupported()} is true.
135
124
*
136
- * @return A rotated version of this object.
125
+ * @return BinaryBitmap A rotated version of this object.
137
126
*/
138
- public function rotateCounterClockwise45 () {
127
+ public function rotateCounterClockwise45 ()
128
+ {
139
129
$ newSource = $ this ->binarizer ->getLuminanceSource ()->rotateCounterClockwise45 ();
130
+
140
131
return new BinaryBitmap ($ this ->binarizer ->createBinarizer ($ newSource ));
141
132
}
142
133
143
- //@Override
144
- public function toString () {
134
+ public function toString ()
135
+ {
145
136
try {
146
137
return $ this ->getBlackMatrix ()->toString ();
147
138
} catch (NotFoundException $ e ) {
148
- return "" ;
149
139
}
140
+
141
+ return '' ;
150
142
}
151
143
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
+ }
152
166
}
0 commit comments