当前位置:首页 > CMS教程 > Thinkphp > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-04-17 09:17:17 浏览: 评论:0 

这篇文章主要介绍了Thinkphp使用Zxing扩展库解析二维码内容图文讲解,图文步骤讲解的很清晰,有需要的同学可以跟着小编一起来学习下。

一、下载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文件,如下:

  1. <?php 
  2. namespace Zxing; 
  3.    
  4. use Zxing\Common\HybridBinarizer; 
  5. use Zxing\Qrcode\QRCodeReader; 
  6. include_once('common/customFunctions.php'); 
  7.    
  8. final class QrReader 
  9. { 
  10. } 

QrReader.php完整代码:

  1. <?php 
  2. namespace Zxing; 
  3.    
  4. use Zxing\Common\HybridBinarizer; 
  5. use Zxing\Qrcode\QRCodeReader; 
  6. include_once('common/customFunctions.php'); 
  7.    
  8. final class QrReader 
  9. { 
  10.  const SOURCE_TYPE_FILE  = 'file'; 
  11.  const SOURCE_TYPE_BLOB  = 'blob'; 
  12.  const SOURCE_TYPE_RESOURCE = 'resource'; 
  13.    
  14.  private $bitmap; 
  15.  private $reader; 
  16.  private $result; 
  17.    
  18.  public function __construct($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true) 
  19.  { 
  20.   if (!in_array($sourceType, [ 
  21.    self::SOURCE_TYPE_FILE, 
  22.    self::SOURCE_TYPE_BLOB, 
  23.    self::SOURCE_TYPE_RESOURCE, 
  24.   ], true)) { 
  25.    throw new \InvalidArgumentException('Invalid image source.'); 
  26.   } 
  27.   $im = null; 
  28.   switch ($sourceType) { 
  29.    case QrReader::SOURCE_TYPE_FILE: 
  30.     if ($useImagickIfAvailable && extension_loaded('imagick')) { 
  31.      $im = new \Imagick(); 
  32.      $im->readImage($imgSource); 
  33.     } else { 
  34.      $image = file_get_contents($imgSource); 
  35.      $im = imagecreatefromstring($image); 
  36.     } 
  37.     break; 
  38.    
  39.    case QrReader::SOURCE_TYPE_BLOB: 
  40.     if ($useImagickIfAvailable && extension_loaded('imagick')) { 
  41.      $im = new \Imagick(); 
  42.      $im->readImageBlob($imgSource); 
  43.     } else { 
  44.      $im = imagecreatefromstring($imgSource); 
  45.     } 
  46.     break; 
  47.    
  48.    case QrReader::SOURCE_TYPE_RESOURCE: 
  49.     $im = $imgSource; 
  50.     if ($useImagickIfAvailable && extension_loaded('imagick')) { 
  51.      $useImagickIfAvailable = true; 
  52.     } else { 
  53.      $useImagickIfAvailable = false; 
  54.     } 
  55.     break; 
  56.   } 
  57.   if ($useImagickIfAvailable && extension_loaded('imagick')) { 
  58.    if (!$im instanceof \Imagick) { 
  59.     throw new \InvalidArgumentException('Invalid image source.'); 
  60.    } 
  61.    $width = $im->getImageWidth(); 
  62.    $height = $im->getImageHeight(); 
  63.    $source = new IMagickLuminanceSource($im, $width, $height); 
  64.   } else { 
  65.    if (!is_resource($im)) { 
  66.     throw new \InvalidArgumentException('Invalid image source.'); 
  67.    } 
  68.    $width = imagesx($im); 
  69.    $height = imagesy($im); 
  70.    $source = new GDLuminanceSource($im, $width, $height); 
  71.   } 
  72.   $histo  = new HybridBinarizer($source); 
  73.   $this->bitmap = new BinaryBitmap($histo); 
  74.   $this->reader = new QRCodeReader(); 
  75.  } 
  76.    
  77.  public function decode() 
  78.  { 
  79.   try { 
  80.    $this->result = $this->reader->decode($this->bitmap); 
  81.   } catch (NotFoundException $er) { 
  82.    $this->result = false; 
  83.   } catch (FormatException $er) { 
  84.    $this->result = false; 
  85.   } catch (ChecksumException $er) { 
  86.    $this->result = false; 
  87.   } 
  88.  } 
  89.    
  90.  public function text() 
  91.  { 
  92.   $this->decode(); 
  93.    
  94.   if (method_exists($this->result, 'toString')) { 
  95.    return $this->result->toString(); 
  96.   } 
  97.    
  98.   return $this->result; 
  99.  } 
  100.    
  101.  public function getResult() 
  102.  { 
  103.   return $this->result; 
  104.  } 
  105. } 

6、在代码里调用

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

Tags: Thinkphp扩展库解析二维码 Zxing

分享到: