I am look at building a 13 digit EAN bar code scanner which works on the web on mobile devices and will use the devices camera to take an image of bar code to scan and decode. I'm not trying to do this through a native app as I would prefer to make this part of my native website search experience. E.g. website visitors on a mobile will be prompted to scan a bar code without having to open an app.
This script works well on desktop and uses the Navigator.getUserMedia property to do all this in JavaScript however support in Android is only just starting and support on IOS is non-existent
So I am wondering if I can instead, to support mobile devices which is the whole point of what I am trying to do, rather than read the bar code in the browser, take a picture of the bar code, send this to a server via Ajax, have the server decode the image and send the response back to webpage.
With this approach I know there are python scripts which can read a bar code, such as , however are there PHP equivalents to this?
I am look at building a 13 digit EAN bar code scanner which works on the web on mobile devices and will use the devices camera to take an image of bar code to scan and decode. I'm not trying to do this through a native app as I would prefer to make this part of my native website search experience. E.g. website visitors on a mobile will be prompted to scan a bar code without having to open an app.
This script works well on desktop https://github./EddieLa/JOB and uses the Navigator.getUserMedia property to do all this in JavaScript however support in Android is only just starting and support on IOS is non-existent http://caniuse./#feat=stream
So I am wondering if I can instead, to support mobile devices which is the whole point of what I am trying to do, rather than read the bar code in the browser, take a picture of the bar code, send this to a server via Ajax, have the server decode the image and send the response back to webpage.
With this approach I know there are python scripts which can read a bar code, such as https://pypi.python/pypi/zbar, however are there PHP equivalents to this?
Share Improve this question asked Jul 16, 2015 at 0:17 Ben PatonBen Paton 1,4429 gold badges37 silver badges60 bronze badges 1- This might be of some help – Darren Commented Jul 16, 2015 at 0:30
3 Answers
Reset to default 9From my experience ZBAR is just the best one I found after a long research and many tries with other free options available, including BarBara. Just download and install ZBar (depending on your OS) and run PHP's exec
mand. Windows example:
exec('C:\\"Program Files (x86)"\\ZBar\\bin\\zbarimg -q C:\\path\\img.jpg', $result);
print_r($result);
I run ZBar in 10.000+ big images in many formats (jpg, gif, png & bmp) and it detected the barcodes successfully in about 70% of them. It can read many barcode formats, including EAN-13, QR-Code and others, and can read multiple barcodes in the same image as well. Defenitely worth a try!
Use BarBara Bar Code Library:
official site: http://sourceforge/projects/barbara
download php source code: http://sourceforge/projects/barbara/files/BarBara%20Source/PHP5/barbara.zip/download
Testing:
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once "barcode.php";
$bc = new barcode;
$scanner = new BarScan;
$bc->load("barcode.js");
echo "Dictionary Loaded..";
$scanner->Codecs = $bc;
echo "Test";
//Manually set code type
$scanner->CodeType = $bc->code39;
$img = new Imagick("test/code-25.gif");
echo "Image Loaded...";
echo "<br />Decoded: " . $scanner->Scan($img, 0, 20, 2048, 0);
I was able to get much better results for reading barcodes by using the php library imagick bined with zbar and the poser library "robbiep/zbar-qrdecoder": "^2.0". The code is as follow and removes grey scale that could be behind barcode and hinder its recognition. Cheers!
$blankAndWhiteFileName = time()."-".rand();
$imagick = new Imagick($file);
$imagick->modulateImage(100, 0, 100);
$imagick->whiteThresholdImage('#a9a9a9');
$imagick->contrastImage(1);
$imagick->setImageFormat('jpg');
$tempFilename = tempnam('/tmp', $blankAndWhiteFileName);
$blackAndWhite = $imagick->writeImage($tempFilename);
$ZbarDecoder = new RobbieP\ZbarQrdecoder\ZbarDecoder();
$result = $ZbarDecoder->make($tempFilename);
unlink($tempFilename);