PHP二维码识别

166次阅读
没有评论

原文链接:https://blog.csdn.net/mrwangweijin/article/details/79145677

php-qrcode-detector-decoder 的扩展。

// 在控台执行安装
composer require khanamiryan/qrcode-detector-decoder

// 使用方法
require __DIR__ . "/vendor/autoload.php";
$qrcode = new QrReader('path/to_image');
$text = $qrcode->text(); // 返回二维码的内容 

php-qrcode-detector-decoder 是基于 Zxing library(谷歌推出的用来识别多种格式条形码的开源项目)而实现的,我也顺利的找到了 Zxing library 的 GitHub 地址,其中包括了很多语言的二维码识别,有兴趣的可以自己去看。下面来说我们用到的 php-zxing 扩展。

相关 GitHub 地址

https://github.com/khanamiryan/php-qrcode-detector-decoder

https://github.com/zxing/zxing

// 在 composer 文件中引入扩展,然后 composer update
{ 
"require": {"dsiddharth2/php-zxing": "dev-master"} 
}

// php 中的代码
use PHPZxing\PHPZxingDecoder;

$config = array(
'try_harder' => true, // 当不知道二维码的位置是设置为 true
'multiple_bar_codes' => true, // 当要识别多张二维码是设置为 true
'crop' => '100,200,300,300', // 设置二维码的大概位置
);
$decoder = new PHPZxingDecoder($config);
$decoder->setJavaPath('/your/path/to/java'); // 设置 jdk 的安装路径,该扩展是居于 java 的,所以需要 jdk。如果设置了 jdk 的环境变量则无需设置

$decodedData = current($decoder->decode('../images/Code128Barcode.jpg')); // 路径需要时绝对路径或相对路径,不能是 url
/**
* 返回的对象类型
* 识别成功时返回 ZxingImage 对象 包括
* getImageValue 二维码的内容
* getFormat 编码图像的格式
* getType 获取解码图像的类型,例如:URL,TEXT 等
* getImagePath 获取图像的路径
* 图片中没有识别的二维码时返回 ZxingBarNotFound 对象 包括
* getImageErrorCode 获取未找到图像的错误代码
* getErrorMessage 错误信息
* getImagePath 获取图像的路径
/**
// 例如
$decodedData->getImageValue(); // 二维码的内容 
正文完
有偿技术支持加微信
post-qrcode
 
评论(没有评论)
验证码