Skip to content

Commit 4f4e165

Browse files
author
Ruslan Yarullin
committed
more optimization
1 parent 96d5f80 commit 4f4e165

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3877
-3471
lines changed

composer.json

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
{
2-
"name": "khanamiryan/qrcode-detector-decoder",
3-
"type": "library",
4-
"description": "QR code decoder / reader",
5-
"keywords": ["qr", "zxing", "barcode"],
6-
"homepage": "https://github.com/khanamiryan/php-qrcode-detector-decoder",
7-
"license": "MIT",
8-
"authors": [{
9-
"name": "Ashot Khanamiryan",
10-
"email": "[email protected]",
11-
"homepage": "https://github.com/khanamiryan",
12-
"role": "Developer"
13-
}],
14-
"require": {
15-
"php": "^5.6|^7.0"
16-
},
17-
"require-dev": {
18-
"phpunit/phpunit": "^5.7"
19-
},
20-
"autoload": {
21-
"classmap": ["lib/"],
22-
"files": ["lib/common/customFunctions.php"]
2+
"name": "khanamiryan/qrcode-detector-decoder",
3+
"type": "library",
4+
"description": "QR code decoder / reader",
5+
"keywords": [
6+
"qr",
7+
"zxing",
8+
"barcode"
9+
],
10+
"homepage": "https://github.com/khanamiryan/php-qrcode-detector-decoder",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Ashot Khanamiryan",
15+
"email": "[email protected]",
16+
"homepage": "https://github.com/khanamiryan",
17+
"role": "Developer"
2318
}
19+
],
20+
"require": {
21+
"php": "^5.6|^7.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "^5.7"
25+
},
26+
"autoload": {
27+
"classmap": [
28+
"lib/"
29+
],
30+
"files": [
31+
"lib/common/customFunctions.php"
32+
]
33+
}
2434
}

lib/Binarizer.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
19-
namespace Zxing;
18+
namespace Zxing;
2019

