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

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:  * Encoder.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\Spec;
 21: 
 22: /**
 23:  * Com\Tecnick\Barcode\Type\Square\QrCode\Encoder
 24:  *
 25:  * @since       2015-02-21
 26:  * @category    Library
 27:  * @package     Barcode
 28:  * @author      Nicola Asuni <info@tecnick.com>
 29:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 30:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 31:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 32:  */
 33: class Encoder extends \Com\Tecnick\Barcode\Type\Square\QrCode\Init
 34: {
 35:     /**
 36:      * Data code
 37:      *
 38:      * @var array
 39:      */
 40:     protected $datacode = array();
 41: 
 42:     /**
 43:      * Error correction code
 44:      *
 45:      * @var array
 46:      */
 47:     protected $ecccode = array();
 48: 
 49:     /**
 50:      * Blocks
 51:      *
 52:      * @var array
 53:      */
 54:     protected $blocks;
 55: 
 56:     /**
 57:      * Reed-Solomon blocks
 58:      *
 59:      * @var array
 60:      */
 61:     protected $rsblocks = array(); //of RSblock
 62: 
 63:     /**
 64:      * Counter
 65:      *
 66:      * @var int
 67:      */
 68:     protected $count;
 69: 
 70:     /**
 71:      * Data length
 72:      *
 73:      * @var int
 74:      */
 75:     protected $dataLength;
 76: 
 77:     /**
 78:      * Error correction length
 79:      *
 80:      * @var int
 81:      */
 82:     protected $eccLength;
 83: 
 84:     /**
 85:      * Value bv1
 86:      *
 87:      * @var int
 88:      */
 89:     protected $bv1;
 90: 
 91:     /**
 92:      * Width.
 93:      *
 94:      * @var int
 95:      */
 96:     protected $width;
 97: 
 98:     /**
 99:      * Frame
100:      *
101:      * @var array
102:      */
103:     protected $frame;
104: 
105:     /**
106:      * Horizontal bit position
107:      *
108:      * @var int
109:      */
110:     protected $xpos;
111: 
112:     /**
113:      * Vertical bit position
114:      *
115:      * @var int
116:      */
117:     protected $ypos;
118: 
119:     /**
120:      * Direction
121:      *
122:      * @var int
123:      */
124:     protected $dir;
125: 
126:     /**
127:      * Single bit value
128:      *
129:      * @var int
130:      */
131:     protected $bit;
132: 
133:     /**
134:      * Reed-Solomon items
135:      *
136:      * @va array
137:      */
138:     protected $rsitems = array();
139: 
140:     /**
141:      * Encode mask
142:      *
143:      * @param int   $maskNo   Mask number (masking mode)
144:      * @param array $datacode Data code to encode
145:      *
146:      * @return array Encoded Mask
147:      */
148:     public function encodeMask($maskNo, $datacode)
149:     {
150:         // initialize values
151:         $this->datacode = $datacode;
152:         $spec = $this->spc->getEccSpec($this->version, $this->level, array(0, 0, 0, 0, 0));
153:         $this->bv1 = $this->spc->rsBlockNum1($spec);
154:         $this->dataLength = $this->spc->rsDataLength($spec);
155:         $this->eccLength = $this->spc->rsEccLength($spec);
156:         $this->ecccode = array_fill(0, $this->eccLength, 0);
157:         $this->blocks = $this->spc->rsBlockNum($spec);
158:         $this->init($spec);
159:         $this->count = 0;
160:         $this->width = $this->spc->getWidth($this->version);
161:         $this->frame = $this->spc->createFrame($this->version);
162:         $this->xpos = ($this->width - 1);
163:         $this->ypos = ($this->width - 1);
164:         $this->dir = -1;
165:         $this->bit = -1;
166:         
167:         // interleaved data and ecc codes
168:         for ($idx = 0; $idx < ($this->dataLength + $this->eccLength); $idx++) {
169:             $code = $this->getCode();
170:             $bit = 0x80;
171:             for ($jdx = 0; $jdx < 8; $jdx++) {
172:                 $addr = $this->getNextPosition();
173:                 $this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
174:                 $bit = $bit >> 1;
175:             }
176:         }
177: 
178:         // remainder bits
179:         $rbits = $this->spc->getRemainder($this->version);
180:         for ($idx = 0; $idx < $rbits; $idx++) {
181:             $addr = $this->getNextPosition();
182:             $this->setFrameAt($addr, 0x02);
183:         }
184: 
185:         // masking
186:         $this->runLength = array_fill(0, (Data::QRSPEC_WIDTH_MAX + 1), 0);
187:         if ($maskNo < 0) {
188:             if ($this->qr_find_best_mask) {
189:                 $mask = $this->mask($this->width, $this->frame, $this->level);
190:             } else {
191:                 $mask = $this->makeMask($this->width, $this->frame, (intval($this->qr_default_mask) % 8), $this->level);
192:             }
193:         } else {
194:             $mask = $this->makeMask($this->width, $this->frame, $maskNo, $this->level);
195:         }
196:         if ($mask == null) {
197:             throw new BarcodeException('Null Mask');
198:         }
199:         return $mask;
200:     }
201: 
202:     /**
203:      * Return Reed-Solomon block code
204:      *
205:      * @return array rsblocks
206:      */
207:     protected function getCode()
208:     {
209:         if ($this->count < $this->dataLength) {
210:             $row = ($this->count % $this->blocks);
211:             $col = ($this->count / $this->blocks);
212:             if ($col >= $this->rsblocks[0]['dataLength']) {
213:                 $row += $this->bv1;
214:             }
215:             $ret = $this->rsblocks[$row]['data'][$col];
216:         } elseif ($this->count < ($this->dataLength + $this->eccLength)) {
217:             $row = (($this->count - $this->dataLength) % $this->blocks);
218:             $col = (($this->count - $this->dataLength) / $this->blocks);
219:             $ret = $this->rsblocks[$row]['ecc'][$col];
220:         } else {
221:             return 0;
222:         }
223:         ++$this->count;
224:         return $ret;
225:     }
226: 
227:     /**
228:      * Set frame value at specified position
229:      *
230:      * @param array $pos X,Y position
231:      * @param int   $val Value of the character to set
232:      */
233:     protected function setFrameAt($pos, $val)
234:     {
235:         $this->frame[$pos['y']][$pos['x']] = chr($val);
236:     }
237: 
238:     /**
239:      * Return the next frame position
240:      *
241:      * @return array of x,y coordinates
242:      */
243:     protected function getNextPosition()
244:     {
245:         do {
246:             if ($this->bit == -1) {
247:                 $this->bit = 0;
248:                 return array('x' => $this->xpos, 'y' => $this->ypos);
249:             }
250:             $xpos = $this->xpos;
251:             $ypos = $this->ypos;
252:             $wdt = $this->width;
253:             $this->getNextPositionB($xpos, $ypos, $wdt);
254:             if (($xpos < 0) || ($ypos < 0)) {
255:                 throw new BarcodeException('Error getting next position');
256:             }
257:             $this->xpos = $xpos;
258:             $this->ypos = $ypos;
259:         } while (ord($this->frame[$ypos][$xpos]) & 0x80);
260: 
261:         return array('x' => $xpos, 'y' => $ypos);
262:     }
263: 
264:     /**
265:      * Internal cycle for getNextPosition
266:      *
267:      * @param int $xpos
268:      * @param int $ypos
269:      * @param int $wdt
270:      */
271:     protected function getNextPositionB(&$xpos, &$ypos, $wdt)
272:     {
273:         if ($this->bit == 0) {
274:             --$xpos;
275:             ++$this->bit;
276:         } else {
277:             ++$xpos;
278:             $ypos += $this->dir;
279:             --$this->bit;
280:         }
281:         if ($this->dir < 0) {
282:             if ($ypos < 0) {
283:                 $ypos = 0;
284:                 $xpos -= 2;
285:                 $this->dir = 1;
286:                 if ($xpos == 6) {
287:                     --$xpos;
288:                     $ypos = 9;
289:                 }
290:             }
291:         } else {
292:             if ($ypos == $wdt) {
293:                 $ypos = $wdt - 1;
294:                 $xpos -= 2;
295:                 $this->dir = -1;
296:                 if ($xpos == 6) {
297:                     --$xpos;
298:                     $ypos -= 8;
299:                 }
300:             }
301:         }
302:     }
303: }
304: 
 

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