国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.1KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /*
  4. * PHP QR Code encoder
  5. *
  6. * Bitstream class
  7. *
  8. * Based on libqrencode C library distributed under LGPL 2.1
  9. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  10. *
  11. * PHP QR Code is distributed under LGPL 3
  12. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 3 of the License, or any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. class QRbitstream {
  29. public $data = array();
  30. //----------------------------------------------------------------------
  31. public function size()
  32. {
  33. return count($this->data);
  34. }
  35. //----------------------------------------------------------------------
  36. public function allocate($setLength)
  37. {
  38. $this->data = array_fill(0, $setLength, 0);
  39. return 0;
  40. }
  41. //----------------------------------------------------------------------
  42. public static function newFromNum($bits, $num)
  43. {
  44. $bstream = new QRbitstream();
  45. $bstream->allocate($bits);
  46. $mask = 1 << ($bits - 1);
  47. for($i=0; $i<$bits; $i++) {
  48. if ($num & $mask) {
  49. $bstream->data[$i] = 1;
  50. } else {
  51. $bstream->data[$i] = 0;
  52. }
  53. $mask = $mask >> 1;
  54. }
  55. return $bstream;
  56. }
  57. //----------------------------------------------------------------------
  58. public static function newFromBytes($size, $data)
  59. {
  60. $bstream = new QRbitstream();
  61. $bstream->allocate($size * 8);
  62. $p=0;
  63. for($i=0; $i<$size; $i++) {
  64. $mask = 0x80;
  65. for($j=0; $j<8; $j++) {
  66. if ($data[$i] & $mask) {
  67. $bstream->data[$p] = 1;
  68. } else {
  69. $bstream->data[$p] = 0;
  70. }
  71. $p++;
  72. $mask = $mask >> 1;
  73. }
  74. }
  75. return $bstream;
  76. }
  77. //----------------------------------------------------------------------
  78. public function append(QRbitstream $arg)
  79. {
  80. if (is_null($arg)) {
  81. return -1;
  82. }
  83. if ($arg->size() == 0) {
  84. return 0;
  85. }
  86. if ($this->size() == 0) {
  87. $this->data = $arg->data;
  88. return 0;
  89. }
  90. $this->data = array_values(array_merge($this->data, $arg->data));
  91. return 0;
  92. }
  93. //----------------------------------------------------------------------
  94. public function appendNum($bits, $num)
  95. {
  96. if ($bits == 0)
  97. return 0;
  98. $b = QRbitstream::newFromNum($bits, $num);
  99. if (is_null($b))
  100. return -1;
  101. $ret = $this->append($b);
  102. unset($b);
  103. return $ret;
  104. }
  105. //----------------------------------------------------------------------
  106. public function appendBytes($size, $data)
  107. {
  108. if ($size == 0)
  109. return 0;
  110. $b = QRbitstream::newFromBytes($size, $data);
  111. if (is_null($b))
  112. return -1;
  113. $ret = $this->append($b);
  114. unset($b);
  115. return $ret;
  116. }
  117. //----------------------------------------------------------------------
  118. public function toByte()
  119. {
  120. $size = $this->size();
  121. if ($size == 0) {
  122. return array();
  123. }
  124. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  125. $bytes = (int)($size / 8);
  126. $p = 0;
  127. for($i=0; $i<$bytes; $i++) {
  128. $v = 0;
  129. for($j=0; $j<8; $j++) {
  130. $v = $v << 1;
  131. $v |= $this->data[$p];
  132. $p++;
  133. }
  134. $data[$i] = $v;
  135. }
  136. if ($size & 7) {
  137. $v = 0;
  138. for($j=0; $j<($size & 7); $j++) {
  139. $v = $v << 1;
  140. $v |= $this->data[$p];
  141. $p++;
  142. }
  143. $data[$bytes] = $v;
  144. }
  145. return $data;
  146. }
  147. }