巅云php学苑
近期车展
最新发布
快速导航

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

网络 / https / 2022-04-06 21:01
visits visits 718 collect - report - QRcode

Basicinformation

content

一、下载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 ZxingCommonfill_array()

解决方法:修改Zxing目录的QrReader.php文件,载入common/customFunctions.php文件,如下:

<?php
namespace Zxing;
 
use ZxingCommonHybridBinarizer;
use ZxingQrcodeQRCodeReader;
include_once("common/customFunctions.php");
 
final class QrReader
{
}

QrReader.php完整代码:

<?php
namespace Zxing;
 
use ZxingCommonHybridBinarizer;
use ZxingQrcodeQRCodeReader;
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 ZxingQrReader;
//调用类库
$qrcode = new QrReader("二维码图片路径"); 
$content = $qrcode->text();

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

notmore

Users comments(0)

rate100%
  • notmore
+ more
  • contacts:
  • area:
  • tel: total(2007)twigs area:unknown

联系我们

合作或咨询可通过如下方式:

QQ/微信:123456

网址:www.xxxx.cn

微信公众号:车展门票

关于本站

「车展网www.xxxxxxx.cn」是一个集全国各地品牌经销车商的平台,让您在所在城市,第一时间收到车展的最新消息,让您的试驾、选车、购车、贷车一步到位,更多优惠直达客户,无需东奔西走,带上中意的车回家,让您省钱,更省心。

Copyright 车展网 www.xxxx.cn Reserved渝ICP备xxxxxxxx号

关注我们