Skip to content

Commit 7e53d44

Browse files
author
Ruslan Yarullin
committed
Some optimizations
1 parent 4f4e165 commit 7e53d44

13 files changed

+59
-51
lines changed

lib/Binarizer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ protected function __construct($source)
3737
$this->source = $source;
3838
}
3939

40+
/**
41+
* @return LuminanceSource
42+
*/
4043
public final function getLuminanceSource()
4144
{
4245
return $this->source;
@@ -54,7 +57,7 @@ public final function getLuminanceSource()
5457
* @param row An optional preallocated array. If null or too small, it will be ignored.
5558
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
5659
*
57-
* @return The array of bits for this row (true means black).
60+
* @return array The array of bits for this row (true means black).
5861
* @throws NotFoundException if row can't be binarized
5962
*/
6063
public abstract function getBlackRow($y, $row);
@@ -65,7 +68,7 @@ public abstract function getBlackRow($y, $row);
6568
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one
6669
* fetched using getBlackRow(), so don't mix and match between them.
6770
*
68-
* @return The 2D array of bits for the image (true means black).
71+
* @return BitMatrix The 2D array of bits for the image (true means black).
6972
* @throws NotFoundException if image can't be binarized to make a matrix
7073
*/
7174
public abstract function getBlackMatrix();
@@ -77,7 +80,7 @@ public abstract function getBlackMatrix();
7780
*
7881
* @param source The LuminanceSource this Binarizer will operate on.
7982
*
80-
* @return A new concrete Binarizer implementation object.
83+
* @return Binarizer A new concrete Binarizer implementation object.
8184
*/
8285
public abstract function createBinarizer($source);
8386

lib/BinaryBitmap.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,27 @@
2929
*/
3030
final class BinaryBitmap
3131
{
32-
3332
private $binarizer;
3433
private $matrix;
3534

36-
public function __construct($binarizer)
35+
public function __construct(Binarizer $binarizer)
3736
{
38-
if ($binarizer == null) {
37+
if ($binarizer === null) {
3938
throw new \InvalidArgumentException("Binarizer must be non-null.");
4039
}
4140
$this->binarizer = $binarizer;
4241
}
4342

4443
/**
45-
* @return The width of the bitmap.
44+
* @return int The width of the bitmap.
4645
*/
4746
public function getWidth()
4847
{
4948
return $this->binarizer->getWidth();
5049
}
5150

5251
/**
53-
* @return The height of the bitmap.
52+
* @return int The height of the bitmap.
5453
*/
5554
public function getHeight()
5655
{
@@ -66,7 +65,7 @@ public function getHeight()
6665
* @param row An optional preallocated array. If null or too small, it will be ignored.
6766
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
6867
*
69-
* @return The array of bits for this row (true means black).
68+
* @return array The array of bits for this row (true means black).
7069
* @throws NotFoundException if row can't be binarized
7170
*/
7271
public function getBlackRow($y, $row)
@@ -91,7 +90,7 @@ public function isCropSupported()
9190
* @param width The width of the rectangle to crop.
9291
* @param height The height of the rectangle to crop.
9392
*
94-
* @return A cropped version of this object.
93+
* @return BinaryBitmap A cropped version of this object.
9594
*/
9695
public function crop($left, $top, $width, $height)
9796
{
@@ -112,7 +111,7 @@ public function isRotateSupported()
112111
* Returns a new object with rotated image data by 90 degrees counterclockwise.
113112
* Only callable if {@link #isRotateSupported()} is true.
114113
*
115-
* @return A rotated version of this object.
114+
* @return BinaryBitmap A rotated version of this object.
116115
*/
117116
public function rotateCounterClockwise()
118117
{
@@ -125,7 +124,7 @@ public function rotateCounterClockwise()
125124
* Returns a new object with rotated image data by 45 degrees counterclockwise.
126125
* Only callable if {@link #isRotateSupported()} is true.
127126
*
128-
* @return A rotated version of this object.
127+
* @return BinaryBitmap A rotated version of this object.
129128
*/
130129
public function rotateCounterClockwise45()
131130
{
@@ -139,8 +138,9 @@ public function toString()
139138
try {
140139
return $this->getBlackMatrix()->toString();
141140
} catch (NotFoundException $e) {
142-
return "";
143141
}
142+
143+
return '';
144144
}
145145

146146
//@Override
@@ -151,17 +151,17 @@ public function toString()
151151
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one
152152
* fetched using getBlackRow(), so don't mix and match between them.
153153
*
154-
* @return The 2D array of bits for the image (true means black).
154+
* @return BitMatrix The 2D array of bits for the image (true means black).
155155
* @throws NotFoundException if image can't be binarized to make a matrix
156156
*/
157-
public function getBlackMatrix()
157+
public function getBlackMatrix(): BitMatrix
158158
{
159159
// The matrix is created on demand the first time it is requested, then cached. There are two
160160
// reasons for this:
161161
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
162162
// 1D Reader finds a barcode before the 2D Readers run.
163163
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
164-
if ($this->matrix == null) {
164+
if ($this->matrix === null) {
165165
$this->matrix = $this->binarizer->getBlackMatrix();
166166
}
167167

lib/GDLuminanceSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function GDLuminanceSource($gdImage, $width, $height)
110110
public function getRow($y, $row = null)
111111
{
112112
if ($y < 0 || $y >= $this->getHeight()) {
113-
throw new \InvalidArgumentException("Requested row is outside the image: " + y);
113+
throw new \InvalidArgumentException('Requested row is outside the image: ' . $y);
114114
}
115115
$width = $this->getWidth();
116116
if ($row == null || count($row) < $width) {

lib/IMagickLuminanceSource.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<?php
22

3-
43
namespace Zxing;
54

65
/**
76
* This class is used to help decode images from files which arrive as GD Resource
87
* It does not support rotation.
9-
*
10-
*
11-
*
128
*/
139
final class IMagickLuminanceSource extends LuminanceSource
1410
{
@@ -44,7 +40,7 @@ public function __construct(
4440
$this->top = $top;
4541
}
4642

47-
public function _IMagickLuminanceSource($image, $width, $height)
43+
public function _IMagickLuminanceSource(\Imagick $image, $width, $height)
4844
{
4945
parent::__construct($width, $height);
5046

@@ -66,8 +62,8 @@ public function _IMagickLuminanceSource($image, $width, $height)
6662
$array = [];
6763
$rgb = [];
6864

69-
for ($i = 0; $i < count($pixels); $i += 3) {
70-
65+
$countPixels = count($pixels);
66+
for ($i = 0; $i < $countPixels; $i += 3) {
7167
$r = $pixels[$i] & 0xff;
7268
$g = $pixels[$i + 1] & 0xff;
7369
$b = $pixels[$i + 2] & 0xff;
@@ -86,7 +82,7 @@ public function _IMagickLuminanceSource($image, $width, $height)
8682
public function getRow($y, $row = null)
8783
{
8884
if ($y < 0 || $y >= $this->getHeight()) {
89-
throw new \InvalidArgumentException("Requested row is outside the image: " + y);
85+
throw new \InvalidArgumentException('Requested row is outside the image: ' . $y);
9086
}
9187
$width = $this->getWidth();
9288
if ($row == null || count($row) < $width) {

lib/QrReader.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Zxing;
44

5+
use Zxing\Common\HybridBinarizer;
6+
57
final class QrReader
68
{
79
const SOURCE_TYPE_FILE = 'file';
@@ -12,11 +14,15 @@ final class QrReader
1214
public function __construct($imgsource, $sourcetype = QrReader::SOURCE_TYPE_FILE, $isUseImagickIfAvailable = true)
1315
{
1416
try {
17+
$time = microtime(true);
1518
switch ($sourcetype) {
1619
case QrReader::SOURCE_TYPE_FILE:
1720
if ($isUseImagickIfAvailable && extension_loaded('imagick')) {
1821
$im = new \Imagick();
1922
$im->readImage($imgsource);
23+
$im->rotateImage('#00000000', 180);
24+
$im->cropImage(500, 500, 0, 0);
25+
// $im->writeImage('test.jpg');
2026
} else {
2127
$image = file_get_contents($imgsource);
2228
$im = imagecreatefromstring($image);
@@ -44,16 +50,18 @@ public function __construct($imgsource, $sourcetype = QrReader::SOURCE_TYPE_FILE
4450
if ($isUseImagickIfAvailable && extension_loaded('imagick')) {
4551
$width = $im->getImageWidth();
4652
$height = $im->getImageHeight();
47-
$source = new \Zxing\IMagickLuminanceSource($im, $width, $height);
53+
$source = new IMagickLuminanceSource($im, $width, $height);
4854
} else {
4955
$width = imagesx($im);
5056
$height = imagesy($im);
51-
$source = new \Zxing\GDLuminanceSource($im, $width, $height);
57+
$source = new GDLuminanceSource($im, $width, $height);
5258
}
53-
$histo = new \Zxing\Common\HybridBinarizer($source);
54-
$bitmap = new \Zxing\BinaryBitmap($histo);
59+
$histo = new HybridBinarizer($source);
60+
$bitmap = new BinaryBitmap($histo);
5561
$reader = new \Zxing\Qrcode\QRCodeReader();
5662

63+
echo 'init ', microtime(true) - $time, PHP_EOL;
64+
5765
$this->result = $reader->decode($bitmap);
5866
} catch (\Zxing\NotFoundException $er) {
5967
$this->result = false;

lib/common/HybridBinarizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function getBlackMatrix()
8888
$blackPoints = $this->calculateBlackPoints($luminances, $subWidth, $subHeight, $width, $height);
8989

9090
$newMatrix = new BitMatrix($width, $height);
91-
$this->calculateThresholdForBlock($luminances, $subWidth, $subHeight, $width, $height, $blackPoints, $newMatrix);
91+
self::calculateThresholdForBlock($luminances, $subWidth, $subHeight, $width, $height, $blackPoints, $newMatrix);
9292
$this->matrix = $newMatrix;
9393
} else {
9494
// If the image is too small, fall back to the global histogram approach.

lib/common/reedsolomon/ReedSolomonDecoder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public function decode(&$received, $twoS)
8181
$omega = $sigmaOmega[1];
8282
$errorLocations = $this->findErrorLocations($sigma);
8383
$errorMagnitudes = $this->findErrorMagnitudes($omega, $errorLocations);
84-
for ($i = 0; $i < count($errorLocations); $i++) {
84+
$errorLocationsCount = count($errorLocations);
85+
for ($i = 0; $i < $errorLocationsCount; $i++) {
8586
$position = count($received) - 1 - $this->field->log($errorLocations[$i]);
8687
if ($position < 0) {
8788
throw new ReedSolomonException("Bad error location");

lib/qrcode/QRCodeReader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ public function decode(BinaryBitmap $image, $hints = null)
6060
$decoderResult = $this->decoder->decode($bits, $hints);
6161
$points = self::$NO_POINTS;
6262
} else {
63+
$time = microtime(true);
6364
$detector = new Detector($image->getBlackMatrix());
6465
$detectorResult = $detector->detect($hints);
66+
echo sprintf('detect: %f', microtime(true) - $time),PHP_EOL;
6567

68+
$time = microtime(true);
6669
$decoderResult = $this->decoder->decode($detectorResult->getBits(), $hints);
6770
$points = $detectorResult->getPoints();
71+
echo sprintf('decode: %f', microtime(true) - $time),PHP_EOL;
6872
}
6973

7074
// If the code was mirrored: swap the bottom-left and the top-right points.

lib/qrcode/decoder/DataBlock.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public static function getDataBlocks($rawCodewords,
7171
$result = [];//new DataBlock[$totalBlocks];
7272
$numResultBlocks = 0;
7373
foreach ($ecBlockArray as $ecBlock) {
74-
for ($i = 0; $i < $ecBlock->getCount(); $i++) {
74+
$ecBlockCount = $ecBlock->getCount();
75+
for ($i = 0; $i < $ecBlockCount; $i++) {
7576
$numDataCodewords = $ecBlock->getDataCodewords();
7677
$numBlockCodewords = $ecBlocks->getECCodewordsPerBlock() + $numDataCodewords;
7778
$result[$numResultBlocks++] = new DataBlock($numDataCodewords, fill_array(0, $numBlockCodewords, 0));

lib/qrcode/decoder/Mode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct($characterCountBitsForVersions, $bits)
4545
$this->bits = $bits;
4646
}
4747

48-
static function Init()
48+
public static function Init()
4949
{
5050

5151

0 commit comments

Comments
 (0)