1 | <?php |
||
2 | /** |
||
3 | * Class Decoder |
||
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\Decoder; |
||
13 | |||
14 | use chillerlan\QRCode\Common\{BitBuffer, EccLevel, MaskPattern, Mode, ReedSolomonDecoder, Version}; |
||
15 | use chillerlan\QRCode\Data\{AlphaNum, Byte, ECI, Hanzi, Kanji, Number}; |
||
16 | use chillerlan\QRCode\Detector\Detector; |
||
17 | use Throwable; |
||
18 | use function chr, str_replace; |
||
19 | |||
20 | /** |
||
21 | * The main class which implements QR Code decoding -- as opposed to locating and extracting |
||
22 | * the QR Code from an image. |
||
23 | * |
||
24 | * @author Sean Owen |
||
25 | */ |
||
26 | final class Decoder{ |
||
27 | |||
28 | private ?Version $version = null; |
||
29 | private ?EccLevel $eccLevel = null; |
||
30 | private ?MaskPattern $maskPattern = null; |
||
31 | private BitBuffer $bitBuffer; |
||
32 | |||
33 | /** |
||
34 | * Decodes a QR Code represented as a BitMatrix. |
||
35 | * A 1 or "true" is taken to mean a black module. |
||
36 | * |
||
37 | * @throws \Throwable|\chillerlan\QRCode\Decoder\QRCodeDecoderException |
||
38 | */ |
||
39 | public function decode(LuminanceSourceInterface $source):DecoderResult{ |
||
40 | $matrix = (new Detector($source))->detect(); |
||
41 | |||
42 | try{ |
||
43 | // clone the BitMatrix to avoid errors in case we run into mirroring |
||
44 | return $this->decodeMatrix(clone $matrix); |
||
45 | } |
||
46 | catch(Throwable $e){ |
||
47 | |||
48 | try{ |
||
49 | /* |
||
50 | * Prepare for a mirrored reading. |
||
51 | * |
||
52 | * Since we're here, this means we have successfully detected some kind |
||
53 | * of version and format information when mirrored. This is a good sign, |
||
54 | * that the QR code may be mirrored, and we should try once more with a |
||
55 | * mirrored content. |
||
56 | */ |
||
57 | return $this->decodeMatrix($matrix->resetVersionInfo()->mirrorDiagonal()); |
||
58 | } |
||
59 | catch(Throwable $f){ |
||
60 | // Throw the exception from the original reading |
||
61 | throw $e; |
||
62 | } |
||
63 | |||
64 | } |
||
65 | |||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException |
||
70 | */ |
||
71 | private function decodeMatrix(BitMatrix $matrix):DecoderResult{ |
||
72 | // Read raw codewords |
||
73 | $rawCodewords = $matrix->readCodewords(); |
||
74 | $this->version = $matrix->getVersion(); |
||
75 | $this->eccLevel = $matrix->getEccLevel(); |
||
76 | $this->maskPattern = $matrix->getMaskPattern(); |
||
77 | |||
78 | if($this->version === null || $this->eccLevel === null || $this->maskPattern === null){ |
||
79 | throw new QRCodeDecoderException('unable to read version or format info'); // @codeCoverageIgnore |
||
80 | } |
||
81 | |||
82 | $resultBytes = (new ReedSolomonDecoder($this->version, $this->eccLevel))->decode($rawCodewords); |
||
83 | |||
84 | return $this->decodeBitStream($resultBytes); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Decode the contents of that stream of bytes |
||
89 | * |
||
90 | * @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException |
||
91 | */ |
||
92 | private function decodeBitStream(BitBuffer $bitBuffer):DecoderResult{ |
||
93 | $this->bitBuffer = $bitBuffer; |
||
94 | $versionNumber = $this->version->getVersionNumber(); |
||
0 ignored issues
–
show
|
|||
95 | $symbolSequence = -1; |
||
96 | $parityData = -1; |
||
97 | $fc1InEffect = false; |
||
98 | $result = ''; |
||
99 | |||
100 | // While still another segment to read... |
||
101 | while($this->bitBuffer->available() >= 4){ |
||
102 | $datamode = $this->bitBuffer->read(4); // mode is encoded by 4 bits |
||
103 | |||
104 | // OK, assume we're done |
||
105 | if($datamode === Mode::TERMINATOR){ |
||
106 | break; |
||
107 | } |
||
108 | elseif($datamode === Mode::NUMBER){ |
||
109 | $result .= Number::decodeSegment($this->bitBuffer, $versionNumber); |
||
110 | } |
||
111 | elseif($datamode === Mode::ALPHANUM){ |
||
112 | $result .= $this->decodeAlphanumSegment($versionNumber, $fc1InEffect); |
||
113 | } |
||
114 | elseif($datamode === Mode::BYTE){ |
||
115 | $result .= Byte::decodeSegment($this->bitBuffer, $versionNumber); |
||
116 | } |
||
117 | elseif($datamode === Mode::KANJI){ |
||
118 | $result .= Kanji::decodeSegment($this->bitBuffer, $versionNumber); |
||
119 | } |
||
120 | elseif($datamode === Mode::STRCTURED_APPEND){ |
||
121 | |||
122 | if($this->bitBuffer->available() < 16){ |
||
123 | throw new QRCodeDecoderException('structured append: not enough bits left'); |
||
124 | } |
||
125 | // sequence number and parity is added later to the result metadata |
||
126 | // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue |
||
127 | $symbolSequence = $this->bitBuffer->read(8); |
||
128 | $parityData = $this->bitBuffer->read(8); |
||
129 | } |
||
130 | elseif($datamode === Mode::FNC1_FIRST || $datamode === Mode::FNC1_SECOND){ |
||
131 | // We do little with FNC1 except alter the parsed result a bit according to the spec |
||
132 | $fc1InEffect = true; |
||
133 | } |
||
134 | elseif($datamode === Mode::ECI){ |
||
135 | $result .= ECI::decodeSegment($this->bitBuffer, $versionNumber); |
||
136 | } |
||
137 | elseif($datamode === Mode::HANZI){ |
||
138 | $result .= Hanzi::decodeSegment($this->bitBuffer, $versionNumber); |
||
139 | } |
||
140 | else{ |
||
141 | throw new QRCodeDecoderException('invalid data mode'); |
||
142 | } |
||
143 | |||
144 | } |
||
145 | |||
146 | return new DecoderResult([ |
||
147 | 'rawBytes' => $this->bitBuffer, |
||
148 | 'data' => $result, |
||
149 | 'version' => $this->version, |
||
150 | 'eccLevel' => $this->eccLevel, |
||
151 | 'maskPattern' => $this->maskPattern, |
||
152 | 'structuredAppendParity' => $parityData, |
||
153 | 'structuredAppendSequence' => $symbolSequence, |
||
154 | ]); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * |
||
159 | */ |
||
160 | private function decodeAlphanumSegment(int $versionNumber, bool $fc1InEffect):string{ |
||
161 | $str = AlphaNum::decodeSegment($this->bitBuffer, $versionNumber); |
||
162 | |||
163 | // See section 6.4.8.1, 6.4.8.2 |
||
164 | if($fc1InEffect){ // ??? |
||
165 | // We need to massage the result a bit if in an FNC1 mode: |
||
166 | $str = str_replace(chr(0x1d), '%', $str); |
||
167 | $str = str_replace('%%', '%', $str); |
||
168 | } |
||
169 | |||
170 | return $str; |
||
171 | } |
||
172 | |||
173 | } |
||
174 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.