Skip to content

Commit 1c06116

Browse files
committed
Updated the examples file
1 parent 0a0699f commit 1c06116

File tree

1 file changed

+97
-175
lines changed

1 file changed

+97
-175
lines changed

src/examples/example.php

Lines changed: 97 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,105 @@
1-
PHPZxing - Wrapper for Zxing Java Library
2-
===========================================
3-
PHPZxing is a small php wrapper that uses the Zxing library to Create and read Barcodes.
4-
Under the hood it still uses the [Zxing library](https://github.com/zxing/zxing) to encode and decode data.
5-
6-
Install using composer
7-
--------------------
8-
9-
```json
10-
{
11-
"require": {
12-
"dsiddharth2/php-zxing": "1.0.1"
13-
}
14-
}
15-
```
16-
17-
Note
18-
--------------------
19-
* Only Decoder is programmed right now. Needs programming of Encoder.
20-
* The Default location of java that is configured is /usr/bin/java
21-
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.
26-
27-
Basic Usage
28-
----------
29-
```php
30-
use PHPZxing\PHPZxingDecoder;
31-
32-
$decoder = new PHPZxingDecoder();
33-
$decodedData = $decoder->decode('../images/Code128Barcode.jpg');
34-
if($data->isFound()) {
35-
$data->getImageValue();
36-
$data->getFormat();
37-
$data->getType();
38-
}
39-
```
40-
41-
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.
42-
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-
```
71-
The Public methods that we can use in PHPZxing\ZxingImage are,
72-
73-
| Method Name | Function |
74-
| ------------- |--------------------------------------------------------------|
75-
| getImageValue | Get the value decoded in the image passed |
76-
| getFormat | Get the format of the image that is encoded, example : CODE_39 |
77-
| getType | Get the type of the image decoded, example : URL, TEXT etc |
78-
| getImagePath | Get Path of the image |
79-
80-
The Public methods that we can use in PHPZxing\ZxingImage are,
81-
82-
| Method Name | Function |
83-
| ------------- |--------------------------------------------------------------|
84-
| getImageErrorCode | Get the error code for the image not found |
85-
| getErrorMessage | Error Message |
86-
| getImagePath | Get Path of the image |
1+
<?php
2+
/*
3+
Descrition : PHPZxing Example file
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+
36+
error_reporting(E_ALL);
37+
ini_set('display_errors', 1);
38+
39+
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "PHPZxingBase.php";
40+
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "PHPZxingInterface.php";
41+
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "PHPZxingDecoder.php";
42+
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "ZxingImage.php";
43+
require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "ZxingBarNotFound.php";
44+
45+
use PHPZxing\PHPZxingDecoder;
46+
47+
// Bar Code Found
48+
$decoder = new PHPZxingDecoder();
49+
$data = $decoder->decode('../images/Code128Barcode.jpg');
50+
if($data->isFound()) {
51+
$data->getImageValue();
52+
$data->getFormat();
53+
$data->getType();
54+
}
8755

56+
// Bar Code Not Found
57+
$decoder = new PHPZxingDecoder();
58+
$data = $decoder->decode('../images/no_bar_code_found.jpeg');
59+
if($data->isFound()) {
60+
$data->getImageValue();
61+
$data->getFormat();
62+
$data->getType();
63+
} else {
64+
echo "No Bar Code Found";
65+
}
8866

89-
Setting the configurations
90-
----------
91-
```php
92-
use PHPZxing\PHPZxingDecoder;
67+
// Bar Code Options
68+
$config = array(
69+
'try_harder' => true,
70+
'crop' => '100,200,300,300',
71+
);
72+
$decoder = new PHPZxingDecoder($config);
73+
$decodedArray = $decoder->decode('../images');
74+
if(is_array($decodedArray)){
75+
foreach ($decodedArray as $data) {
76+
if($data->isFound()) {
77+
print_r($data);
78+
}
79+
}
80+
}
9381

94-
$config = array(
95-
'try_harder' => true,
96-
);
97-
$decoder = new PHPZxingDecoder($config);
98-
$decodedArray = $decoder->decode('../images');
99-
if(is_array($decodedArray)){
82+
// Send Multiple Images
83+
$decoder = new PHPZxingDecoder();
84+
$imageArrays = array(
85+
'../images/Code128Barcode.jpg',
86+
'../images/Code39Barcode.jpg'
87+
);
88+
$decodedArray = $decoder->decode($imageArrays);
10089
foreach ($decodedArray as $data) {
101-
if($data->isFound()) {
90+
if($data instanceof PHPZxing\ZxingImage) {
10291
print_r($data);
92+
} else {
93+
echo "Bar Code cannot be read";
10394
}
10495
}
105-
}
106-
```
107-
108-
You can also use it with configurations. The Decoder has 3 configurations,
109-
110-
| Config Name | Function |
111-
| ------------- |--------------------------------------------------------------|
112-
| try_harder | If the image has bar/Qr code at unknown locations, then use this non mobile mode. |
113-
| multiple_bar_codes | If the image has multiple bar codes you want to read. |
114-
| crop | Crop the image and it will read only the cropped portion |
115-
116-
More Examples
117-
----------
118-
119-
You can pass array of images too,
120-
121-
```php
122-
use PHPZxing\PHPZxingDecoder;
123-
124-
$decoder = new PHPZxingDecoder();
125-
$imageArrays = array(
126-
'../images/Code128Barcode.jpg',
127-
'../images/Code39Barcode.jpg'
128-
);
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');
150-
print_r($decodedData);
151-
```
152-
153-
154-
Set Java Path
155-
----------
156-
If your java PATH is not set properly, the decoder will not work. You need to set path of java variable.
157-
158-
```php
159-
use PHPZxing\PHPZxingDecoder;
160-
161-
$decoder = new PHPZxingDecoder();
162-
$decoder->setJavaPath('/your/path/to/java');
163-
$decodedData = $decoder->decode('../images/Code128Barcode.jpg');
164-
print_r($decodedData);
165-
```
166-
167-
Where is my java located ?
168-
----------
169-
If you do not know the path to java, then you can use the following on *nix enviromnents
170-
```
171-
$ which java
172-
```
173-
174-
On Windows read the follwoing stackoverflow [Link](https://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line)
175-
176-
## Acknowledgments
177-
178-
* [Zxing library](https://github.com/zxing/zxing)
179-
180-
Contibution
181-
----------
182-
Please Contribute or suggest changes.
18396

97+
// Bar Code options for reading multiple bar codes in the same image
98+
$config = array(
99+
'try_harder' => true,
100+
'multiple_bar_codes' => true
101+
);
102+
$decoder = new PHPZxingDecoder($config);
103+
$decodedData = $decoder->decode('../images/multiple_bar_codes.jpg');
104+
print_r($decodedData);
105+
?>

0 commit comments

Comments
 (0)