• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Thinkphp使用Zxing扩展库解析二维码内容图文讲解

    一、下载PHP版本的Zxing扩展库

    下载地址:https://github.com/khanamiryan/php-qrcode-detector-decoder

    二、使用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();

    到此这篇关于Thinkphp使用Zxing扩展库解析二维码内容图文讲解的文章就介绍到这了,更多相关Thinkphp使用Zxing扩展库解析二维码内容内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • php+laravel 扫码二维码签到功能
    • PHP实现一个二维码同时支持支付宝和微信支付的示例
    • PHP基于phpqrcode类生成二维码的方法示例详解
    • PHP基于phpqrcode类库生成二维码过程解析
    • thinkphp3.2框架集成QRcode生成二维码的方法分析
    • PHP生成二维码与识别二维码的方法详解【附源码下载】
    • php生成二维码不保存服务器还有下载功能的实现代码
    • PHP二维码的生成与识别案例
    上一篇:WordPress网站访问慢解决方案细图文教程
    下一篇:Thinkphp5.1获取项目根目录以及子目录路径的方法实例讲解
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    Thinkphp使用Zxing扩展库解析二维码内容图文讲解 Thinkphp,使用,Zxing,扩展,库,