2120
use Zxing\Common\BitArray;
2221
use Zxing\Common\BitMatrix;
@@ -29,15 +28,17 @@
2928
*
3029
* @author [email protected] (Daniel Switkin)
3130
*/
32-
abstract class Binarizer {
33-
31+
abstract class Binarizer
32+
{
3433
private $source;
3534

36-
protected function __construct($source) {
35+
protected function __construct($source)
36+
{
3737
$this->source = $source;
3838
}
3939

40-
public final function getLuminanceSource() {
40+
public final function getLuminanceSource()
41+
{
4142
return $this->source;
4243
}
4344

@@ -49,9 +50,10 @@ public final function getLuminanceSource() {
4950
* and passed in with each call for performance. However it is legal to keep more than one row
5051
* at a time if needed.
5152
*
52-
* @param y The row to fetch, which must be in [0, bitmap height)
53+
* @param y The row to fetch, which must be in [0, bitmap height)
5354
* @param row An optional preallocated array. If null or too small, it will be ignored.
5455
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
56+
*
5557
* @return The array of bits for this row (true means black).
5658
* @throws NotFoundException if row can't be binarized
5759
*/
@@ -74,16 +76,18 @@ public abstract function getBlackMatrix();
7476
* of 1 bit data. See Effective Java for why we can't use Java's clone() method.
7577
*
7678
* @param source The LuminanceSource this Binarizer will operate on.
79+
*
7780
* @return A new concrete Binarizer implementation object.
7881
*/
7982
public abstract function createBinarizer($source);
8083

81-
public final function getWidth() {
84+
public final function getWidth()
85+
{
8286
return $this->source->getWidth();
8387
}
8488

85-
public final function getHeight() {
89+
public final function getHeight()
90+
{
8691
return $this->source->getHeight();
8792
}
88-
8993
}

lib/BinaryBitmap.php

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
*
2828
* @author [email protected] (Daniel Switkin)
2929
*/
30-
final class BinaryBitmap {
30+
final class BinaryBitmap
31+
{
3132

32-
private $binarizer;
33-
private $matrix;
33+
private $binarizer;
34+
private $matrix;
3435

35-
public function __construct($binarizer) {
36+
public function __construct($binarizer)
37+
{
3638
if ($binarizer == null) {
3739
throw new \InvalidArgumentException("Binarizer must be non-null.");
3840
}
@@ -42,14 +44,16 @@ public function __construct($binarizer) {
4244
/**
4345
* @return The width of the bitmap.
4446
*/
45-
public function getWidth() {
47+
public function getWidth()
48+
{
4649
return $this->binarizer->getWidth();
4750
}
4851

4952
/**
5053
* @return The height of the bitmap.
5154
*/
52-
public function getHeight() {
55+
public function getHeight()
56+
{
5357
return $this->binarizer->getHeight();
5458
}
5559

@@ -58,63 +62,49 @@ public function getHeight() {
5862
* cached data. Callers should assume this method is expensive and call it as seldom as possible.
5963
* This method is intended for decoding 1D barcodes and may choose to apply sharpening.
6064
*
61-
* @param y The row to fetch, which must be in [0, bitmap height)
65+
* @param y The row to fetch, which must be in [0, bitmap height)
6266
* @param row An optional preallocated array. If null or too small, it will be ignored.
6367
* If used, the Binarizer will call BitArray.clear(). Always use the returned object.
68+
*
6469
* @return The array of bits for this row (true means black).
6570
* @throws NotFoundException if row can't be binarized
6671
*/
67-
public function getBlackRow($y, $row) {
72+
public function getBlackRow($y, $row)
73+
{
6874
return $this->binarizer->getBlackRow($y, $row);
6975
}
7076

71-
/**
72-
* Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
73-
* and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
74-
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one
75-
* fetched using getBlackRow(), so don't mix and match between them.
76-
*
77-
* @return The 2D array of bits for the image (true means black).
78-
* @throws NotFoundException if image can't be binarized to make a matrix
79-
*/
80-
public function getBlackMatrix(){
81-
// The matrix is created on demand the first time it is requested, then cached. There are two
82-
// reasons for this:
83-
// 1. This work will never be done if the caller only installs 1D Reader objects, or if a
84-
// 1D Reader finds a barcode before the 2D Readers run.
85-
// 2. This work will only be done once even if the caller installs multiple 2D Readers.
86-
if ($this->matrix == null) {
87-
$this->matrix = $this->binarizer->getBlackMatrix();
88-
}
89-
return $this->matrix;
90-
}
91-
9277
/**
9378
* @return Whether this bitmap can be cropped.
9479
*/
95-
public function isCropSupported() {
80+
public function isCropSupported()
81+
{
9682
return $this->binarizer->getLuminanceSource()->isCropSupported();
9783
}
9884

9985
/**
10086
* Returns a new object with cropped image data. Implementations may keep a reference to the
10187
* original data rather than a copy. Only callable if isCropSupported() is true.
10288
*
103-
* @param left The left coordinate, which must be in [0,getWidth())
104-
* @param top The top coordinate, which must be in [0,getHeight())
105-
* @param width The width of the rectangle to crop.
89+
* @param left The left coordinate, which must be in [0,getWidth())
90+
* @param top The top coordinate, which must be in [0,getHeight())
91+
* @param width The width of the rectangle to crop.
10692
* @param height The height of the rectangle to crop.
93+
*
10794
* @return A cropped version of this object.
10895
*/
109-
public function crop($left, $top, $width, $height) {
96+
public function crop($left, $top, $width, $height)
97+
{
11098
$newSource = $this->binarizer->getLuminanceSource()->crop($left, $top, $width, $height);
99+
111100
return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
112101
}
113102

114103
/**
115104
* @return Whether this bitmap supports counter-clockwise rotation.
116105
*/
117-
public function isRotateSupported() {
106+
public function isRotateSupported()
107+
{
118108
return $this->binarizer->getLuminanceSource()->isRotateSupported();
119109
}
120110

@@ -124,8 +114,10 @@ public function isRotateSupported() {
124114
*
125115
* @return A rotated version of this object.
126116
*/
127-
public function rotateCounterClockwise() {
117+
public function rotateCounterClockwise()
118+
{
128119
$newSource = $this->binarizer->getLuminanceSource()->rotateCounterClockwise();
120+
129121
return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
130122
}
131123

@@ -135,18 +127,44 @@ public function rotateCounterClockwise() {
135127
*
136128
* @return A rotated version of this object.
137129
*/
138-
public function rotateCounterClockwise45() {
130+
public function rotateCounterClockwise45()
131+
{
139132
$newSource = $this->binarizer->getLuminanceSource()->rotateCounterClockwise45();
133+
140134
return new BinaryBitmap($this->binarizer->createBinarizer($newSource));
141135
}
142136

143-
//@Override
144-
public function toString() {
137+
public function toString()
138+
{
145139
try {
146140
return $this->getBlackMatrix()->toString();
147141
} catch (NotFoundException $e) {
148142
return "";
149143
}
150144
}
151145

146+
//@Override
147+
148+
/**
149+
* Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
150+
* and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
151+
* may not apply sharpening. Therefore, a row from this matrix may not be identical to one
152+
* fetched using getBlackRow(), so don't mix and match between them.
153+
*
154+
* @return The 2D array of bits for the image (true means black).
155+
* @throws NotFoundException if image can't be binarized to make a matrix
156+
*/
157+
public function getBlackMatrix()
158+
{
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.
164+
if ($this->matrix == null) {
165+
$this->matrix = $this->binarizer->getBlackMatrix();
166+
}
167+
168+
return $this->matrix;
169+
}
152170
}

lib/ChecksumException.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@
2323
*
2424
* @author Sean Owen
2525
*/
26-
final class ChecksumException extends ReaderException {
27-
28-
private static $instance;
29-
30-
31-
32-
public static function getChecksumInstance($cause=null) {
33-
if (self::$isStackTrace) {
34-
return new ChecksumException($cause);
35-
} else {
36-
if(!self::$instance){
37-
self::$instance = new ChecksumException($cause);
26+
final class ChecksumException extends ReaderException
27+
{
28+
private static $instance;
29+
30+
public static function getChecksumInstance($cause = null)
31+
{
32+
if (self::$isStackTrace) {
33+
return new ChecksumException($cause);
34+
} else {
35+
if (!self::$instance) {
36+
self::$instance = new ChecksumException($cause);
37+
}
38+
39+
return self::$instance;
3840
}
39-
return self::$instance;
4041
}
41-
}
42-
43-
44-
}
42+
}

lib/FormatException.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,20 @@
2424
*
2525
* @author Sean Owen
2626
*/
27-
final class FormatException extends ReaderException {
28-
27+
final class FormatException extends ReaderException
28+
{
2929
private static $instance;
3030

31-
32-
public function __construct($cause=null) {
33-
34-
if($cause){
31+
public function __construct($cause = null)
32+
{
33+
if ($cause) {
3534
parent::__construct($cause);
3635
}
37-
3836
}
3937

40-
41-
public static function getFormatInstance($cause=null) {
42-
if(!self::$instance){
38+
public static function getFormatInstance($cause = null)
39+
{
40+
if (!self::$instance) {
4341
self::$instance = new FormatException();
4442
}
4543
if (self::$isStackTrace) {
@@ -49,4 +47,3 @@ public static function getFormatInstance($cause=null) {
4947
}
5048
}
5149
}
52-

0 commit comments

Comments
 (0)