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

2022-04-15 0 467

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

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

二、使用Zxing扩展库

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

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

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

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

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

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

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

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 PHP编程 Thinkphp使用Zxing扩展库解析二维码内容图文讲解 https://www.niceym.com/17506.html