Skip to content

Commit 0a0699f

Browse files
committed
Fixed the 1.0.1 version
2 parents 3f95565 + 7bf6628 commit 0a0699f

File tree

8 files changed

+325
-65
lines changed

8 files changed

+325
-65
lines changed

README.md

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Install using composer
99
```json
1010
{
1111
"require": {
12-
"dsiddharth2/php-zxing": "1.0.0"
12+
"dsiddharth2/php-zxing": "1.0.1"
1313
}
1414
}
1515
```
@@ -19,21 +19,55 @@ Note
1919
* Only Decoder is programmed right now. Needs programming of Encoder.
2020
* The Default location of java that is configured is /usr/bin/java
2121

22+
Changes in version 1.0.1
23+
--------------------
24+
* Added a isFound function that will tell if the bar code is found.
25+
* If the image has one bar code detected, then it returns the object instead of array of a single object.
2226

2327
Basic Usage
2428
----------
2529
```php
2630
use PHPZxing\PHPZxingDecoder;
2731

2832
$decoder = new PHPZxingDecoder();
29-
$decodedData = current($decoder->decode('../images/Code128Barcode.jpg'));
30-
$decodedData->getImageValue();
31-
$decodedData->getFormat();
32-
$decodedData->getType();
33+
$decodedData = $decoder->decode('../images/Code128Barcode.jpg');
34+
if($data->isFound()) {
35+
$data->getImageValue();
36+
$data->getFormat();
37+
$data->getType();
38+
}
3339
```
3440

3541
The Decoded data is an Array of Objects of PHPZxing\ZxingImage if the bar code is found. If not found then it is an array of Objects PHPZxing\ZxingBarNotFound.
3642

43+
Checking for existence of Barcode
44+
----------
45+
The Existance of bar code can be found using the functoin isFound()
46+
47+
```php
48+
use PHPZxing\PHPZxingDecoder;
49+
50+
$decoder = new PHPZxingDecoder();
51+
$data = $decoder->decode('../images/Code128Barcode.jpg');
52+
if($data->isFound()) {
53+
$data->getImageValue();
54+
$data->getFormat();
55+
$data->getType();
56+
}
57+
```
58+
59+
You can also check using the instanceof object,
60+
```php
61+
use PHPZxing\PHPZxingDecoder;
62+
63+
$decoder = new PHPZxingDecoder();
64+
$data = $decoder->decode('../images/Code128Barcode.jpg');
65+
if($data instanceof PHPZxing\ZxingImage) {
66+
$data->getImageValue();
67+
$data->getFormat();
68+
$data->getType();
69+
}
70+
```
3771
The Public methods that we can use in PHPZxing\ZxingImage are,
3872

3973
| Method Name | Function |
@@ -58,13 +92,17 @@ Setting the configurations
5892
use PHPZxing\PHPZxingDecoder;
5993

6094
$config = array(
61-
'try_harder' => true, // Non mobile mode
62-
'multiple_bar_codes' => true, // If the image contains muliple bar codes
63-
'crop' => '100,200,300,300', // If you want to crop image in pixels
95+
'try_harder' => true,
6496
);
6597
$decoder = new PHPZxingDecoder($config);
66-
$decodedData = $decoder->decode('../images/'); // Reads images in complete directory
67-
print_r($decodedData);
98+
$decodedArray = $decoder->decode('../images');
99+
if(is_array($decodedArray)){
100+
foreach ($decodedArray as $data) {
101+
if($data->isFound()) {
102+
print_r($data);
103+
}
104+
}
105+
}
68106
```
69107

70108
You can also use it with configurations. The Decoder has 3 configurations,
@@ -84,15 +122,35 @@ You can pass array of images too,
84122
use PHPZxing\PHPZxingDecoder;
85123

86124
$decoder = new PHPZxingDecoder();
87-
// Images can be sent as an array
88125
$imageArrays = array(
89126
'../images/Code128Barcode.jpg',
90127
'../images/Code39Barcode.jpg'
91128
);
92-
$decodedData = $decoder->decode($imageArrays);
129+
$decodedArray = $decoder->decode($imageArrays);
130+
foreach ($decodedArray as $data) {
131+
if($data instanceof PHPZxing\ZxingImage) {
132+
print_r($data);
133+
} else {
134+
echo "Bar Code cannot be read";
135+
}
136+
}
137+
```
138+
139+
Reading multiple bar codes,
140+
141+
```php
142+
use PHPZxing\PHPZxingDecoder;
143+
144+
$config = array(
145+
'try_harder' => true,
146+
'multiple_bar_codes' => true
147+
);
148+
$decoder = new PHPZxingDecoder($config);
149+
$decodedData = $decoder->decode('../images/multiple_bar_codes.jpg');
93150
print_r($decodedData);
94151
```
95152

153+
96154
Set Java Path
97155
----------
98156
If your java PATH is not set properly, the decoder will not work. You need to set path of java variable.

src/PHPZxing/PHPZxingBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
- Siddharth Deshpande (dsiddharth2@gmail.com)
99
...
1010
* PHPZxing
11-
* Version 1.0
12-
* Copyright (c) 2017 Siddharth Deshpande
11+
* Version 1.0.1
12+
* Copyright (c) 2018 Siddharth Deshpande
1313
*
1414
* Permission is hereby granted, free of charge, to any person
1515
* obtaining a copy of this software and associated documentation

