javascript 从php中的图像读取/解码条形码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31443370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:47:49  来源:igfitidea点击:

Read/decode bar codes from an image in php

javascriptphphtmlbarcode

提问by Ben Paton

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.

我正在考虑构建一个 13 位 EAN 条形码扫描仪,它可以在移动设备上的网络上运行,并将使用设备摄像头拍摄条形码图像以进行扫描和解码。我不想通过本机应用程序来做到这一点,因为我更愿意将这部分作为我本机网站搜索体验的一部分。例如,手机上的网站访问者将被提示扫描条形码,而无需打开应用程序。

This script works well on desktop https://github.com/EddieLa/JOBand 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

此脚本在桌面https://github.com/EddieLa/JOB上运行良好,并使用 Navigator.getUserMedia 属性在 JavaScript 中执行所有这些操作,但对 Android 的支持才刚刚开始,对 IOS 的支持尚不存在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.

所以我想知道我是否可以改为支持移动设备,这是我尝试做的重点,而不是在浏览器中读取条形码,拍下条形码的照片,然后通过以下方式将其发送到服务器Ajax,让服务器解码图像并将响应发送回网页。

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?

通过这种方法,我知道有可以读取条形码的 python 脚本,例如https://pypi.python.org/pypi/zbar,但是是否有 PHP 等价物?

回答by Heitor

From my experience ZBARis 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 execcommand. Windows example:

根据我的经验,ZBAR是我经过长期研究和许多其他可用选项(包括 BarBara)尝试后发现的最好的。只需下载并安装 ZBar(取决于您的操作系统)并运行 PHP 的exec命令。窗口示例:

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!

我在 10.000 多张多种格式(jpg、gif、png 和 bmp)的大图像中运行 ZBar,它在大约 70% 的图像中成功检测到条形码。它可以读取多种条码格式,包括 EAN-13、QR-Code 等,也可以读取同一图像中的多个条码。绝对值得一试!

回答by Another Person

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

使用BarBara条码库:

官网:http: //sourceforge.net/projects/barbara

下载php源代码: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);

回答by Scurt Mcgurt

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!

通过使用 php 库 imagick 结合 zbar 和作曲家库“robbiep/zbar-qrdecoder”:“^2.0”,我能够获得更好的条形码读取结果。代码如下,去除了可能在条形码后面阻碍其识别的灰度。干杯!

        $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);