summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 60b9dbab1606874c62a9763fe5bd12939e2d49ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
PHPZxing - Wrapper for Zxing Java Library
===========================================
PHPZxing is a small php wrapper that uses the Zxing library to Create and read Barcodes.
Under the hood it still uses the [Zxing library](https://github.com/zxing/zxing) to encode and decode data.

Install using composer
--------------------

```json
{  
    "require": {
        "dsiddharth2/php-zxing": "1.0.1"
    }  
}
```

Note
--------------------
* Only Decoder is programmed right now. Needs programming of Encoder.
* The Default location of java that is configured is /usr/bin/java

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

Basic Usage
----------
```php
use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('../images/Code128Barcode.jpg');
if($data->isFound()) {
    $data->getImageValue();
    $data->getFormat();
    $data->getType();        
}
```

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.

Checking for existence of Barcode
----------
The Existance of bar code can be found using the functoin isFound()

```php
use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('../images/Code128Barcode.jpg');
if($data->isFound()) {
    $data->getImageValue();
    $data->getFormat();
    $data->getType();        
}
```

You can also check using the instanceof object,
```php
use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$data           = $decoder->decode('../images/Code128Barcode.jpg');
if($data instanceof PHPZxing\ZxingImage) {
    $data->getImageValue();
    $data->getFormat();
    $data->getType();
}
```
The Public methods that we can use in PHPZxing\ZxingImage are,

| Method Name       | Function                                                       |
| -------------     |--------------------------------------------------------------|
| getImageValue     | Get the value decoded in the image passed                      |
| getFormat         | Get the format of the image that is encoded, example : CODE_39 |
| getType           | Get the type of the image decoded, example : URL, TEXT etc     |
| getImagePath      | Get Path of the image                                          |

The Public methods that we can use in PHPZxing\ZxingImage are,

| Method Name           | Function                                                       |
| -------------         |--------------------------------------------------------------|
| getImageErrorCode     | Get the error code for the image not found                     |
| getErrorMessage       | Error Message                                                  |
| getImagePath          | Get Path of the image                                          |


Setting the configurations
----------
```php
use PHPZxing\PHPZxingDecoder;

$config = array(
    'try_harder'            => true,
);
$decoder        = new PHPZxingDecoder($config);
$decodedArray   = $decoder->decode('../images');
if(is_array($decodedArray)){
    foreach ($decodedArray as $data) {
        if($data->isFound()) {
            print_r($data);
        }
    }
}
```

You can also use it with configurations. The Decoder has 3 configurations,

| Config Name           | Function                                                       |
| -------------         |--------------------------------------------------------------|
| try_harder            | If the image has bar/Qr code at unknown locations, then use this non mobile mode. |
| multiple_bar_codes    | If the image has multiple bar codes you want to read. |
| crop                  | Crop the image and it will read only the cropped portion |

More Examples
----------

You can pass array of images too,

```php
use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$imageArrays = array(
    '../images/Code128Barcode.jpg',
    '../images/Code39Barcode.jpg'
);
$decodedArray  = $decoder->decode($imageArrays);
foreach ($decodedArray as $data) {
    if($data instanceof PHPZxing\ZxingImage) {
        print_r($data);
    } else {
        echo "Bar Code cannot be read";
    }
}
```

Reading multiple bar codes,

```php
use PHPZxing\PHPZxingDecoder;

$config = array(
    'try_harder' => true,
    'multiple_bar_codes' => true
);
$decoder        = new PHPZxingDecoder($config);
$decodedData    = $decoder->decode('../images/multiple_bar_codes.jpg');
print_r($decodedData);
```


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

```php
use PHPZxing\PHPZxingDecoder;

$decoder        = new PHPZxingDecoder();
$decoder->setJavaPath('/your/path/to/java');
$decodedData    = $decoder->decode('../images/Code128Barcode.jpg');
print_r($decodedData);
```

Where is my java located ?
----------
If you do not know the path to java, then you can use the following on *nix enviromnents
```
$ which java
```

On Windows read the follwoing stackoverflow [Link](https://stackoverflow.com/questions/304319/is-there-an-equivalent-of-which-on-the-windows-command-line)

## Acknowledgments

* [Zxing library](https://github.com/zxing/zxing)

Contibution
----------
Please Contribute or suggest changes.