如何使用 Windows COM 对象制作 PHP 条形码阅读器?
本文档介绍了如何使用 PHP 和 Windows 组件对象模型 (COM) 实现一个在线条码读取器。
引言
Dynamsoft 条码读取器 for Windows 包含 C/C++、DotNET 和 COM 的动态链接库。 如果您想使用 Python、PHP、Java、Ruby 等高级编程语言来使用 Dynamsoft 条码读取器创建条码读取器应用程序,您可以创建一个 C/C++ 包装器作为扩展,或者使用 COM 对象。 要在 Windows 上创建 PHP 扩展,您可以参考 Dynamsoft GitHub 仓库 中的示例代码。 然而,在创建 PHP 扩展时,许多开发人员都遇到了 PHP 版本号不匹配的问题。 因此,在 PHP 中使用 COM 似乎更方便。
支持的 1D/2D 条码类型
- Code 39, Code 93, Code 128, Codabar, Interleaved 2 of 5, EAN-8, EAN-13, UPC-A, UPC-E, Industrial 2 of 5
- QRCode
- DataMatrix
- PDF417
支持的图像类型
- BMP
- JPEG
- PNG
- GIF
- TIFF
环境
- Windows
- PHP 5.5 及以上版本
COM 注册
Dynamsoft 条码读取器的安装程序将自动注册 COM DLL。 如果您想直接将 DLL 分发到其他 Windows PC,请不要忘记手动注册它们
regsvr32 DynamsoftBarcodeReaderCtrlx86.dll
regsvr32 DynamsoftBarcodeReaderCtrlx64.dll
取消注册 COM
regsvr32 /u DynamsoftBarcodeReaderCtrlx86.dll
regsvr32 /u DynamsoftBarcodeReaderCtrlx64.dll
使用 JavaScript 和 PHP 上传文件
使用 FormData
接口包装选定的文件,并使用 XMLHttpRequest
发送它。
var fd = new FormData();
var file = document.getElementById('fileToUpload').files[0];
fd.append('fileToUpload', file);
fd.append('uploadFlag', uploadFlag);
fd.append('barcodetype', getSelectedBarcodeTypes());
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", uploadComplete, false);
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "readbarcode.php");
xhr.send(fd);
将临时上传的文件保存到文件夹
// get current working directory
$root = getcwd();
// tmp dir for receiving uploaded barcode images
$tmpDir = $root . "/uploads/";
if (!file_exists($tmpDir)) {
mkdir($tmpDir);
}
$target_file = $tmpDir . $tmpname;
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
readBarcode($target_file, $btype);
unlink($target_file);
}
else {
echo "Fail to upload file.";
}
使用 COM 对象进行 PHP 条码读取
在 PHP 中创建 COM 对象
$this->m_reader = new COM("DBRCtrl.BarcodeReader");
$this->m_option = new COM("DBRCtrl.ReaderOptions");
初始化许可证
$this->m_reader->InitLicense($key);
设置条码格式和读取器选项
$this->m_option->BarcodeFormats = $format;
$this->m_reader->ReaderOptions =$this->m_option;
解码条码图像
$this->m_reader->DecodeFile($filename);
获取结果
$this->m_resultArray = $this->m_reader->Barcodes;
在 Nginx 上运行 PHP 条码读取器
首先,您需要打开 php.ini 来确认 PHP extension%php%\ext\php_com_dotnet.dll 是否已启用
extension=php_com_dotnet.dll
打开 %nginx%\confg\ nginx.conf 并取消注释 PHP 配置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
指定 SCRIPT_FILENAME
。 例如
fastcgi_param SCRIPT_FILENAME %nginx%/html/$fastcgi_script_name;
将所有 PHP 脚本复制到 %nginx%/html/。
运行 Nginx 和 PHP CGI
%nginx%\nginx
%php%\php-cgi.exe -b 127.0.0.1:9000 -c %php%\php.ini
访问 localhost:8080/index.php 并尝试读取条码图像
出现错误:413 请求实体过大! 上传的图像大小约为 20M。 如果上传大文件,您也可能会遇到此问题。 为了避免此问题,您可以更改 nginx.conf 中的默认实体大小
client_max_body_size 30M;
再次尝试读取条码图像
仍然失败! 根据警告,您还应该更改 PHP 限制。 打开 php.ini 并编辑以下行
post_max_size 30M;
再试一次
现在您可以开始动手实践了。 有关 Dynamsoft 条码读取器 的更多信息,请联系 support@dynamsoft.com。