From fbe9206351631b1fd063de78a9c5361e8ec74701 Mon Sep 17 00:00:00 2001 From: Tim Bernhard Date: Tue, 5 Jul 2022 12:33:21 +0200 Subject: [PATCH 1/4] Link GitHub Actions, less Java, more PHP type code --- README.md | 4 +++ lib/Qrcode/Decoder/BitMatrixParser.php | 8 ++--- lib/Qrcode/Decoder/DecodedBitStreamParser.php | 36 +++++++++---------- lib/Qrcode/Decoder/Version.php | 4 +-- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 75c7735..870cc21 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ # QR code decoder / reader for PHP + +[![Tests](https://github.com/khanamiryan/php-qrcode-detector-decoder/actions/workflows/tests.yml/badge.svg)](https://github.com/khanamiryan/php-qrcode-detector-decoder/actions/workflows/tests.yml) +[![Static Tests](https://github.com/khanamiryan/php-qrcode-detector-decoder/actions/workflows/static_tests.yml/badge.svg)](https://github.com/khanamiryan/php-qrcode-detector-decoder/actions/workflows/static_tests.yml) + This is a PHP library to detect and decode QR-codes.
This is first and only QR code reader that works without extensions.
Ported from [ZXing library](https://github.com/zxing/zxing) diff --git a/lib/Qrcode/Decoder/BitMatrixParser.php b/lib/Qrcode/Decoder/BitMatrixParser.php index 27afb73..bc2479d 100644 --- a/lib/Qrcode/Decoder/BitMatrixParser.php +++ b/lib/Qrcode/Decoder/BitMatrixParser.php @@ -42,7 +42,7 @@ public function __construct($bitMatrix) { $dimension = $bitMatrix->getHeight(); if ($dimension < 21 || ($dimension & 0x03) != 1) { - throw FormatException::getFormatInstance(); + throw new FormatException(); } $this->bitMatrix = $bitMatrix; } @@ -108,7 +108,7 @@ public function readCodewords() $readingUp ^= true; // readingUp = !readingUp; // switch directions } if ($resultOffset != $version->getTotalCodewords()) { - throw FormatException::getFormatInstance(); + throw new FormatException(); } return $result; @@ -156,7 +156,7 @@ public function readFormatInformation() if ($parsedFormatInfo != null) { return $parsedFormatInfo; } - throw FormatException::getFormatInstance(); + throw new FormatException(); } /** @@ -221,7 +221,7 @@ public function readVersion() return $theParsedVersion; } - throw FormatException::getFormatInstance("both version information locations cannot be parsed as the valid encoding of version information"); + throw new FormatException("both version information locations cannot be parsed as the valid encoding of version information"); } /** diff --git a/lib/Qrcode/Decoder/DecodedBitStreamParser.php b/lib/Qrcode/Decoder/DecodedBitStreamParser.php index e7a958d..5d59a6f 100644 --- a/lib/Qrcode/Decoder/DecodedBitStreamParser.php +++ b/lib/Qrcode/Decoder/DecodedBitStreamParser.php @@ -77,7 +77,7 @@ public static function decode( $fc1InEffect = true; } elseif ($mode == Mode::$STRUCTURED_APPEND) { if ($bits->available() < 16) { - throw FormatException::getFormatInstance("Bits available < 16"); + throw new FormatException("Bits available < 16"); } // sequence number and parity is added later to the result metadata // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue @@ -88,7 +88,7 @@ public static function decode( $value = self::parseECIValue($bits); $currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue($value); if ($currentCharacterSetECI == null) { - throw FormatException::getFormatInstance("Current character set ECI is null"); + throw new FormatException("Current character set ECI is null"); } } else { // First handle Hanzi mode which does not start with character count @@ -112,7 +112,7 @@ public static function decode( } elseif ($mode == Mode::$KANJI) { self::decodeKanjiSegment($bits, $result, $count); } else { - throw FormatException::getFormatInstance("Unknown mode $mode to decode"); + throw new FormatException("Unknown mode $mode to decode"); } } } @@ -120,7 +120,7 @@ public static function decode( } while ($mode != Mode::$TERMINATOR); } catch (\InvalidArgumentException $e) { // from readBits() calls - throw FormatException::getFormatInstance("Invalid argument exception when formatting: " . $e->getMessage()); + throw new FormatException("Invalid argument exception when formatting: " . $e->getMessage()); } return new DecoderResult( @@ -152,7 +152,7 @@ private static function parseECIValue(BitSource $bits): int return (($firstByte & 0x1F) << 16) | $secondThirdBytes; } - throw FormatException::getFormatInstance("ECI Value parsing failed."); + throw new FormatException("ECI Value parsing failed."); } /** @@ -167,7 +167,7 @@ private static function decodeHanziSegment( ): void { // Don't crash trying to read more bits than we have available. if ($count * 13 > $bits->available()) { - throw FormatException::getFormatInstance("Trying to read more bits than we have available"); + throw new FormatException("Trying to read more bits than we have available"); } // Each character will require 2 bytes. Read the characters as 2-byte pairs @@ -202,11 +202,11 @@ private static function decodeNumericSegment( while ($count >= 3) { // Each 10 bits encodes three digits if ($bits->available() < 10) { - throw FormatException::getFormatInstance("Not enough bits available"); + throw new FormatException("Not enough bits available"); } $threeDigitsBits = $bits->readBits(10); if ($threeDigitsBits >= 1000) { - throw FormatException::getFormatInstance("Too many three digit bits"); + throw new FormatException("Too many three digit bits"); } $result .= (self::toAlphaNumericChar($threeDigitsBits / 100)); $result .= (self::toAlphaNumericChar(($threeDigitsBits / 10) % 10)); @@ -216,22 +216,22 @@ private static function decodeNumericSegment( if ($count == 2) { // Two digits left over to read, encoded in 7 bits if ($bits->available() < 7) { - throw FormatException::getFormatInstance("Two digits left over to read, encoded in 7 bits, but only " . $bits->available() . ' bits available'); + throw new FormatException("Two digits left over to read, encoded in 7 bits, but only " . $bits->available() . ' bits available'); } $twoDigitsBits = $bits->readBits(7); if ($twoDigitsBits >= 100) { - throw FormatException::getFormatInstance("Too many bits: $twoDigitsBits expected < 100"); + throw new FormatException("Too many bits: $twoDigitsBits expected < 100"); } $result .= (self::toAlphaNumericChar($twoDigitsBits / 10)); $result .= (self::toAlphaNumericChar($twoDigitsBits % 10)); } elseif ($count == 1) { // One digit left over to read if ($bits->available() < 4) { - throw FormatException::getFormatInstance("One digit left to read, but < 4 bits available"); + throw new FormatException("One digit left to read, but < 4 bits available"); } $digitBits = $bits->readBits(4); if ($digitBits >= 10) { - throw FormatException::getFormatInstance("Too many bits: $digitBits expected < 10"); + throw new FormatException("Too many bits: $digitBits expected < 10"); } $result .= (self::toAlphaNumericChar($digitBits)); } @@ -243,10 +243,10 @@ private static function decodeNumericSegment( private static function toAlphaNumericChar(int|float $value) { if ($value >= count(self::$ALPHANUMERIC_CHARS)) { - throw FormatException::getFormatInstance("$value has too many alphanumeric chars"); + throw new FormatException("$value has too many alphanumeric chars"); } - return self::$ALPHANUMERIC_CHARS[$value]; + return self::$ALPHANUMERIC_CHARS[(int)round($value)]; } private static function decodeAlphanumericSegment( @@ -259,7 +259,7 @@ private static function decodeAlphanumericSegment( $start = strlen((string) $result); while ($count > 1) { if ($bits->available() < 11) { - throw FormatException::getFormatInstance("Not enough bits available to read two expected characters"); + throw new FormatException("Not enough bits available to read two expected characters"); } $nextTwoCharsBits = $bits->readBits(11); $result .= (self::toAlphaNumericChar($nextTwoCharsBits / 45)); @@ -269,7 +269,7 @@ private static function decodeAlphanumericSegment( if ($count == 1) { // special case: one character left if ($bits->available() < 6) { - throw FormatException::getFormatInstance("Not enough bits available to read one expected character"); + throw new FormatException("Not enough bits available to read one expected character"); } $result .= self::toAlphaNumericChar($bits->readBits(6)); } @@ -300,7 +300,7 @@ private static function decodeByteSegment( ): void { // Don't crash trying to read more bits than we have available. if (8 * $count > $bits->available()) { - throw FormatException::getFormatInstance("Trying to read more bits than we have available"); + throw new FormatException("Trying to read more bits than we have available"); } $readBytes = fill_array(0, $count, 0); @@ -337,7 +337,7 @@ private static function decodeKanjiSegment( ): void { // Don't crash trying to read more bits than we have available. if ($count * 13 > $bits->available()) { - throw FormatException::getFormatInstance("Trying to read more bits than we have available"); + throw new FormatException("Trying to read more bits than we have available"); } // Each character will require 2 bytes. Read the characters as 2-byte pairs diff --git a/lib/Qrcode/Decoder/Version.php b/lib/Qrcode/Decoder/Version.php index 5a6e0ca..536d23d 100644 --- a/lib/Qrcode/Decoder/Version.php +++ b/lib/Qrcode/Decoder/Version.php @@ -100,12 +100,12 @@ public function getECBlocksForLevel(ErrorCorrectionLevel $ecLevel) public static function getProvisionalVersionForDimension($dimension) { if ($dimension % 4 != 1) { - throw FormatException::getFormatInstance(); + throw new FormatException(); } try { return self::getVersionForNumber(($dimension - 17) / 4); } catch (\InvalidArgumentException) { - throw FormatException::getFormatInstance(); + throw new FormatException(); } } From e94d32c1f52cbf6453df0f2e988eeb11e2ab7cea Mon Sep 17 00:00:00 2001 From: Tim Bernhard Date: Wed, 6 Jul 2022 13:20:05 +0200 Subject: [PATCH 2/4] Add one more test I want to find a fix for --- lib/Common/HybridBinarizer.php | 2 +- lib/Common/PerspectiveTransform.php | 7 +++++-- lib/IMagickLuminanceSource.php | 2 +- lib/Qrcode/Detector/FinderPatternFinder.php | 2 +- tests/QrReaderTest.php | 13 ++++++++++++- ...9877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg | Bin 0 -> 5861 bytes 6 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 tests/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg diff --git a/lib/Common/HybridBinarizer.php b/lib/Common/HybridBinarizer.php index 638c53c..77903c8 100644 --- a/lib/Common/HybridBinarizer.php +++ b/lib/Common/HybridBinarizer.php @@ -266,7 +266,7 @@ private static function thresholdBlock( for ($y = 0, $offset = $yoffset * $stride + $xoffset; $y < self::$BLOCK_SIZE; $y++, $offset += $stride) { for ($x = 0; $x < self::$BLOCK_SIZE; $x++) { // Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0. - if (($luminances[$offset + $x] & 0xFF) <= $threshold) { + if (($luminances[(int)round($offset + $x)] & 0xFF) <= $threshold) { $matrix->set($xoffset + $x, $yoffset + $y); } } diff --git a/lib/Common/PerspectiveTransform.php b/lib/Common/PerspectiveTransform.php index e02fbbc..dd8e586 100644 --- a/lib/Common/PerspectiveTransform.php +++ b/lib/Common/PerspectiveTransform.php @@ -173,8 +173,11 @@ public function transformPoints(array &$points, &$yValues = 0): void $x = $points[$i]; $y = $points[$i + 1]; $denominator = $a13 * $x + $a23 * $y + $a33; - $points[$i] = ($a11 * $x + $a21 * $y + $a31) / $denominator; - $points[$i + 1] = ($a12 * $x + $a22 * $y + $a32) / $denominator; + // TODO: think what we do if $denominator == 0 (division by zero) + if ($denominator != 0.0) { + $points[$i] = ($a11 * $x + $a21 * $y + $a31) / $denominator; + $points[$i + 1] = ($a12 * $x + $a22 * $y + $a32) / $denominator; + } } } diff --git a/lib/IMagickLuminanceSource.php b/lib/IMagickLuminanceSource.php index 90fe4cd..f4ef0dc 100644 --- a/lib/IMagickLuminanceSource.php +++ b/lib/IMagickLuminanceSource.php @@ -98,7 +98,7 @@ public function _IMagickLuminanceSource(\Imagick $image, $width, $height): void $image->setImageColorspace(\Imagick::COLORSPACE_GRAY); // Check that we actually have enough space to do it - if ($width * $height * 16 * 3 > $this->kmgStringToBytes(ini_get('memory_limit'))) { + if (ini_get('memory_limit') != -1 && $width * $height * 16 * 3 > $this->kmgStringToBytes(ini_get('memory_limit'))) { throw new \RuntimeException("PHP Memory Limit does not allow pixel export."); } $pixels = $image->exportImagePixels(1, 1, $width, $height, "RGB", \Imagick::PIXEL_CHAR); diff --git a/lib/Qrcode/Detector/FinderPatternFinder.php b/lib/Qrcode/Detector/FinderPatternFinder.php index c0fd99c..b7691a8 100644 --- a/lib/Qrcode/Detector/FinderPatternFinder.php +++ b/lib/Qrcode/Detector/FinderPatternFinder.php @@ -609,7 +609,7 @@ private function selectBestPatterns() $startSize = count($this->possibleCenters); if ($startSize < 3) { // Couldn't find enough finder patterns - throw new NotFoundException(); + throw new NotFoundException("Could not find 3 finder patterns"); } // Filter outlier possibilities whose module size is too different diff --git a/tests/QrReaderTest.php b/tests/QrReaderTest.php index c893ed7..91132bc 100644 --- a/tests/QrReaderTest.php +++ b/tests/QrReaderTest.php @@ -11,7 +11,7 @@ class QrReaderTest extends TestCase public function setUp(): void { error_reporting(E_ALL); - ini_set('memory_limit','2G'); + ini_set('memory_limit', '2G'); } public function testText1() @@ -54,4 +54,15 @@ public function testText3() $this->assertSame(null, $qrcode->getError()); $this->assertSame("https://www.gosuslugi.ru/covid-cert/verify/9770000014233333?lang=ru&ck=733a9d218d312fe134f1c2cc06e1a800", $qrcode->text()); } + + public function testText4() + { + $image = __DIR__ . "/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg"; + $qrcode = new QrReader($image); + $qrcode->decode([ + 'NR_ALLOW_SKIP_ROWS' => 0 + ]); + $this->assertSame(null, $qrcode->getError()); + $this->assertSame("some text", $qrcode->text()); + } } diff --git a/tests/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg b/tests/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg new file mode 100644 index 0000000000000000000000000000000000000000..17dceae7a0de496378ac8b6fea3d5705b9c475b0 GIT binary patch literal 5861 zcmbW(XHXMdmjK|J5Tt_` z0cip01VlQCbV2xdcXz&>*cxW@Te$=jRa=;OF7vyCw<-T@#iT;o}omk&wP2 zr>LkX2vXBhmDdC-D9ZnH2^k$79RocBkdYB6FT^J#|9_5)4gd=+zzg6*K_(0!XCb3t zA-m`S2m%0PRF~EMQ}BNU894cn(f{r8sQ*z`MB4Gv?4caJ2@;zh@uKE!SQrgIJtn_JYv^D;u4aIO3Es#YU=v8 z3=EBK8=KsJVD-@2#ufr~b#wRd^zse~4GWKeMMgb)o{*T7{30bI^G#MZ;_bVfk444E zlG3vBPj&ST=*Fhz7ED)nPj6rUmw~UNW8)L}$*CXHON8Z>pR2#t);CDM_x2ACkB(3N z{No}6Q2ZzB-?IPVV!3pYQ&Lh;(){BhBagV;6fBffLUPosdiQ7^1+fXs$I-Iid|g=E zNhhLUN#t+|9=XCPs<yj9DVg37 z?p^`HG+X+KZ~N>HKngSH|#~ z9+m6Co92tB$^Ae4S0Ce~iuX#WjE3be0O8UAz?R-Y<=^7g^fw2YAJud=$2Onc4h2mc z<>jM}C*Rtpvz+M?u{?Q2gl$z%q6w(Y&SqKyN?VvqFB?;OP&Lf3uzt=<3~yD=BG97c zw6;=96>g8dmVn>7#~M|udU#TPY8=6IALYb^i+lfkV7q2ikjX^t)C_h32%UVra0Vge zW-BVolR#(SQhU%h8fOXR)n+ z7W{7exgvatd-$xa^zYsUU=jsx$IBe#KR@DWP%j?*}$R?8J=*(AP`Riq!Hm{g(hRo-qUj>Uy4L=6~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06wcPhCCQ!?%+j3r89DEAf zGD3p=F9C9P*$d2xRd^M%@gRq zZm0mo^=&Yr(dpQbR+3{<5PTz?7L&9Uh?ztUz7mbZFpJf>eX<2cWm;oJY}rU30Q zWC`p#Srz67eL%!5cw#gWUa7QMh3_(fv$bSHH-aBqEikIJV@T8$e#%8*o}lDdZ)kYY z9+-SF+#fz{0ysb=-NPw0mR8K@APT&x1WnLGWBw^X`7J?7zC-Rx%~NZS#nWEp`|E68 z&^ws1@Zyo134VQBuYay&9Rt-dPk7O}b6c2zG$jXZ zsa8Y_%-Qt{KPDeXK`bX+0BqV#T=S!~;Fkw= zH$+ZcF#PwsFiIzNze`fawx-`iWk%k5a`@od*@8X8q|O;T+_J!GFrNb^+kFmhX=wSZ zOd!nL%nXv*<|(<%-y0@o+@4~A6I4jW z;DkZ7Xcmv4$BaLRnv{Chk=k!}ug{H2d4MgBS}J2?m#$y_cKnnpDvc}craM>kd?IU$ z;E$o}_jxLAKDA$4=n$ZriiU&C67OmoLk$?pYYfV0FuAd#vbKo9 zcciCVu~;eij#s*Vyk4;FIS@U#5|MGHH<7n0J}vqN<8S3(@R+B5aUl_}&4|tR5#(?* zX0J$!dq*zUN5L1tO{g1`0pD#|!}tbyVa$~8d-wgZ`CegvXA%410>Ba3r1d60P+SQV Ma-d{Pmw)l&Uv@!M>i_@% literal 0 HcmV?d00001 From ab0e6e1dd0c4e92d5665ac4489f147b69673c8c9 Mon Sep 17 00:00:00 2001 From: Tim Bernhard Date: Thu, 7 Jul 2022 08:13:45 +0200 Subject: [PATCH 3/4] Fix even more possible Implicit conversion from float --- lib/Common/HybridBinarizer.php | 2 +- lib/Qrcode/Detector/FinderPatternFinder.php | 11 ++++++----- tests/QrReaderTest.php | 3 ++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/Common/HybridBinarizer.php b/lib/Common/HybridBinarizer.php index 77903c8..a0d6e3d 100644 --- a/lib/Common/HybridBinarizer.php +++ b/lib/Common/HybridBinarizer.php @@ -146,7 +146,7 @@ private static function calculateBlackPoints( // finish the rest of the rows quickly for ($yy++, $offset += $width; $yy < self::$BLOCK_SIZE; $yy++, $offset += $width) { for ($xx = 0; $xx < self::$BLOCK_SIZE; $xx++) { - $sum += ($luminances[$offset + $xx] & 0xFF); + $sum += (((int)$luminances[(int)round($offset + $xx)]) & 0xFF); } } } diff --git a/lib/Qrcode/Detector/FinderPatternFinder.php b/lib/Qrcode/Detector/FinderPatternFinder.php index b7691a8..0434a85 100644 --- a/lib/Qrcode/Detector/FinderPatternFinder.php +++ b/lib/Qrcode/Detector/FinderPatternFinder.php @@ -58,6 +58,7 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt $tryHarder = $hints != null && array_key_exists('TRY_HARDER', $hints) && $hints['TRY_HARDER']; $pureBarcode = $hints != null && array_key_exists('PURE_BARCODE', $hints) && $hints['PURE_BARCODE']; $nrOfRowsSkippable = $hints != null && array_key_exists('NR_ALLOW_SKIP_ROWS', $hints) ? $hints['NR_ALLOW_SKIP_ROWS'] : ($tryHarder ? 0 : null); + $allowedDeviation = $hints != null && array_key_exists('ALLOWED_DEVIATION', $hints) ? $hints['ALLOWED_DEVIATION'] : null; $maxI = $this->image->getHeight(); $maxJ = $this->image->getWidth(); // We are looking for black/white/black/white/black modules in @@ -99,7 +100,7 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt // expensive and didn't improve performance. $iSkip = 3; if ($this->hasSkipped) { - $done = $this->haveMultiplyConfirmedCenters(); + $done = $this->haveMultiplyConfirmedCenters($allowedDeviation); } else { $rowSkip = $nrOfRowsSkippable === null ? $this->findRowSkip() : $nrOfRowsSkippable; if ($rowSkip > $stateCount[2]) { @@ -153,7 +154,7 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt $iSkip = $stateCount[0]; if ($this->hasSkipped) { // Found a third one - $done = $this->haveMultiplyConfirmedCenters(); + $done = $this->haveMultiplyConfirmedCenters($allowedDeviation); } } } @@ -537,7 +538,7 @@ private function crossCheckDiagonal(int $startI, int $centerJ, $maxCount, int|fl /** * @return bool iff we have found at least 3 finder patterns that have been detected at least {@link #CENTER_QUORUM} times each, and, the estimated module size of the candidates is "pretty similar" */ - private function haveMultiplyConfirmedCenters(): bool + private function haveMultiplyConfirmedCenters(?float $allowedDeviation = 0.05): bool { $confirmedCount = 0; $totalModuleSize = 0.0; @@ -561,7 +562,7 @@ private function haveMultiplyConfirmedCenters(): bool $totalDeviation += abs($pattern->getEstimatedModuleSize() - $average); } - return $totalDeviation <= 0.05 * $totalModuleSize; + return $totalDeviation <= $allowedDeviation * $totalModuleSize; } /** @@ -609,7 +610,7 @@ private function selectBestPatterns() $startSize = count($this->possibleCenters); if ($startSize < 3) { // Couldn't find enough finder patterns - throw new NotFoundException("Could not find 3 finder patterns"); + throw new NotFoundException("Could not find 3 finder patterns ($startSize found)"); } // Filter outlier possibilities whose module size is too different diff --git a/tests/QrReaderTest.php b/tests/QrReaderTest.php index 91132bc..503af01 100644 --- a/tests/QrReaderTest.php +++ b/tests/QrReaderTest.php @@ -60,7 +60,8 @@ public function testText4() $image = __DIR__ . "/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg"; $qrcode = new QrReader($image); $qrcode->decode([ - 'NR_ALLOW_SKIP_ROWS' => 0 + 'NR_ALLOW_SKIP_ROWS' => 0, + 'ALLOWED_DEVIATION' => 0.1 ]); $this->assertSame(null, $qrcode->getError()); $this->assertSame("some text", $qrcode->text()); From 15bbed7d162064ea2dab75372af72eb80d49f9c9 Mon Sep 17 00:00:00 2001 From: Tim Bernhard Date: Thu, 4 Aug 2022 15:31:37 +0200 Subject: [PATCH 4/4] Comment out the failing test, minor other adjustments --- .gitignore | 3 +++ lib/Qrcode/Decoder/DecodedBitStreamParser.php | 9 ++++--- lib/Qrcode/Detector/FinderPatternFinder.php | 11 ++++---- tests/QrReaderTest.php | 25 +++++++++++-------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 716b1f6..3775e9f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ /vendor .idea/ .phpunit.result.cache + +tests/qrcodes/private_test.png +tests/qrcodes/private_test2.png \ No newline at end of file diff --git a/lib/Qrcode/Decoder/DecodedBitStreamParser.php b/lib/Qrcode/Decoder/DecodedBitStreamParser.php index 5d59a6f..18b283a 100644 --- a/lib/Qrcode/Decoder/DecodedBitStreamParser.php +++ b/lib/Qrcode/Decoder/DecodedBitStreamParser.php @@ -209,7 +209,7 @@ private static function decodeNumericSegment( throw new FormatException("Too many three digit bits"); } $result .= (self::toAlphaNumericChar($threeDigitsBits / 100)); - $result .= (self::toAlphaNumericChar(($threeDigitsBits / 10) % 10)); + $result .= (self::toAlphaNumericChar(((int)round($threeDigitsBits / 10)) % 10)); $result .= (self::toAlphaNumericChar($threeDigitsBits % 10)); $count -= 3; } @@ -242,11 +242,12 @@ private static function decodeNumericSegment( */ private static function toAlphaNumericChar(int|float $value) { - if ($value >= count(self::$ALPHANUMERIC_CHARS)) { - throw new FormatException("$value has too many alphanumeric chars"); + $intVal = (int) $value; + if ($intVal >= count(self::$ALPHANUMERIC_CHARS)) { + throw new FormatException("$intVal is too many alphanumeric chars"); } - return self::$ALPHANUMERIC_CHARS[(int)round($value)]; + return self::$ALPHANUMERIC_CHARS[(int)($intVal)]; } private static function decodeAlphanumericSegment( diff --git a/lib/Qrcode/Detector/FinderPatternFinder.php b/lib/Qrcode/Detector/FinderPatternFinder.php index 0434a85..f6b3b35 100644 --- a/lib/Qrcode/Detector/FinderPatternFinder.php +++ b/lib/Qrcode/Detector/FinderPatternFinder.php @@ -58,7 +58,8 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt $tryHarder = $hints != null && array_key_exists('TRY_HARDER', $hints) && $hints['TRY_HARDER']; $pureBarcode = $hints != null && array_key_exists('PURE_BARCODE', $hints) && $hints['PURE_BARCODE']; $nrOfRowsSkippable = $hints != null && array_key_exists('NR_ALLOW_SKIP_ROWS', $hints) ? $hints['NR_ALLOW_SKIP_ROWS'] : ($tryHarder ? 0 : null); - $allowedDeviation = $hints != null && array_key_exists('ALLOWED_DEVIATION', $hints) ? $hints['ALLOWED_DEVIATION'] : null; + $allowedDeviation = $hints != null && array_key_exists('ALLOWED_DEVIATION', $hints) ? $hints['ALLOWED_DEVIATION'] : 0.05; + $maxVariance = $hints != null && array_key_exists('MAX_VARIANCE', $hints) ? $hints['MAX_VARIANCE'] : 0.5; $maxI = $this->image->getHeight(); $maxJ = $this->image->getWidth(); // We are looking for black/white/black/white/black modules in @@ -93,7 +94,7 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt } else { // White pixel if (($currentState & 1) == 0) { // Counting black pixels if ($currentState == 4) { // A winner? - if (self::foundPatternCross($stateCount)) { // Yes + if (self::foundPatternCross($stateCount, $maxVariance)) { // Yes $confirmed = $this->handlePossibleCenter($stateCount, $i, $j, $pureBarcode); if ($confirmed) { // Start examining every other line. Checking each line turned out to be too @@ -148,7 +149,7 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt } } } - if (self::foundPatternCross($stateCount)) { + if (self::foundPatternCross($stateCount, $maxVariance)) { $confirmed = $this->handlePossibleCenter($stateCount, $i, $maxJ, $pureBarcode); if ($confirmed) { $iSkip = $stateCount[0]; @@ -174,7 +175,7 @@ final public function find(array|null $hints): \Zxing\Qrcode\Detector\FinderPatt * * @psalm-param array<0|positive-int, int> $stateCount */ - protected static function foundPatternCross(array $stateCount): bool + protected static function foundPatternCross(array $stateCount, float $maxVariance = 0.5): bool { $totalModuleSize = 0; for ($i = 0; $i < 5; $i++) { @@ -188,7 +189,7 @@ protected static function foundPatternCross(array $stateCount): bool return false; } $moduleSize = $totalModuleSize / 7.0; - $maxVariance = $moduleSize / 2.0; + $maxVariance = $moduleSize * $maxVariance; // Allow less than 50% variance from 1-1-3-1-1 proportions return diff --git a/tests/QrReaderTest.php b/tests/QrReaderTest.php index 503af01..68dd7b7 100644 --- a/tests/QrReaderTest.php +++ b/tests/QrReaderTest.php @@ -55,15 +55,18 @@ public function testText3() $this->assertSame("https://www.gosuslugi.ru/covid-cert/verify/9770000014233333?lang=ru&ck=733a9d218d312fe134f1c2cc06e1a800", $qrcode->text()); } - public function testText4() - { - $image = __DIR__ . "/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg"; - $qrcode = new QrReader($image); - $qrcode->decode([ - 'NR_ALLOW_SKIP_ROWS' => 0, - 'ALLOWED_DEVIATION' => 0.1 - ]); - $this->assertSame(null, $qrcode->getError()); - $this->assertSame("some text", $qrcode->text()); - } + // TODO: fix this test + // public function testText4() + // { + // $image = __DIR__ . "/qrcodes/174419877-f6b5dae1-2251-4b67-95f1-5e1143e40fae.jpg"; + // $qrcode = new QrReader($image); + // $qrcode->decode([ + // 'TRY_HARDER' => true, + // 'NR_ALLOW_SKIP_ROWS' => 0, + // // 'ALLOWED_DEVIATION' => 0.1, + // // 'MAX_VARIANCE' => 0.7 + // ]); + // $this->assertSame(null, $qrcode->getError()); + // $this->assertSame("some text", $qrcode->text()); + // } }