Skip to content

Commit f00f53f

Browse files
author
Ruslan Yarullin
committed
some clean
1 parent 7e53d44 commit f00f53f

11 files changed

+200
-234
lines changed

lib/BinaryBitmap.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
namespace Zxing;
1919

20-
use Zxing\Common\BitArray;
2120
use Zxing\Common\BitMatrix;
2221

23-
2422
/**
2523
* This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
2624
* accept a BinaryBitmap and attempt to decode it.
@@ -74,9 +72,9 @@ public function getBlackRow($y, $row)
7472
}
7573

7674
/**
77-
* @return Whether this bitmap can be cropped.
75+
* @return bool Whether this bitmap can be cropped.
7876
*/
79-
public function isCropSupported()
77+
public function isCropSupported(): bool
8078
{
8179
return $this->binarizer->getLuminanceSource()->isCropSupported();
8280
}
@@ -143,8 +141,6 @@ public function toString()
143141
return '';
144142
}
145143

146-
//@Override
147-
148144
/**
149145
* Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
150146
* and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
@@ -156,11 +152,11 @@ public function toString()
156152
*/
157153
public function getBlackMatrix(): BitMatrix
158154
{
159-
// The matrix is created on demand the first time it is requested, then cached. There are two
160-
// reasons for this:
161-
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
162-
// 1D Reader finds a barcode before the 2D Readers run.
163-
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
155+
// The matrix is created on demand the first time it is requested, then cached. There are two
156+
// reasons for this:
157+
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
158+
// 1D Reader finds a barcode before the 2D Readers run.
159+
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
164160
if ($this->matrix === null) {
165161
$this->matrix = $this->binarizer->getBlackMatrix();
166162
}

lib/LuminanceSource.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public final function getHeight()
6565
}
6666

6767
/**
68-
* @return Whether this subclass supports cropping.
68+
* @return bool Whether this subclass supports cropping.
6969
*/
7070
public function isCropSupported()
7171
{
@@ -153,8 +153,6 @@ public final function toString()
153153
return $result;
154154
}
155155

156-
//@Override
157-
158156
/**
159157
* Fetches one row of luminance data from the underlying platform's bitmap. Values range from
160158
* 0 (black) to 255 (white). Because Java does not have an unsigned byte type, callers will have
@@ -170,5 +168,4 @@ public final function toString()
170168
* An array containing the luminance data.
171169
*/
172170
public abstract function getRow($y, $row);
173-
174171
}

lib/QrReader.php

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,96 @@
33
namespace Zxing;
44

55
use Zxing\Common\HybridBinarizer;
6+
use Zxing\Qrcode\QRCodeReader;
67

