Blogger Information
Blog 35
fans 0
comment 0
visits 29410
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Thinkphp使用Zxing扩展库解析二维码内容图文讲解
P粉526161432
Original
909 people have browsed it
  1. 这篇文章主要介绍了Thinkphp使用Zxing扩展库解析二维码内容图文讲解,图文步骤讲解的很清晰,有需要的同学可以跟着小编一起来学习下
  1. 一、下载PHP版本的Zxing扩展库
  1. 下载地址:https://github.com/khanamiryan/php-qrcode-detector-decoder
  1. 二、使用Zxing扩展库
  1. 1、文件下载好后,直接解压,结构如下,我们只需要lib这个文件夹
  2. 2、将lib文件夹重命名为Zxing,然后打开Zxing目录下的QrReader.php文件,可以发现命名空间是Zxing
  1. 3、接下来就很简单了,把Zxing文件夹放到thnikphp的扩展目录extend
  1. 、报错 Fatal error:: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in
  2. 报错原因:PHP内存不够
  3. 解决方法:在调用QrReader前,先用ini_set()方法修改内存限制大小
  1. 、报错 Fatal error:: Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in
  2. 报错原因:PHP内存不够
  3. 解决方法:在调用QrReader前,先用ini_set()方法修改内存限制大小
  1. //修改php内存限制为1024M
  2. ini_set('memory_limit','1024M');
  1. 5、报错 Call to undefined function Zxing\Common\fill_array()
  2. 解决方法:修改Zxing目录的QrReader.php文件,载入common/customFunctions.php文件,如下:
  1. <?php
  2. namespace Zxing;
  3. use Zxing\Common\HybridBinarizer;
  4. use Zxing\Qrcode\QRCodeReader;
  5. include_once('common/customFunctions.php');
  6. final class QrReader
  7. {
  8. }
  1. QrReader.php完整代码:
  1. <?php
  2. namespace Zxing;
  3. use Zxing\Common\HybridBinarizer;
  4. use Zxing\Qrcode\QRCodeReader;
  5. include_once('common/customFunctions.php');
  6. final class QrReader
  7. {
  8. const SOURCE_TYPE_FILE = 'file';
  9. const SOURCE_TYPE_BLOB = 'blob';
  10. const SOURCE_TYPE_RESOURCE = 'resource';
  11. private $bitmap;
  12. private $reader;
  13. private $result;
  14. public function __construct($imgSource, $sourceType = QrReader::SOURCE_TYPE_FILE, $useImagickIfAvailable = true)
  15. {
  16. if (!in_array($sourceType, [
  17. self::SOURCE_TYPE_FILE,
  18. self::SOURCE_TYPE_BLOB,
  19. self::SOURCE_TYPE_RESOURCE,
  20. ], true)) {
  21. throw new \InvalidArgumentException('Invalid image source.');
  22. }
  23. $im = null;
  24. switch ($sourceType) {
  25. case QrReader::SOURCE_TYPE_FILE:
  26. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  27. $im = new \Imagick();
  28. $im->readImage($imgSource);
  29. } else {
  30. $image = file_get_contents($imgSource);
  31. $im = imagecreatefromstring($image);
  32. }
  33. break;
  34. case QrReader::SOURCE_TYPE_BLOB:
  35. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  36. $im = new \Imagick();
  37. $im->readImageBlob($imgSource);
  38. } else {
  39. $im = imagecreatefromstring($imgSource);
  40. }
  41. break;
  42. case QrReader::SOURCE_TYPE_RESOURCE:
  43. $im = $imgSource;
  44. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  45. $useImagickIfAvailable = true;
  46. } else {
  47. $useImagickIfAvailable = false;
  48. }
  49. break;
  50. }
  51. if ($useImagickIfAvailable && extension_loaded('imagick')) {
  52. if (!$im instanceof \Imagick) {
  53. throw new \InvalidArgumentException('Invalid image source.');
  54. }
  55. $width = $im->getImageWidth();
  56. $height = $im->getImageHeight();
  57. $source = new IMagickLuminanceSource($im, $width, $height);
  58. } else {
  59. if (!is_resource($im)) {
  60. throw new \InvalidArgumentException('Invalid image source.');
  61. }
  62. $width = imagesx($im);
  63. $height = imagesy($im);
  64. $source = new GDLuminanceSource($im, $width, $height);
  65. }
  66. $histo = new HybridBinarizer($source);
  67. $this->bitmap = new BinaryBitmap($histo);
  68. $this->reader = new QRCodeReader();
  69. }
  70. public function decode()
  71. {
  72. try {
  73. $this->result = $this->reader->decode($this->bitmap);
  74. } catch (NotFoundException $er) {
  75. $this->result = false;
  76. } catch (FormatException $er) {
  77. $this->result = false;
  78. } catch (ChecksumException $er) {
  79. $this->result = false;
  80. }
  81. }
  82. public function text()
  83. {
  84. $this->decode();
  85. if (method_exists($this->result, 'toString')) {
  86. return $this->result->toString();
  87. }
  88. return $this->result;
  89. }
  90. public function getResult()
  91. {
  92. return $this->result;
  93. }
  94. }
  1. 6、在代码里调用
  1. //引用
  2. use Zxing\QrReader;
  3. //调用类库
  4. $qrcode = new QrReader("二维码图片路径");
  5. $content = $qrcode->text();
  1. 到此这篇关于Thinkphp使用Zxing扩展库解析二维码内容图文讲解的文章就介绍到这了,更多相关Thinkphp使用Zxing扩展库解析二维码内容内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post