|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Zxing\Qrcode\Decoder; |
|
|
|
|
|
use Zxing\FormatException; |
|
|
use Zxing\Common\BitMatrix; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final class BitMatrixParser |
|
|
{ |
|
|
|
|
|
private $bitMatrix; |
|
|
private $parsedVersion; |
|
|
private $parsedFormatInfo; |
|
|
private $mirror; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($bitMatrix) |
|
|
{ |
|
|
$dimension = $bitMatrix->getHeight(); |
|
|
if ($dimension < 21 || ($dimension & 0x03) != 1) { |
|
|
throw FormatException::getFormatInstance(); |
|
|
} |
|
|
$this->bitMatrix = $bitMatrix; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function readCodewords() |
|
|
{ |
|
|
|
|
|
$formatInfo = $this->readFormatInformation(); |
|
|
$version = $this->readVersion(); |
|
|
|
|
|
|
|
|
|
|
|
$dataMask = DataMask::forReference($formatInfo->getDataMask()); |
|
|
$dimension = $this->bitMatrix->getHeight(); |
|
|
$dataMask->unmaskBitMatrix($this->bitMatrix, $dimension); |
|
|
|
|
|
$functionPattern = $version->buildFunctionPattern(); |
|
|
|
|
|
$readingUp = true; |
|
|
if ($version->getTotalCodewords()) { |
|
|
$result = fill_array(0, $version->getTotalCodewords(), 0); |
|
|
} else { |
|
|
$result = []; |
|
|
} |
|
|
$resultOffset = 0; |
|
|
$currentByte = 0; |
|
|
$bitsRead = 0; |
|
|
|
|
|
for ($j = $dimension - 1; $j > 0; $j -= 2) { |
|
|
if ($j == 6) { |
|
|
|
|
|
|
|
|
$j--; |
|
|
} |
|
|
|
|
|
for ($count = 0; $count < $dimension; $count++) { |
|
|
$i = $readingUp ? $dimension - 1 - $count : $count; |
|
|
for ($col = 0; $col < 2; $col++) { |
|
|
|
|
|
if (!$functionPattern->get($j - $col, $i)) { |
|
|
|
|
|
$bitsRead++; |
|
|
$currentByte <<= 1; |
|
|
if ($this->bitMatrix->get($j - $col, $i)) { |
|
|
$currentByte |= 1; |
|
|
} |
|
|
|
|
|
if ($bitsRead == 8) { |
|
|
$result[$resultOffset++] = $currentByte; |
|
|
$bitsRead = 0; |
|
|
$currentByte = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
$readingUp ^= true; |
|
|
} |
|
|
if ($resultOffset != $version->getTotalCodewords()) { |
|
|
throw FormatException::getFormatInstance(); |
|
|
} |
|
|
|
|
|
return $result; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function readFormatInformation() |
|
|
{ |
|
|
|
|
|
if ($this->parsedFormatInfo != null) { |
|
|
return $this->parsedFormatInfo; |
|
|
} |
|
|
|
|
|
|
|
|
$formatInfoBits1 = 0; |
|
|
for ($i = 0; $i < 6; $i++) { |
|
|
$formatInfoBits1 = $this->copyBit($i, 8, $formatInfoBits1); |
|
|
} |
|
|
|
|
|
$formatInfoBits1 = $this->copyBit(7, 8, $formatInfoBits1); |
|
|
$formatInfoBits1 = $this->copyBit(8, 8, $formatInfoBits1); |
|
|
$formatInfoBits1 = $this->copyBit(8, 7, $formatInfoBits1); |
|
|
|
|
|
for ($j = 5; $j >= 0; $j--) { |
|
|
$formatInfoBits1 = $this->copyBit(8, $j, $formatInfoBits1); |
|
|
} |
|
|
|
|
|
|
|
|
$dimension = $this->bitMatrix->getHeight(); |
|
|
$formatInfoBits2 = 0; |
|
|
$jMin = $dimension - 7; |
|
|
for ($j = $dimension - 1; $j >= $jMin; $j--) { |
|
|
$formatInfoBits2 = $this->copyBit(8, $j, $formatInfoBits2); |
|
|
} |
|
|
for ($i = $dimension - 8; $i < $dimension; $i++) { |
|
|
$formatInfoBits2 = $this->copyBit($i, 8, $formatInfoBits2); |
|
|
} |
|
|
|
|
|
$parsedFormatInfo = FormatInformation::decodeFormatInformation($formatInfoBits1, $formatInfoBits2); |
|
|
if ($parsedFormatInfo != null) { |
|
|
return $parsedFormatInfo; |
|
|
} |
|
|
throw FormatException::getFormatInstance(); |
|
|
} |
|
|
|
|
|
private function copyBit($i, $j, $versionBits) |
|
|
{ |
|
|
$bit = $this->mirror ? $this->bitMatrix->get($j, $i) : $this->bitMatrix->get($i, $j); |
|
|
|
|
|
return $bit ? ($versionBits << 1) | 0x1 : $versionBits << 1; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function readVersion() |
|
|
{ |
|
|
|
|
|
if ($this->parsedVersion != null) { |
|
|
return $this->parsedVersion; |
|
|
} |
|
|
|
|
|
$dimension = $this->bitMatrix->getHeight(); |
|
|
|
|
|
$provisionalVersion = ($dimension - 17) / 4; |
|
|
if ($provisionalVersion <= 6) { |
|
|
return Version::getVersionForNumber($provisionalVersion); |
|
|
} |
|
|
|
|
|
|
|
|
$versionBits = 0; |
|
|
$ijMin = $dimension - 11; |
|
|
for ($j = 5; $j >= 0; $j--) { |
|
|
for ($i = $dimension - 9; $i >= $ijMin; $i--) { |
|
|
$versionBits = $this->copyBit($i, $j, $versionBits); |
|
|
} |
|
|
} |
|
|
|
|
|
$theParsedVersion = Version::decodeVersionInformation($versionBits); |
|
|
if ($theParsedVersion != null && $theParsedVersion->getDimensionForVersion() == $dimension) { |
|
|
$this->parsedVersion = $theParsedVersion; |
|
|
|
|
|
return $theParsedVersion; |
|
|
} |
|
|
|
|
|
|
|
|
$versionBits = 0; |
|
|
for ($i = 5; $i >= 0; $i--) { |
|
|
for ($j = $dimension - 9; $j >= $ijMin; $j--) { |
|
|
$versionBits = $this->copyBit($i, $j, $versionBits); |
|
|
} |
|
|
} |
|
|
|
|
|
$theParsedVersion = Version::decodeVersionInformation($versionBits); |
|
|
if ($theParsedVersion != null && $theParsedVersion->getDimensionForVersion() == $dimension) { |
|
|
$this->parsedVersion = $theParsedVersion; |
|
|
|
|
|
return $theParsedVersion; |
|
|
} |
|
|
throw FormatException::getFormatInstance(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function remask() |
|
|
{ |
|
|
if ($this->parsedFormatInfo == null) { |
|
|
return; |
|
|
} |
|
|
$dataMask = DataMask::forReference($this->parsedFormatInfo->getDataMask()); |
|
|
$dimension = $this->bitMatrix->getHeight(); |
|
|
$dataMask->unmaskBitMatrix($this->bitMatrix, $dimension); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setMirror($mirror) |
|
|
{ |
|
|
$parsedVersion = null; |
|
|
$parsedFormatInfo = null; |
|
|
$this->mirror = $mirror; |
|
|
} |
|
|
|
|
|
|
|
|
public function mirror() |
|
|
{ |
|
|
for ($x = 0; $x < $this->bitMatrix->getWidth(); $x++) { |
|
|
for ($y = $x + 1; $y < $this->bitMatrix->getHeight(); $y++) { |
|
|
if ($this->bitMatrix->get($x, $y) != $this->bitMatrix->get($y, $x)) { |
|
|
$this->bitMatrix->flip($y, $x); |
|
|
$this->bitMatrix->flip($x, $y); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|