飞道的博客

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

353人阅读  评论(0)

一、下载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()方法修改内存限制大小

  
  1. //修改php内存限制为1024M
  2. 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. use Zxing\ Common\ HybridBinarizer;
  4. use Zxing\ Qrcode\ QRCodeReader;
  5. include_once( 'common/customFunctions.php');
  6. final class QrReader
  7. {
  8. }

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. }

6、在代码里调用


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

 


转载:https://blog.csdn.net/qq15577969/article/details/114217992
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场