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