-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathDecoder.php
173 lines (150 loc) · 5.21 KB
/
Decoder.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
<?php
/**
* Class Decoder
*
* @created 17.01.2021
* @author ZXing Authors
* @author Smiley <[email protected]>
* @copyright 2021 Smiley
* @license Apache-2.0
*/
namespace chillerlan\QRCode\Decoder;
use chillerlan\QRCode\Common\{BitBuffer, EccLevel, LuminanceSourceInterface, MaskPattern, Mode, Version};
use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Hanzi, Kanji, Number};
use chillerlan\QRCode\Detector\Detector;
use Throwable;
use function chr, str_replace;
/**
* The main class which implements QR Code decoding -- as opposed to locating and extracting
* the QR Code from an image.
*
* @author Sean Owen
*/
final class Decoder{
private ?Version $version = null;
private ?EccLevel $eccLevel = null;
private ?MaskPattern $maskPattern = null;
private BitBuffer $bitBuffer;
/**
* Decodes a QR Code represented as a BitMatrix.
* A 1 or "true" is taken to mean a black module.
*
* @throws \Throwable|\chillerlan\QRCode\Decoder\QRCodeDecoderException
*/
public function decode(LuminanceSourceInterface $source):DecoderResult{
$matrix = (new Detector($source))->detect();
try{
// clone the BitMatrix to avoid errors in case we run into mirroring
return $this->decodeMatrix(clone $matrix);
}
catch(Throwable $e){
try{
/*
* Prepare for a mirrored reading.
*
* Since we're here, this means we have successfully detected some kind
* of version and format information when mirrored. This is a good sign,
* that the QR code may be mirrored, and we should try once more with a
* mirrored content.
*/
return $this->decodeMatrix($matrix->resetVersionInfo()->mirrorDiagonal());
}
catch(Throwable $f){
// Throw the exception from the original reading
throw $e;
}
}
}
/**
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
*/
private function decodeMatrix(BitMatrix $matrix):DecoderResult{
// Read raw codewords
$rawCodewords = $matrix->readCodewords();
$this->version = $matrix->getVersion();
$this->eccLevel = $matrix->getEccLevel();
$this->maskPattern = $matrix->getMaskPattern();
if($this->version === null || $this->eccLevel === null || $this->maskPattern === null){
throw new QRCodeDecoderException('unable to read version or format info'); // @codeCoverageIgnore
}
$resultBytes = (new ReedSolomonDecoder($this->version, $this->eccLevel))->decode($rawCodewords);
return $this->decodeBitStream($resultBytes);
}
/**
* Decode the contents of that stream of bytes
*
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException
*/
private function decodeBitStream(BitBuffer $bitBuffer):DecoderResult{
$this->bitBuffer = $bitBuffer;
$versionNumber = $this->version->getVersionNumber();
$symbolSequence = -1;
$parityData = -1;
$fc1InEffect = false;
$result = '';
// While still another segment to read...
while($this->bitBuffer->available() >= 4){
$datamode = $this->bitBuffer->read(4); // mode is encoded by 4 bits
// OK, assume we're done
if($datamode === Mode::TERMINATOR){
break;
}
elseif($datamode === Mode::NUMBER){
$result .= Number::decodeSegment($this->bitBuffer, $versionNumber);
}
elseif($datamode === Mode::ALPHANUM){
$result .= $this->decodeAlphanumSegment($versionNumber, $fc1InEffect);
}
elseif($datamode === Mode::BYTE){
$result .= Byte::decodeSegment($this->bitBuffer, $versionNumber);
}
elseif($datamode === Mode::KANJI){
$result .= Kanji::decodeSegment($this->bitBuffer, $versionNumber);
}
elseif($datamode === Mode::STRCTURED_APPEND){
if($this->bitBuffer->available() < 16){
throw new QRCodeDecoderException('structured append: not enough bits left');
}
// sequence number and parity is added later to the result metadata
// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
$symbolSequence = $this->bitBuffer->read(8);
$parityData = $this->bitBuffer->read(8);
}
elseif($datamode === Mode::FNC1_FIRST || $datamode === Mode::FNC1_SECOND){
// We do little with FNC1 except alter the parsed result a bit according to the spec
$fc1InEffect = true;
}
elseif($datamode === Mode::ECI){
$result .= ECI::decodeSegment($this->bitBuffer, $versionNumber);
}
elseif($datamode === Mode::HANZI){
$result .= Hanzi::decodeSegment($this->bitBuffer, $versionNumber);
}
else{
throw new QRCodeDecoderException('invalid data mode');
}
}
return new DecoderResult([
'rawBytes' => $this->bitBuffer,
'data' => $result,
'version' => $this->version,
'eccLevel' => $this->eccLevel,
'maskPattern' => $this->maskPattern,
'structuredAppendParity' => $parityData,
'structuredAppendSequence' => $symbolSequence,
]);
}
/**
*
*/
private function decodeAlphanumSegment(int $versionNumber, bool $fc1InEffect):string{
$str = AlphaNum::decodeSegment($this->bitBuffer, $versionNumber);
// See section 6.4.8.1, 6.4.8.2
if($fc1InEffect){ // ???
// We need to massage the result a bit if in an FNC1 mode:
$str = str_replace(chr(0x1d), '%', $str);
$str = str_replace('%%', '%', $str);
}
return $str;
}
}