diff --git a/src/Decoder/BitMatrix.php b/src/Decoder/BitMatrix.php index 588564b1f..3c78335c6 100644 --- a/src/Decoder/BitMatrix.php +++ b/src/Decoder/BitMatrix.php @@ -13,7 +13,7 @@ namespace chillerlan\QRCode\Decoder; use chillerlan\QRCode\Common\{EccLevel, MaskPattern, Version}; use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix}; -use function array_fill, count; +use function array_fill, array_map, array_reverse, count; use const PHP_INT_MAX, PHP_INT_SIZE; /** @@ -63,6 +63,10 @@ final class BitMatrix extends QRMatrix{ private const FORMAT_INFO_MASK_QR = 0x5412; // 0101010000010010 + /** + * This flag has effect only on the copyVersionBit() method. + * Before proceeding with readCodewords() the resetInfo() method should be called. + */ private bool $mirror = false; /** @@ -74,33 +78,26 @@ final class BitMatrix extends QRMatrix{ } /** - * Prepare the parser for a mirrored operation. - * This flag has effect only on the readFormatInformation() and the - * readVersion() methods. Before proceeding with readCodewords() the - * mirror() method should be called. + * Resets the current version info in order to attempt another reading */ - public function setMirror(bool $mirror):self{ + public function resetVersionInfo():self{ $this->version = null; $this->eccLevel = null; $this->maskPattern = null; - $this->mirror = $mirror; return $this; } /** - * Mirror the bit matrix in order to attempt a second reading. + * Mirror the bit matrix diagonally in order to attempt a second reading. */ - public function mirror():self{ + public function mirrorDiagonal():self{ + $this->mirror = !$this->mirror; - for($x = 0; $x < $this->moduleCount; $x++){ - for($y = ($x + 1); $y < $this->moduleCount; $y++){ - if($this->get($x, $y) !== $this->get($y, $x)){ - $this->flip($y, $x); - $this->flip($x, $y); - } - } - } + // mirror vertically + $matrix = array_reverse($this->matrix); + // rotate by 90 degrees clockwise + $this->matrix = array_map(fn(...$a) => array_reverse($a), ...$matrix); return $this; } diff --git a/src/Decoder/Decoder.php b/src/Decoder/Decoder.php index 63839da0c..87a09e939 100644 --- a/src/Decoder/Decoder.php +++ b/src/Decoder/Decoder.php @@ -54,7 +54,7 @@ final class Decoder{ * that the QR code may be mirrored, and we should try once more with a * mirrored content. */ - return $this->decodeMatrix($matrix->setMirror(true)->mirror()); + return $this->decodeMatrix($matrix->resetVersionInfo()->mirrorDiagonal()); } catch(Throwable $f){ // Throw the exception from the original reading