78
final class QrReader
89
{
910
const SOURCE_TYPE_FILE = 'file';
1011
const SOURCE_TYPE_BLOB = 'blob';
1112
const SOURCE_TYPE_RESOURCE = 'resource';
12-
public $result;
1313

14-
public function __construct($imgsource, $sourcetype = QrReader::SOURCE_TYPE_FILE, $isUseImagickIfAvailable = true)
14+
private $bitmap;
15+
private $reader;
16+
private $result;
17+
18+
public function __construct($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true)
1519
{
16-
try {
17-
$time = microtime(true);
18-
switch ($sourcetype) {
19-
case QrReader::SOURCE_TYPE_FILE:
20-
if ($isUseImagickIfAvailable && extension_loaded('imagick')) {
21-
$im = new \Imagick();
22-
$im->readImage($imgsource);
23-
$im->rotateImage('#00000000', 180);
24-
$im->cropImage(500, 500, 0, 0);
25-
// $im->writeImage('test.jpg');
26-
} else {
27-
$image = file_get_contents($imgsource);
28-
$im = imagecreatefromstring($image);
29-
}
30-
break;
20+
if (!in_array($sourceType, [
21+
self::SOURCE_TYPE_FILE,
22+
self::SOURCE_TYPE_BLOB,
23+
self::SOURCE_TYPE_RESOURCE,
24+
], true)) {
25+
throw new \InvalidArgumentException('Invalid image source.');
26+
}
27+
$im = null;
28+
switch ($sourceType) {
29+
case QrReader::SOURCE_TYPE_FILE:
30+
if ($useImagickIfAvailable && extension_loaded('imagick')) {
31+
$im = new \Imagick();
32+
$im->readImage($imgSource);
33+
} else {
34+
$image = file_get_contents($imgSource);
35+
$im = imagecreatefromstring($image);
36+
}
37+
break;
3138

32-
case QrReader::SOURCE_TYPE_BLOB:
33-
if ($isUseImagickIfAvailable && extension_loaded('imagick')) {
34-
$im = new \Imagick();
35-
$im->readimageblob($imgsource);
36-
} else {
37-
$im = imagecreatefromstring($imgsource);
38-
}
39-
break;
39+
case QrReader::SOURCE_TYPE_BLOB:
40+
if ($useImagickIfAvailable && extension_loaded('imagick')) {
41+
$im = new \Imagick();
42+
$im->readImageBlob($imgSource);
43+
} else {
44+
$im = imagecreatefromstring($imgSource);
45+
}
46+
break;
4047

41-
case QrReader::SOURCE_TYPE_RESOURCE:
42-
$im = $imgsource;
43-
if ($isUseImagickIfAvailable && extension_loaded('imagick')) {
44-
$isUseImagickIfAvailable = true;
45-
} else {
46-
$isUseImagickIfAvailable = false;
47-
}
48-
break;
48+
case QrReader::SOURCE_TYPE_RESOURCE:
49+
$im = $imgSource;
50+
if ($useImagickIfAvailable && extension_loaded('imagick')) {
51+
$useImagickIfAvailable = true;
52+
} else {
53+
$useImagickIfAvailable = false;
54+
}
55+
break;
56+
}
57+
if ($useImagickIfAvailable && extension_loaded('imagick')) {
58+
if (!$im instanceof \Imagick) {
59+
throw new \InvalidArgumentException('Invalid image source.');
4960
}
50-
if ($isUseImagickIfAvailable && extension_loaded('imagick')) {
51-
$width = $im->getImageWidth();
52-
$height = $im->getImageHeight();
53-
$source = new IMagickLuminanceSource($im, $width, $height);
54-
} else {
55-
$width = imagesx($im);
56-
$height = imagesy($im);
57-
$source = new GDLuminanceSource($im, $width, $height);
61+
$width = $im->getImageWidth();
62+
$height = $im->getImageHeight();
63+
$source = new IMagickLuminanceSource($im, $width, $height);
64+
} else {
65+
if (!is_resource($im)) {
66+
throw new \InvalidArgumentException('Invalid image source.');
5867
}
59-
$histo = new HybridBinarizer($source);
60-
$bitmap = new BinaryBitmap($histo);
61-
$reader = new \Zxing\Qrcode\QRCodeReader();
62-
63-
echo 'init ', microtime(true) - $time, PHP_EOL;
68+
$width = imagesx($im);
69+
$height = imagesy($im);
70+
$source = new GDLuminanceSource($im, $width, $height);
71+
}
72+
$histo = new HybridBinarizer($source);
73+
$this->bitmap = new BinaryBitmap($histo);
74+
$this->reader = new QRCodeReader();
75+
}
6476

65-
$this->result = $reader->decode($bitmap);
66-
} catch (\Zxing\NotFoundException $er) {
77+
public function decode(): void
78+
{
79+
try {
80+
$this->result = $this->reader->decode($this->bitmap);
81+
} catch (NotFoundException $er) {
6782
$this->result = false;
68-
} catch (\Zxing\FormatException $er) {
83+
} catch (FormatException $er) {
6984
$this->result = false;
70-
} catch (\Zxing\ChecksumException $er) {
85+
} catch (ChecksumException $er) {
7186
$this->result = false;
7287
}
7388
}
7489

75-
public function decode()
76-
{
77-
return $this->text();
78-
}
79-
8090
public function text()
8191
{
92+
$this->decode();
93+
8294
if (method_exists($this->result, 'toString')) {
83-
return ($this->result->toString());
95+
return $this->result->toString();
8496
}
8597

8698
return $this->result;

lib/Result.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
*/
2525
final class Result
2626
{
27-
2827
private $text;
2928
private $rawBytes;
3029
private $resultPoints;
3130
private $format;
3231
private $resultMetadata;
3332
private $timestamp;
3433

35-
3634
public function __construct(
3735
$text,
3836
$rawBytes,
@@ -46,7 +44,7 @@ public function __construct(
4644
$this->resultPoints = $resultPoints;
4745
$this->format = $format;
4846
$this->resultMetadata = null;
49-
$this->timestamp = $timestamp ? $timestamp : time();
47+
$this->timestamp = $timestamp ?: time();
5048
}
5149

5250
/**
@@ -95,16 +93,16 @@ public function getResultMetadata()
9593

9694
public function putMetadata($type, $value)
9795
{
98-
if ($this->resultMetadata == null) {
96+
if ($this->resultMetadata === null) {
9997
$this->resultMetadata = [];
10098
}
10199
$resultMetadata[$type] = $value;
102100
}
103101

104102
public function putAllMetadata($metadata)
105103
{
106-
if ($metadata != null) {
107-
if ($this->resultMetadata == null) {
104+
if ($metadata !== null) {
105+
if ($this->resultMetadata === null) {
108106
$this->resultMetadata = $metadata;
109107
} else {
110108
$this->resultMetadata = array_merge($this->resultMetadata, $metadata);
@@ -115,9 +113,9 @@ public function putAllMetadata($metadata)
115113
public function addResultPoints($newPoints)
116114
{
117115
$oldPoints = $this->resultPoints;
118-
if ($oldPoints == null) {
116+
if ($oldPoints === null) {
119117
$this->resultPoints = $newPoints;
120-
} else if ($newPoints != null && count($newPoints) > 0) {
118+
} else if ($newPoints !== null && count($newPoints) > 0) {
121119
$allPoints = fill_array(0, count($oldPoints) + count($newPoints), 0);
122120
$allPoints = arraycopy($oldPoints, 0, $allPoints, 0, count($oldPoints));
123121
$allPoints = arraycopy($newPoints, 0, $allPoints, count($oldPoints), count($newPoints));
@@ -130,7 +128,6 @@ public function getTimestamp()
130128
return $this->timestamp;
131129
}
132130

133-
//@Override
134131
public function toString()
135132
{
136133
return $this->text;

0 commit comments

Comments
 (0)