src/PHPZxing/PHPZxingDecoder.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
-
1616
...
1717
* PHPZxingDecoder
18-
* Version 1.0
19-
* Copyright (c) 2017 Siddharth Deshpande
18+
* Version 1.0.1
19+
* Copyright (c) 2018 Siddharth Deshpande
2020
*
2121
* Permission is hereby granted, free of charge, to any person
2222
* obtaining a copy of this software and associated documentation
@@ -79,7 +79,11 @@ public function __construct($config = array()) {
7979

8080
if(isset($config['crop']) && array_key_exists('crop', $config)) {
8181
$this->crop = strval($config['crop']);
82-
}
82+
}
83+
84+
if(isset($config['returnAs']) && array_key_exists('returnAs', $config)) {
85+
$this->returnAs = strval($config['returnAs']);
86+
}
8387
}
8488

8589
private function basePrepare() {
@@ -170,6 +174,7 @@ private function createImages($output) {
170174
$image[] = new ZxingImage($imagePath, $imageValue, $format, $type);
171175

172176
} else if(preg_match('/No barcode found/', $singleLine)) {
177+
173178
$exploded = explode(" ", $singleLine);
174179
$imagePath = array_shift($exploded);
175180
$image[] = new ZxingBarNotFound($imagePath, 101, "No barcode found");
@@ -230,6 +235,11 @@ public function decode($image = null) {
230235
throw new \Exception("Is the java PATH set correctly ? Current Path set is : " . $this->getJavaPath());
231236
}
232237

238+
// If the image is single then return the actual image
239+
if(count($image) == 1) {
240+
return current($image);
241+
}
242+
233243
return $image;
234244
} catch(\Exception $e) {
235245
echo $e->getMessage();

src/PHPZxing/PHPZxingInterface.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/*
3+
Descrition : PHPZxingInterface interface that will have all the interface methods stored
4+
5+
license: MIT-style
6+
7+
authors:
8+
- Siddharth Deshpande (dsiddharth2@gmail.com)
9+
...
10+
* PHPZxing
11+
* Version 1.0.1
12+
* Copyright (c) 2018 Siddharth Deshpande
13+
*
14+
* Permission is hereby granted, free of charge, to any person
15+
* obtaining a copy of this software and associated documentation
16+
* files (the "Software"), to deal in the Software without
17+
* restriction, including without limitation the rights to use,
18+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
19+
* copies of the Software, and to permit persons to whom the
20+
* Software is furnished to do so, subject to the following
21+
* conditions:
22+
*
23+
* The above copyright notice and this permission notice shall be
24+
* included in all copies or substantial portions of the Software.
25+
*
26+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
28+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
30+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
31+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33+
* OTHER DEALINGS IN THE SOFTWARE.
34+
*/
35+
namespace PHPZxing;
36+
37+
interface PHPZxingInterface {
38+
public function isFound();
39+
}

src/PHPZxing/ZxingBarNotFound.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
- Siddharth Deshpande (dsiddharth2@gmail.com)
1010
...
1111
* PHPZxing
12-
* Version 1.0
13-
* Copyright (c) 2017 Siddharth Deshpande
12+
* Version 1.0.1
13+
* Copyright (c) 2018 Siddharth Deshpande
1414
*
1515
* Permission is hereby granted, free of charge, to any person
1616
* obtaining a copy of this software and associated documentation
@@ -36,7 +36,9 @@
3636

3737
namespace PHPZxing;
3838

39-
class ZxingBarNotFound {
39+
use PHPZxing\PHPZxingInterface;
40+
41+
class ZxingBarNotFound implements PHPZxingInterface {
4042
// Path of the image decoded
4143
private $imagePath = null;
4244

@@ -63,4 +65,8 @@ public function getImageErrorCode() {
6365
public function getErrorMessage() {
6466
return $this->message;
6567
}
68+
69+
public function isFound() {
70+
return false;
71+
}
6672
}

src/PHPZxing/ZxingImage.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
- Siddharth Deshpande (dsiddharth2@gmail.com)
99
...
1010
* PHPZxing
11-
* Version 1.0
12-
* Copyright (c) 2017 Siddharth Deshpande
11+
* Version 1.0.1
12+
* Copyright (c) 2018 Siddharth Deshpande
1313
*
1414
* Permission is hereby granted, free of charge, to any person
1515
* obtaining a copy of this software and associated documentation
@@ -34,7 +34,9 @@
3434
*/
3535
namespace PHPZxing;
3636

37-
class ZxingImage {
37+
use PHPZxing\PHPZxingInterface;
38+
39+
class ZxingImage implements PHPZxingInterface {
3840
// Decoded Value from source
3941
private $imageValue = null;
4042

@@ -54,6 +56,10 @@ public function __construct($imagePath, $imageValue , $format, $type) {
5456
$this->imagePath = $imagePath;
5557
}
5658

59+
public function isFound() {
60+
return true;
61+
}
62+
5763
public function getImageValue() {
5864
return $this->imageValue;
5965
}

0 commit comments

Comments
 (0)