diff options
author | dsiddharth2 <dsiddharth2@gmail.com> | 2018-04-28 21:59:07 +0530 |
---|---|---|
committer | dsiddharth2 <dsiddharth2@gmail.com> | 2018-04-28 21:59:07 +0530 |
commit | 7bf66280877860aed2f56119b463599f868637c5 (patch) | |
tree | 50959d2fb862baf4e6660fb43b6f08976add9560 /src | |
parent | 19c513109c0a5f5c78c54a4fada3b1c1ecbabf8e (diff) | |
download | php-zxing-7bf66280877860aed2f56119b463599f868637c5.zip php-zxing-7bf66280877860aed2f56119b463599f868637c5.tar.gz php-zxing-7bf66280877860aed2f56119b463599f868637c5.tar.bz2 |
Programmed the isFound method also updated the doc and examplesorigin/dev
Diffstat (limited to 'src')
-rw-r--r-- | src/PHPZxing/PHPZxingBase.php | 4 | ||||
-rw-r--r-- | src/PHPZxing/PHPZxingDecoder.php | 16 | ||||
-rw-r--r-- | src/PHPZxing/PHPZxingInterface.php | 39 | ||||
-rw-r--r-- | src/PHPZxing/ZxingBarNotFound.php | 12 | ||||
-rw-r--r-- | src/PHPZxing/ZxingImage.php | 12 | ||||
-rw-r--r-- | src/examples/example.php | 225 | ||||
-rw-r--r-- | src/images/no_bar_code_found.jpeg | bin | 0 -> 8571 bytes |
7 files changed, 255 insertions, 53 deletions
diff --git a/src/PHPZxing/PHPZxingBase.php b/src/PHPZxing/PHPZxingBase.php index 5d3cc79..fbad3fe 100644 --- a/src/PHPZxing/PHPZxingBase.php +++ b/src/PHPZxing/PHPZxingBase.php @@ -8,8 +8,8 @@ authors: - Siddharth Deshpande (dsiddharth2@gmail.com) ... * PHPZxing -* Version 1.0 -* Copyright (c) 2017 Siddharth Deshpande +* Version 1.0.1 +* Copyright (c) 2018 Siddharth Deshpande * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation diff --git a/src/PHPZxing/PHPZxingDecoder.php b/src/PHPZxing/PHPZxingDecoder.php index 7ab7804..42176da 100644 --- a/src/PHPZxing/PHPZxingDecoder.php +++ b/src/PHPZxing/PHPZxingDecoder.php @@ -15,8 +15,8 @@ Provides: PHPZxingDecoder - ... * PHPZxingDecoder -* Version 1.0 -* Copyright (c) 2017 Siddharth Deshpande +* Version 1.0.1 +* Copyright (c) 2018 Siddharth Deshpande * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -79,7 +79,11 @@ class PHPZxingDecoder extends PHPZxingBase { if(isset($config['crop']) && array_key_exists('crop', $config)) { $this->crop = strval($config['crop']); - } + } + + if(isset($config['returnAs']) && array_key_exists('returnAs', $config)) { + $this->returnAs = strval($config['returnAs']); + } } private function basePrepare() { @@ -170,6 +174,7 @@ class PHPZxingDecoder extends PHPZxingBase { $image[] = new ZxingImage($imagePath, $imageValue, $format, $type); } else if(preg_match('/No barcode found/', $singleLine)) { + $exploded = explode(" ", $singleLine); $imagePath = array_shift($exploded); $image[] = new ZxingBarNotFound($imagePath, 101, "No barcode found"); @@ -230,6 +235,11 @@ class PHPZxingDecoder extends PHPZxingBase { throw new \Exception("Is the java PATH set correctly ? Current Path set is : " . $this->getJavaPath()); } + // If the image is single then return the actual image + if(count($image) == 1) { + return current($image); + } + return $image; } catch(\Exception $e) { echo $e->getMessage(); diff --git a/src/PHPZxing/PHPZxingInterface.php b/src/PHPZxing/PHPZxingInterface.php new file mode 100644 index 0000000..d9568f8 --- /dev/null +++ b/src/PHPZxing/PHPZxingInterface.php @@ -0,0 +1,39 @@ +<?php +/* +Descrition : PHPZxingInterface interface that will have all the interface methods stored + +license: MIT-style + +authors: +- Siddharth Deshpande (dsiddharth2@gmail.com) +... +* PHPZxing +* Version 1.0.1 +* Copyright (c) 2018 Siddharth Deshpande +* +* Permission is hereby granted, free of charge, to any person +* obtaining a copy of this software and associated documentation +* files (the "Software"), to deal in the Software without +* restriction, including without limitation the rights to use, +* copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following +* conditions: +* +* The above copyright notice and this permission notice shall be +* included in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +*/ +namespace PHPZxing; + +interface PHPZxingInterface { + public function isFound(); +}
\ No newline at end of file diff --git a/src/PHPZxing/ZxingBarNotFound.php b/src/PHPZxing/ZxingBarNotFound.php index 608d743..4e7ddb7 100644 --- a/src/PHPZxing/ZxingBarNotFound.php +++ b/src/PHPZxing/ZxingBarNotFound.php @@ -9,8 +9,8 @@ authors: - Siddharth Deshpande (dsiddharth2@gmail.com) ... * PHPZxing -* Version 1.0 -* Copyright (c) 2017 Siddharth Deshpande +* Version 1.0.1 +* Copyright (c) 2018 Siddharth Deshpande * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -36,7 +36,9 @@ authors: namespace PHPZxing; -class ZxingBarNotFound { +use PHPZxing\PHPZxingInterface; + +class ZxingBarNotFound implements PHPZxingInterface { // Path of the image decoded private $imagePath = null; @@ -63,4 +65,8 @@ class ZxingBarNotFound { public function getErrorMessage() { return $this->message; } + + public function isFound() { + return false; + } }
\ No newline at end of file diff --git a/src/PHPZxing/ZxingImage.php b/src/PHPZxing/ZxingImage.php index d36f560..d49131b 100644 --- a/src/PHPZxing/ZxingImage.php +++ b/src/PHPZxing/ZxingImage.php @@ -8,8 +8,8 @@ authors: - Siddharth Deshpande (dsiddharth2@gmail.com) ... * PHPZxing -* Version 1.0 -* Copyright (c) 2017 Siddharth Deshpande +* Version 1.0.1 +* Copyright (c) 2018 Siddharth Deshpande * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation @@ -34,7 +34,9 @@ authors: */ namespace PHPZxing; -class ZxingImage { +use PHPZxing\PHPZxingInterface; + +class ZxingImage implements PHPZxingInterface { // Decoded Value from source private $imageValue = null; @@ -54,6 +56,10 @@ class ZxingImage { $this->imagePath = $imagePath; } + public function isFound() { + return true; + } + public function getImageValue() { return $this->imageValue; } diff --git a/src/examples/example.php b/src/examples/example.php index 5c979cb..13d2e8c 100644 --- a/src/examples/example.php +++ b/src/examples/example.php @@ -1,42 +1,183 @@ -<?php - error_reporting(E_ALL); - ini_set('display_errors', 1); - - require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "PHPZxingBase.php"; - require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "PHPZxingDecoder.php"; - require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "ZxingImage.php"; - require dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "PHPZxing" . DIRECTORY_SEPARATOR . "ZxingBarNotFound.php"; - - use PHPZxing\PHPZxingDecoder; - - $decoder = new PHPZxingDecoder(); - $decodedData = current($decoder->decode('../images/Code128Barcode.jpg')); - $decodedData->getImageValue(); - $decodedData->getFormat(); - $decodedData->getType(); - - $config = array( - 'try_harder' => true, - 'multiple_bar_codes' => true, - 'crop' => '100,200,300,300', - ); - $decoder = new PHPZxingDecoder($config); - $decodedData = $decoder->decode('../images/'); - print_r($decodedData); - - $decoder = new PHPZxingDecoder(); - $imageArrays = array( - '../images/Code128Barcode.jpg', - '../images/Code39Barcode.jpg' - ); - $decodedData = $decoder->decode($imageArrays); - print_r($decodedData); - - $config = array( - 'try_harder' => true, - 'multiple_bar_codes' => true - ); - $decoder = new PHPZxingDecoder($config); - $decodedData = $decoder->decode('../images/multiple_bar_codes.jpg'); - print_r($decodedData); -?>
\ No newline at end of file +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(); +$decodedData = $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. + diff --git a/src/images/no_bar_code_found.jpeg b/src/images/no_bar_code_found.jpeg Binary files differnew file mode 100644 index 0000000..ea08de6 --- /dev/null +++ b/src/images/no_bar_code_found.jpeg |