1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Detector |
4
|
|
|
* |
5
|
|
|
* @created 17.01.2021 |
6
|
|
|
* @author ZXing Authors |
7
|
|
|
* @author Smiley <[email protected]> |
8
|
|
|
* @copyright 2021 Smiley |
9
|
|
|
* @license Apache-2.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace chillerlan\QRCode\Detector; |
13
|
|
|
|
14
|
|
|
use chillerlan\QRCode\Decoder\{Binarizer, LuminanceSourceInterface}; |
15
|
|
|
use chillerlan\QRCode\Common\Version; |
16
|
|
|
use chillerlan\QRCode\Decoder\BitMatrix; |
17
|
|
|
use function abs, is_nan, max, min, round; |
18
|
|
|
use const NAN; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Encapsulates logic that can detect a QR Code in an image, even if the QR Code |
22
|
|
|
* is rotated or skewed, or partially obscured. |
23
|
|
|
* |
24
|
|
|
* @author Sean Owen |
25
|
|
|
*/ |
26
|
|
|
final class Detector{ |
27
|
|
|
|
28
|
|
|
private BitMatrix $matrix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Detector constructor. |
32
|
|
|
*/ |
33
|
|
|
public function __construct(LuminanceSourceInterface $source){ |
34
|
|
|
$this->matrix = (new Binarizer($source))->getBlackMatrix(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Detects a QR Code in an image. |
39
|
|
|
*/ |
40
|
|
|
public function detect():BitMatrix{ |
41
|
|
|
[$bottomLeft, $topLeft, $topRight] = (new FinderPatternFinder($this->matrix))->find(); |
42
|
|
|
|
43
|
|
|
$moduleSize = $this->calculateModuleSize($topLeft, $topRight, $bottomLeft); |
44
|
|
|
$dimension = $this->computeDimension($topLeft, $topRight, $bottomLeft, $moduleSize); |
45
|
|
|
$provisionalVersion = new Version((int)(($dimension - 17) / 4)); |
46
|
|
|
$alignmentPattern = null; |
47
|
|
|
|
48
|
|
|
// Anything above version 1 has an alignment pattern |
49
|
|
|
if(!empty($provisionalVersion->getAlignmentPattern())){ |
50
|
|
|
// Guess where a "bottom right" finder pattern would have been |
51
|
|
|
$bottomRightX = ($topRight->getX() - $topLeft->getX() + $bottomLeft->getX()); |
52
|
|
|
$bottomRightY = ($topRight->getY() - $topLeft->getY() + $bottomLeft->getY()); |
53
|
|
|
|
54
|
|
|
// Estimate that alignment pattern is closer by 3 modules |
55
|
|
|
// from "bottom right" to known top left location |
56
|
|
|
$correctionToTopLeft = (1.0 - 3.0 / (float)($provisionalVersion->getDimension() - 7)); |
57
|
|
|
$estAlignmentX = (int)($topLeft->getX() + $correctionToTopLeft * ($bottomRightX - $topLeft->getX())); |
58
|
|
|
$estAlignmentY = (int)($topLeft->getY() + $correctionToTopLeft * ($bottomRightY - $topLeft->getY())); |
59
|
|
|
|
60
|
|
|
// Kind of arbitrary -- expand search radius before giving up |
61
|
|
|
for($i = 4; $i <= 16; $i <<= 1){//?????????? |
62
|
|
|
$alignmentPattern = $this->findAlignmentInRegion($moduleSize, $estAlignmentX, $estAlignmentY, (float)$i); |
63
|
|
|
|
64
|
|
|
if($alignmentPattern !== null){ |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
// If we didn't find alignment pattern... well try anyway without it |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$transform = $this->createTransform($topLeft, $topRight, $bottomLeft, $dimension, $alignmentPattern); |
72
|
|
|
|
73
|
|
|
return (new GridSampler)->sampleGrid($this->matrix, $dimension, $transform); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Computes an average estimated module size based on estimated derived from the positions |
78
|
|
|
* of the three finder patterns. |
79
|
|
|
* |
80
|
|
|
* @throws \chillerlan\QRCode\Detector\QRCodeDetectorException |
81
|
|
|
*/ |
82
|
|
|
private function calculateModuleSize(FinderPattern $topLeft, FinderPattern $topRight, FinderPattern $bottomLeft):float{ |
83
|
|
|
// Take the average |
84
|
|
|
$moduleSize = (( |
85
|
|
|
$this->calculateModuleSizeOneWay($topLeft, $topRight) + |
86
|
|
|
$this->calculateModuleSizeOneWay($topLeft, $bottomLeft) |
87
|
|
|
) / 2.0); |
88
|
|
|
|
89
|
|
|
if($moduleSize < 1.0){ |
90
|
|
|
throw new QRCodeDetectorException('module size < 1.0'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $moduleSize; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Estimates module size based on two finder patterns -- it uses |
98
|
|
|
* #sizeOfBlackWhiteBlackRunBothWays(int, int, int, int) to figure the |
99
|
|
|
* width of each, measuring along the axis between their centers. |
100
|
|
|
*/ |
101
|
|
|
private function calculateModuleSizeOneWay(FinderPattern $a, FinderPattern $b):float{ |
102
|
|
|
|
103
|
|
|
$moduleSizeEst1 = $this->sizeOfBlackWhiteBlackRunBothWays($a->getX(), $a->getY(), $b->getX(), $b->getY()); |
104
|
|
|
$moduleSizeEst2 = $this->sizeOfBlackWhiteBlackRunBothWays($b->getX(), $b->getY(), $a->getX(), $a->getY()); |
105
|
|
|
|
106
|
|
|
if(is_nan($moduleSizeEst1)){ |
107
|
|
|
return ($moduleSizeEst2 / 7.0); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if(is_nan($moduleSizeEst2)){ |
111
|
|
|
return ($moduleSizeEst1 / 7.0); |
112
|
|
|
} |
113
|
|
|
// Average them, and divide by 7 since we've counted the width of 3 black modules, |
114
|
|
|
// and 1 white and 1 black module on either side. Ergo, divide sum by 14. |
115
|
|
|
return (($moduleSizeEst1 + $moduleSizeEst2) / 14.0); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* See #sizeOfBlackWhiteBlackRun(int, int, int, int); computes the total width of |
120
|
|
|
* a finder pattern by looking for a black-white-black run from the center in the direction |
121
|
|
|
* of another po$(another finder pattern center), and in the opposite direction too. |
122
|
|
|
* |
123
|
|
|
* @noinspection DuplicatedCode |
124
|
|
|
*/ |
125
|
|
|
private function sizeOfBlackWhiteBlackRunBothWays(float $fromX, float $fromY, float $toX, float $toY):float{ |
126
|
|
|
$result = $this->sizeOfBlackWhiteBlackRun((int)$fromX, (int)$fromY, (int)$toX, (int)$toY); |
127
|
|
|
$dimension = $this->matrix->getSize(); |
128
|
|
|
// Now count other way -- don't run off image though of course |
129
|
|
|
$scale = 1.0; |
130
|
|
|
$otherToX = ($fromX - ($toX - $fromX)); |
131
|
|
|
|
132
|
|
|
if($otherToX < 0){ |
133
|
|
|
$scale = ($fromX / ($fromX - $otherToX)); |
134
|
|
|
$otherToX = 0; |
135
|
|
|
} |
136
|
|
|
elseif($otherToX >= $dimension){ |
137
|
|
|
$scale = (($dimension - 1 - $fromX) / ($otherToX - $fromX)); |
138
|
|
|
$otherToX = ($dimension - 1); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$otherToY = (int)($fromY - ($toY - $fromY) * $scale); |
142
|
|
|
$scale = 1.0; |
143
|
|
|
|
144
|
|
|
if($otherToY < 0){ |
145
|
|
|
$scale = ($fromY / ($fromY - $otherToY)); |
146
|
|
|
$otherToY = 0; |
147
|
|
|
} |
148
|
|
|
elseif($otherToY >= $dimension){ |
149
|
|
|
$scale = (($dimension - 1 - $fromY) / ($otherToY - $fromY)); |
150
|
|
|
$otherToY = ($dimension - 1); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$otherToX = (int)($fromX + ($otherToX - $fromX) * $scale); |
154
|
|
|
$result += $this->sizeOfBlackWhiteBlackRun((int)$fromX, (int)$fromY, $otherToX, $otherToY); |
155
|
|
|
|
156
|
|
|
// Middle pixel is double-counted this way; subtract 1 |
157
|
|
|
return ($result - 1.0); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* This method traces a line from a po$in the image, in the direction towards another point. |
162
|
|
|
* It begins in a black region, and keeps going until it finds white, then black, then white again. |
163
|
|
|
* It reports the distance from the start to this point. |
164
|
|
|
* |
165
|
|
|
* This is used when figuring out how wide a finder pattern is, when the finder pattern |
166
|
|
|
* may be skewed or rotated. |
167
|
|
|
*/ |
168
|
|
|
private function sizeOfBlackWhiteBlackRun(int $fromX, int $fromY, int $toX, int $toY):float{ |
169
|
|
|
// Mild variant of Bresenham's algorithm; |
170
|
|
|
// @see https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm |
171
|
|
|
$steep = abs($toY - $fromY) > abs($toX - $fromX); |
172
|
|
|
|
173
|
|
|
if($steep){ |
174
|
|
|
$temp = $fromX; |
175
|
|
|
$fromX = $fromY; |
176
|
|
|
$fromY = $temp; |
177
|
|
|
$temp = $toX; |
178
|
|
|
$toX = $toY; |
179
|
|
|
$toY = $temp; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$dx = abs($toX - $fromX); |
183
|
|
|
$dy = abs($toY - $fromY); |
184
|
|
|
$error = (-$dx / 2); |
185
|
|
|
$xstep = (($fromX < $toX) ? 1 : -1); |
186
|
|
|
$ystep = (($fromY < $toY) ? 1 : -1); |
187
|
|
|
|
188
|
|
|
// In black pixels, looking for white, first or second time. |
189
|
|
|
$state = 0; |
190
|
|
|
// Loop up until x == toX, but not beyond |
191
|
|
|
$xLimit = ($toX + $xstep); |
192
|
|
|
|
193
|
|
|
for($x = $fromX, $y = $fromY; $x !== $xLimit; $x += $xstep){ |
194
|
|
|
$realX = ($steep) ? $y : $x; |
195
|
|
|
$realY = ($steep) ? $x : $y; |
196
|
|
|
|
197
|
|
|
// Does current pixel mean we have moved white to black or vice versa? |
198
|
|
|
// Scanning black in state 0,2 and white in state 1, so if we find the wrong |
199
|
|
|
// color, advance to next state or end if we are in state 2 already |
200
|
|
|
if(($state === 1) === $this->matrix->check($realX, $realY)){ |
201
|
|
|
|
202
|
|
|
if($state === 2){ |
203
|
|
|
return FinderPattern::distance($x, $y, $fromX, $fromY); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$state++; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$error += $dy; |
210
|
|
|
|
211
|
|
|
if($error > 0){ |
212
|
|
|
|
213
|
|
|
if($y === $toY){ |
214
|
|
|
break; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$y += $ystep; |
218
|
|
|
$error -= $dx; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Found black-white-black; give the benefit of the doubt that the next pixel outside the image |
223
|
|
|
// is "white" so this last po$at (toX+xStep,toY) is the right ending. This is really a |
224
|
|
|
// small approximation; (toX+xStep,toY+yStep) might be really correct. Ignore this. |
225
|
|
|
if($state === 2){ |
226
|
|
|
return FinderPattern::distance(($toX + $xstep), $toY, $fromX, $fromY); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// else we didn't find even black-white-black; no estimate is really possible |
230
|
|
|
return NAN; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Computes the dimension (number of modules on a size) of the QR Code based on the position |
235
|
|
|
* of the finder patterns and estimated module size. |
236
|
|
|
* |
237
|
|
|
* @throws \chillerlan\QRCode\Detector\QRCodeDetectorException |
238
|
|
|
*/ |
239
|
|
|
private function computeDimension(FinderPattern $nw, FinderPattern $ne, FinderPattern $sw, float $size):int{ |
240
|
|
|
$tltrCentersDimension = (int)round($nw->getDistance($ne) / $size); |
241
|
|
|
$tlblCentersDimension = (int)round($nw->getDistance($sw) / $size); |
242
|
|
|
$dimension = (int)((($tltrCentersDimension + $tlblCentersDimension) / 2) + 7); |
243
|
|
|
|
244
|
|
|
switch($dimension % 4){ |
245
|
|
|
case 0: |
246
|
|
|
$dimension++; |
247
|
|
|
break; |
248
|
|
|
// 1? do nothing |
249
|
|
|
case 2: |
250
|
|
|
$dimension--; |
251
|
|
|
break; |
252
|
|
|
case 3: |
253
|
|
|
throw new QRCodeDetectorException('estimated dimension: '.$dimension); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if(($dimension % 4) !== 1){ |
257
|
|
|
throw new QRCodeDetectorException('dimension mod 4 is not 1'); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $dimension; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Attempts to locate an alignment pattern in a limited region of the image, which is |
265
|
|
|
* guessed to contain it. |
266
|
|
|
* |
267
|
|
|
* @param float $overallEstModuleSize estimated module size so far |
268
|
|
|
* @param int $estAlignmentX x coordinate of center of area probably containing alignment pattern |
269
|
|
|
* @param int $estAlignmentY y coordinate of above |
270
|
|
|
* @param float $allowanceFactor number of pixels in all directions to search from the center |
271
|
|
|
* |
272
|
|
|
* @return \chillerlan\QRCode\Detector\AlignmentPattern|null if found, or null otherwise |
273
|
|
|
*/ |
274
|
|
|
private function findAlignmentInRegion( |
275
|
|
|
float $overallEstModuleSize, |
276
|
|
|
int $estAlignmentX, |
277
|
|
|
int $estAlignmentY, |
278
|
|
|
float $allowanceFactor |
279
|
|
|
):?AlignmentPattern{ |
280
|
|
|
// Look for an alignment pattern (3 modules in size) around where it should be |
281
|
|
|
$dimension = $this->matrix->getSize(); |
282
|
|
|
$allowance = (int)($allowanceFactor * $overallEstModuleSize); |
283
|
|
|
$alignmentAreaLeftX = max(0, ($estAlignmentX - $allowance)); |
284
|
|
|
$alignmentAreaRightX = min(($dimension - 1), ($estAlignmentX + $allowance)); |
285
|
|
|
|
286
|
|
|
if(($alignmentAreaRightX - $alignmentAreaLeftX) < ($overallEstModuleSize * 3)){ |
287
|
|
|
return null; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$alignmentAreaTopY = max(0, ($estAlignmentY - $allowance)); |
291
|
|
|
$alignmentAreaBottomY = min(($dimension - 1), ($estAlignmentY + $allowance)); |
292
|
|
|
|
293
|
|
|
if(($alignmentAreaBottomY - $alignmentAreaTopY) < ($overallEstModuleSize * 3)){ |
294
|
|
|
return null; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return (new AlignmentPatternFinder($this->matrix, $overallEstModuleSize))->find( |
298
|
|
|
$alignmentAreaLeftX, |
299
|
|
|
$alignmentAreaTopY, |
300
|
|
|
($alignmentAreaRightX - $alignmentAreaLeftX), |
301
|
|
|
($alignmentAreaBottomY - $alignmentAreaTopY), |
302
|
|
|
); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* |
307
|
|
|
*/ |
308
|
|
|
private function createTransform( |
309
|
|
|
FinderPattern $nw, |
310
|
|
|
FinderPattern $ne, |
311
|
|
|
FinderPattern $sw, |
312
|
|
|
int $size, |
313
|
|
|
AlignmentPattern $ap = null |
314
|
|
|
):PerspectiveTransform{ |
315
|
|
|
$dimMinusThree = ($size - 3.5); |
316
|
|
|
|
317
|
|
|
if($ap instanceof AlignmentPattern){ |
318
|
|
|
$bottomRightX = $ap->getX(); |
319
|
|
|
$bottomRightY = $ap->getY(); |
320
|
|
|
$sourceBottomRightX = ($dimMinusThree - 3.0); |
321
|
|
|
$sourceBottomRightY = $sourceBottomRightX; |
322
|
|
|
} |
323
|
|
|
else{ |
324
|
|
|
// Don't have an alignment pattern, just make up the bottom-right point |
325
|
|
|
$bottomRightX = ($ne->getX() - $nw->getX() + $sw->getX()); |
326
|
|
|
$bottomRightY = ($ne->getY() - $nw->getY() + $sw->getY()); |
327
|
|
|
$sourceBottomRightX = $dimMinusThree; |
328
|
|
|
$sourceBottomRightY = $dimMinusThree; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
return (new PerspectiveTransform)->quadrilateralToQuadrilateral( |
332
|
|
|
3.5, |
333
|
|
|
3.5, |
334
|
|
|
$dimMinusThree, |
335
|
|
|
3.5, |
336
|
|
|
$sourceBottomRightX, |
337
|
|
|
$sourceBottomRightY, |
338
|
|
|
3.5, |
339
|
|
|
$dimMinusThree, |
340
|
|
|
$nw->getX(), |
341
|
|
|
$nw->getY(), |
342
|
|
|
$ne->getX(), |
343
|
|
|
$ne->getY(), |
344
|
|
|
$bottomRightX, |
345
|
|
|
$bottomRightY, |
346
|
|
|
$sw->getX(), |
347
|
|
|
$sw->getY() |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
} |
352
|
|
|
|