8

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.com/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.com/#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.org/pypi/zbar, however are there PHP equivalents to this?

Ben Paton
  • 1,432
  • 9
  • 35
  • 59

3 Answers3

9

From 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 command. 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!

Heitor
  • 683
  • 2
  • 12
  • 26
  • I didn't down vote. However, 70% is pretty terrible accuracy. – James Apr 16 '18 at 14:38
  • @James not necessarily dude, 70% 10,000+ images scanned over old papers with adhesives and stuff over the barcodes was an excellent rate in my case! They were old DVD cases. If you get perfectly scanned images the rate will be close to 100% if you follow my answer above! – Heitor Apr 17 '18 at 21:48
  • 1
    Okay, I didn't realize the bar codes you were scanning were damaged/half-covered. – James Apr 20 '18 at 11:43
  • 1
    However, I have a code39 barcode produced from other code I have as a PDF (so crystal-clear) which I produced a JPG image from. It cannot find the bar code at all in the image, it says. Though it did work on a QR Code I have. Other bar code scanners can read the bar code just fine though. – James Apr 20 '18 at 11:49
  • 1
    Well, let me take that back. I generated a new JPG, leaving *ONLY* the bar code (no white-space around it), and it was able to read it then. I then expanded the image again, filling with just a white background, and now it seems to be working just fine. – James Apr 20 '18 at 11:57
  • In my case it was EAN13 mostly. – Heitor Apr 21 '18 at 12:34
  • I'm currently having issues with it reading `any` barcode from an actual photograph. The photos are clear, so I'm not sure what the issue is. :/ – James May 11 '18 at 13:25
  • Maybe is about the angle the photos ware taken. Scan them if possible. – Heitor May 13 '18 at 22:30
1

Use BarBara Bar Code Library:

official site: http://sourceforge.net/projects/barbara

download php source code: http://sourceforge.net/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);
Heitor
  • 683
  • 2
  • 12
  • 26
0

I was able to get much better results for reading barcodes by using the php library imagick combined with zbar and the composer 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);