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扩展库解析二维码内容内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。