PHP解码 解析二维码

下载地址

链接:https://pan.baidu.com/s/1ZQ6STlrw4abTrTutdG_tzw 
提取码:cjh6

使用Zxing扩展库

1、文件下载好后,直接解压,结构如下,我们只需要lib这个文件夹

 2、将lib文件夹重命名为Zxing,然后打开Zxing目录下的QrReader.php文件,可以发现命名空间是Zxing

3、接下来就很简单了,把Zxing文件夹放到thnikphp的扩展目录extend里 

4、报错 Fatal error:: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in

报错原因:PHP内存不够
解决方法:在调用QrReader前,先用ini_set()方法修改内存限制大小

 

//修改php内存限制为1024M
ini_set('memory_limit','1024M');

5、报错 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、在代码里调用

//引用
use Zxing\QrReader;
//调用类库
$qrcode = new QrReader("二维码图片路径"); 
$content = $qrcode->text();

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值