Wallogit.com
2017 © Pedro Peláez
Wrapper for zxing using php
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 to encode and decode data., (*1)
{
"require": {
"dsiddharth2/php-zxing": "1.0.3"
}
}
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., (*2)
The Existance of bar code can be found using the functoin isFound(), (*3)
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,, (*4)
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,, (*5)
| 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,, (*6)
| Method Name | Function |
|---|---|
| getImageErrorCode | Get the error code for the image not found |
| getErrorMessage | Error Message |
| getImagePath | Get Path of the image |
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 4 configurations,, (*7)
| 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 |
| possible_formats | List of formats to decode, where format is any value in BarcodeFormat |
You can pass array of images too,, (*8)
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,, (*9)
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);
If your java PATH is not set properly, the decoder will not work. You need to set path of java variable., (*10)
use PHPZxing\PHPZxingDecoder;
$decoder = new PHPZxingDecoder();
$decoder->setJavaPath('/your/path/to/java');
$decodedData = $decoder->decode('../images/Code128Barcode.jpg');
print_r($decodedData);
If you do not know the path to java, then you can use the following on *nix enviromnents, (*11)
$ which java
On Windows environment,, (*12)
> where java
For more info, on Windows read the follwoing stackoverflow Link, (*13)
Please Contribute or suggest changes., (*14)