Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解
一、下載PHP版本的Zxing擴(kuò)展庫(kù)
下載地址:https://github.com/khanamiryan/php-qrcode-detector-decoder
二、使用Zxing擴(kuò)展庫(kù)
1、文件下載好后,直接解壓,結(jié)構(gòu)如下,我們只需要lib這個(gè)文件夾

2、將lib文件夾重命名為Zxing,然后打開(kāi)Zxing目錄下的QrReader.php文件,可以發(fā)現(xiàn)命名空間是Zxing

3、接下來(lái)就很簡(jiǎn)單了,把Zxing文件夾放到thnikphp的擴(kuò)展目錄extend里

4、報(bào)錯(cuò) Fatal error:: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in
報(bào)錯(cuò)原因:PHP內(nèi)存不夠
解決方法:在調(diào)用QrReader前,先用ini_set()方法修改內(nèi)存限制大小
//修改php內(nèi)存限制為1024M
ini_set('memory_limit','1024M');
5、報(bào)錯(cuò) Call to undefined function Zxing\Common\fill_array()
解決方法:修改Zxing目錄的QrReader.php文件,載入common/customFunctions.php文件,如下:
<?php
namespace Zxing;
use Zxing\Common\HybridBinarizer;
use Zxing\Qrcode\QRCodeReader;
include_once('common/customFunctions.php');
final class QrReader
{
}
QrReader.php完整代碼:
<?php
namespace Zxing;
use Zxing\Common\HybridBinarizer;
use Zxing\Qrcode\QRCodeReader;
include_once('common/customFunctions.php');
final class QrReader
{
const SOURCE_TYPE_FILE = 'file';
const SOURCE_TYPE_BLOB = 'blob';
const SOURCE_TYPE_RESOURCE = 'resource';
private $bitmap;
private $reader;
private $result;
public function __construct($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true)
{
if (!in_array($sourceType, [
self::SOURCE_TYPE_FILE,
self::SOURCE_TYPE_BLOB,
self::SOURCE_TYPE_RESOURCE,
], true)) {
throw new \InvalidArgumentException('Invalid image source.');
}
$im = null;
switch ($sourceType) {
case QrReader::SOURCE_TYPE_FILE:
if ($useImagickIfAvailable && extension_loaded('imagick')) {
$im = new \Imagick();
$im->readImage($imgSource);
} else {
$image = file_get_contents($imgSource);
$im = imagecreatefromstring($image);
}
break;
case QrReader::SOURCE_TYPE_BLOB:
if ($useImagickIfAvailable && extension_loaded('imagick')) {
$im = new \Imagick();
$im->readImageBlob($imgSource);
} else {
$im = imagecreatefromstring($imgSource);
}
break;
case QrReader::SOURCE_TYPE_RESOURCE:
$im = $imgSource;
if ($useImagickIfAvailable && extension_loaded('imagick')) {
$useImagickIfAvailable = true;
} else {
$useImagickIfAvailable = false;
}
break;
}
if ($useImagickIfAvailable && extension_loaded('imagick')) {
if (!$im instanceof \Imagick) {
throw new \InvalidArgumentException('Invalid image source.');
}
$width = $im->getImageWidth();
$height = $im->getImageHeight();
$source = new IMagickLuminanceSource($im, $width, $height);
} else {
if (!is_resource($im)) {
throw new \InvalidArgumentException('Invalid image source.');
}
$width = imagesx($im);
$height = imagesy($im);
$source = new GDLuminanceSource($im, $width, $height);
}
$histo = new HybridBinarizer($source);
$this->bitmap = new BinaryBitmap($histo);
$this->reader = new QRCodeReader();
}
public function decode()
{
try {
$this->result = $this->reader->decode($this->bitmap);
} catch (NotFoundException $er) {
$this->result = false;
} catch (FormatException $er) {
$this->result = false;
} catch (ChecksumException $er) {
$this->result = false;
}
}
public function text()
{
$this->decode();
if (method_exists($this->result, 'toString')) {
return $this->result->toString();
}
return $this->result;
}
public function getResult()
{
return $this->result;
}
}
6、在代碼里調(diào)用
//引用
use Zxing\QrReader;
//調(diào)用類庫(kù)
$qrcode = new QrReader("二維碼圖片路徑");
$content = $qrcode->text();
到此這篇關(guān)于Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容圖文講解的文章就介紹到這了,更多相關(guān)Thinkphp使用Zxing擴(kuò)展庫(kù)解析二維碼內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用php+mysql來(lái)做一個(gè)功能強(qiáng)大的在線計(jì)算器
有天在努力的搜索計(jì)算器,發(fā)現(xiàn)都是JavaScript,而且要一個(gè)個(gè)地點(diǎn)擊,并且不能記錄,輸入計(jì)算式子時(shí)容易出錯(cuò),于是就想了想該怎樣才能讓它好用點(diǎn)呢,能夠用鍵盤直接輸入。2010-10-10
php+redis實(shí)現(xiàn)多臺(tái)服務(wù)器內(nèi)網(wǎng)存儲(chǔ)session并讀取示例
這篇文章主要介紹了php+redis實(shí)現(xiàn)多臺(tái)服務(wù)器內(nèi)網(wǎng)存儲(chǔ)session并讀取示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解
今天小編就為大家分享一篇laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Laravel框架分頁(yè)實(shí)現(xiàn)方法分析
這篇文章主要介紹了Laravel框架分頁(yè)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Laravel框架實(shí)現(xiàn)分頁(yè)功能的核心代碼及其相關(guān)原理,需要的朋友可以參考下2018-06-06

