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