source-class-Com.Tecnick.Barcode.Type.Square.QrCode.ByteStream

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Exceptions

  1: <?php
  2: /**
  3:  * ByteStream.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Barcode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 12:  *
 13:  * This file is part of tc-lib-barcode software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Barcode\Type\Square\QrCode;
 17: 
 18: use \Com\Tecnick\Barcode\Exception as BarcodeException;
 19: use \Com\Tecnick\Barcode\Type\Square\QrCode\Data;
 20: use \Com\Tecnick\Barcode\Type\Square\QrCode\Estimate;
 21: use \Com\Tecnick\Barcode\Type\Square\QrCode\Spec;
 22: 
 23: /**
 24:  * Com\Tecnick\Barcode\Type\Square\QrCode\ByteStream
 25:  *
 26:  * @since       2015-02-21
 27:  * @category    Library
 28:  * @package     Barcode
 29:  * @author      Nicola Asuni <info@tecnick.com>
 30:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 31:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 32:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 33:  */
 34: class ByteStream extends \Com\Tecnick\Barcode\Type\Square\QrCode\Encode
 35: {
 36:     /**
 37:      * Encoding mode
 38:      *
 39:      * @var int
 40:      */
 41:     protected $hint = 2;
 42: 
 43:     /**
 44:      * QR code version.
 45:      * The Size of QRcode is defined as version. Version is an integer value from 1 to 40.
 46:      * Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
 47:      * So version 40 is 177*177 matrix.
 48:      *
 49:      * @var int
 50:      */
 51:     public $version = 0;
 52: 
 53:     /**
 54:      * Error correction level
 55:      *
 56:      * @var int
 57:      */
 58:     protected $level = 0;
 59: 
 60:     /**
 61:      * Initialize
 62:      *
 63:      * @param int $hint    Encoding mode
 64:      * @param int $version Code version
 65:      * @param int $level   Error Correction Level
 66:      */
 67:     public function __construct($hint, $version, $level)
 68:     {
 69:         $this->hint = $hint;
 70:         $this->version = $version;
 71:         $this->level = $level;
 72:     }
 73: 
 74:     /**
 75:      * Pack all bit streams padding bits into a byte array
 76:      *
 77:      * @param array $items items
 78:      *
 79:      * @return array padded merged byte stream
 80:      */
 81:     public function getByteStream($items)
 82:     {
 83:         return $this->bitstreamToByte(
 84:             $this->appendPaddingBit(
 85:                 $this->mergeBitStream($items)
 86:             )
 87:         );
 88:     }
 89: 
 90:     /**
 91:      * Convert bitstream to bytes
 92:      *
 93:      * @param array $bstream Original bitstream
 94:      *
 95:      * @return array of bytes
 96:      */
 97:     protected function bitstreamToByte($bstream)
 98:     {
 99:         $size = count($bstream);
100:         if ($size == 0) {
101:             return array();
102:         }
103:         $data = array_fill(0, (int)(($size + 7) / 8), 0);
104:         $bytes = (int)($size / 8);
105:         $pos = 0;
106:         for ($idx = 0; $idx < $bytes; ++$idx) {
107:             $val = 0;
108:             for ($jdx = 0; $jdx < 8; ++$jdx) {
109:                 $val = $val << 1;
110:                 $val |= $bstream[$pos];
111:                 $pos++;
112:             }
113:             $data[$idx] = $val;
114:         }
115:         if ($size & 7) {
116:             $val = 0;
117:             for ($jdx = 0; $jdx < ($size & 7); ++$jdx) {
118:                 $val = $val << 1;
119:                 $val |= $bstream[$pos];
120:                 $pos++;
121:             }
122:             $data[$bytes] = $val;
123:         }
124:         return $data;
125:     }
126: 
127:     /**
128:      * merge the bit stream
129:      *
130:      * @param array $items Items
131:      *
132:      * @return array bitstream
133:      */
134:     protected function mergeBitStream($items)
135:     {
136:         $items = $this->convertData($items);
137:         $bstream = array();
138:         foreach ($items as $item) {
139:             $bstream = $this->appendBitstream($bstream, $item['bstream']);
140:         }
141:         return $bstream;
142:     }
143: 
144:     /**
145:      * convertData
146:      *
147:      * @param array $items Items
148:      *
149:      * @return array items
150:      */
151:     protected function convertData($items)
152:     {
153:         $ver = $this->estimateVersion($items, $this->level);
154:         if ($ver > $this->version) {
155:             $this->version = $ver;
156:         }
157:         while (true) {
158:             $cbs = $this->createBitStream($items);
159:             $items = $cbs[0];
160:             $bits = $cbs[1];
161:             if ($bits < 0) {
162:                 throw new BarcodeException('Negative Bits value');
163:             }
164:             $ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
165:             if ($ver > $this->version) {
166:                 $this->version = $ver;
167:             } else {
168:                 break;
169:             }
170:         }
171:         return $items;
172:     }
173: 
174:     /**
175:      * Create BitStream
176:      *
177:      * @param $items
178:      *
179:      * @return array of items and total bits
180:      */
181:     protected function createBitStream($items)
182:     {
183:         $total = 0;
184:         foreach ($items as $key => $item) {
185:             $items[$key] = $this->encodeBitStream($item, $this->version);
186:             $bits = count($items[$key]['bstream']);
187:             $total += $bits;
188:         }
189:         return array($items, $total);
190:     }
191: 
192:     /**
193:      * Encode BitStream
194:      *
195:      * @param array $inputitem
196:      * @param int   $version
197:      *
198:      * @return array input item
199:      */
200:     public function encodeBitStream($inputitem, $version)
201:     {
202:         $inputitem['bstream'] = array();
203:         $specObj = new Spec;
204:         $words = $specObj->maximumWords($inputitem['mode'], $version);
205:         if ($inputitem['size'] > $words) {
206:             $st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
207:             $st2 = $this->newInputItem(
208:                 $inputitem['mode'],
209:                 ($inputitem['size'] - $words),
210:                 array_slice($inputitem['data'], $words)
211:             );
212:             $st1 = $this->encodeBitStream($st1, $version);
213:             $st2 = $this->encodeBitStream($st2, $version);
214:             $inputitem['bstream'] = array();
215:             $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
216:             $inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
217:         } else {
218:             switch ($inputitem['mode']) {
219:                 case Data::$encodingModes['NM']:
220:                     $inputitem = $this->encodeModeNum($inputitem, $version);
221:                     break;
222:                 case Data::$encodingModes['AN']:
223:                     $inputitem = $this->encodeModeAn($inputitem, $version);
224:                     break;
225:                 case Data::$encodingModes['8B']:
226:                     $inputitem = $this->encodeMode8($inputitem, $version);
227:                     break;
228:                 case Data::$encodingModes['KJ']:
229:                     $inputitem = $this->encodeModeKanji($inputitem, $version);
230:                     break;
231:                 case Data::$encodingModes['ST']:
232:                     $inputitem = $this->encodeModeStructure($inputitem);
233:                     break;
234:             }
235:         }
236:         return $inputitem;
237:     }
238: 
239:     /**
240:      * Append Padding Bit to bitstream
241:      *
242:      * @param array $bstream Bit stream
243:      *
244:      * @return array bitstream
245:      */
246:     protected function appendPaddingBit($bstream)
247:     {
248:         if (is_null($bstream)) {
249:             return null;
250:         }
251:         $bits = count($bstream);
252:         $specObj = new Spec;
253:         $maxwords = $specObj->getDataLength($this->version, $this->level);
254:         $maxbits = $maxwords * 8;
255:         if ($maxbits == $bits) {
256:             return $bstream;
257:         }
258:         if ($maxbits - $bits < 5) {
259:             return $this->appendNum($bstream, $maxbits - $bits, 0);
260:         }
261:         $bits += 4;
262:         $words = (int)(($bits + 7) / 8);
263:         $padding = array();
264:         $padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0);
265:         $padlen = $maxwords - $words;
266:         if ($padlen > 0) {
267:             $padbuf = array();
268:             for ($idx = 0; $idx < $padlen; ++$idx) {
269:                 $padbuf[$idx] = (($idx & 1) ? 0x11 : 0xec);
270:             }
271:             $padding = $this->appendBytes($padding, $padlen, $padbuf);
272:         }
273:         return $this->appendBitstream($bstream, $padding);
274:     }
275: }
276: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy