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

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:  * QrCode.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Barcode
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2015-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;
 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\ByteStream;
 21: use \Com\Tecnick\Barcode\Type\Square\QrCode\Split;
 22: use \Com\Tecnick\Barcode\Type\Square\QrCode\Encoder;
 23: 
 24: /**
 25:  * Com\Tecnick\Barcode\Type\Square\QrCode
 26:  *
 27:  * QrCode Barcode type class
 28:  *
 29:  * @since       2015-02-21
 30:  * @category    Library
 31:  * @package     Barcode
 32:  * @author      Nicola Asuni <info@tecnick.com>
 33:  * @copyright   2015-2016 Nicola Asuni - Tecnick.com LTD
 34:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 35:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 36:  */
 37: class QrCode extends \Com\Tecnick\Barcode\Type\Square
 38: {
 39:     /**
 40:      * Barcode format
 41:      *
 42:      * @var string
 43:      */
 44:     protected $format = 'QRCODE';
 45: 
 46:     /**
 47:      * QR code version.
 48:      * The Size of QRcode is defined as version. Version is an integer value from 1 to 40.
 49:      * Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
 50:      * So version 40 is 177*177 matrix.
 51:      *
 52:      * @var int
 53:      */
 54:     protected $version = 0;
 55: 
 56:     /**
 57:      * Error correction level
 58:      *
 59:      * @var int
 60:      */
 61:     protected $level = 0;
 62: 
 63:     /**
 64:      * Encoding mode
 65:      *
 66:      * @var int
 67:      */
 68:     protected $hint = 2;
 69: 
 70:     /**
 71:      * Boolean flag, if false the input string will be converted to uppercase.
 72:      *
 73:      * @var boolean
 74:      */
 75:     protected $case_sensitive = true;
 76: 
 77:     /**
 78:      * If false, checks all masks available,
 79:      * otherwise the value indicates the number of masks to be checked, mask id are random
 80:      *
 81:      * @var int
 82:      */
 83:     protected $random_mask = false;
 84: 
 85:     /**
 86:      * If true, estimates best mask (spec. default, but extremally slow;
 87:      * set to false to significant performance boost but (propably) worst quality code
 88:      *
 89:      * @var bool
 90:      */
 91:     protected $best_mask = true;
 92: 
 93:     /**
 94:      * Default mask used when $this->best_mask === false
 95:      *
 96:      * @var int
 97:      */
 98:     protected $default_mask = 2;
 99: 
100:     /**
101:      * ByteStream class object
102:      *
103:      * @var \Com\Tecnick\Barcode\Type\Square\QrCode\ByteStream
104:      */
105:     protected $bsObj;
106: 
107:     /**
108:      * Set extra (optional) parameters:
109:      *     1: LEVEL - error correction level: L, M, Q, H
110:      *     2: HINT - encoding mode: NL=variable, NM=numeric, AN=alphanumeric, 8B=8bit, KJ=KANJI, ST=STRUCTURED
111:      *     3: VERSION - integer value from 1 to 40
112:      *     4: CASE SENSITIVE - if 0 the input string will be converted to uppercase
113:      *     5: RANDOM MASK - false or number of masks to be checked
114:      *     6: BEST MASK - true to find the best mask (slow)
115:      *     7: DEFAULT MASK - mask to use when the best mask option is false
116:      *
117:      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
118:      * @SuppressWarnings(PHPMD.NPathComplexity)
119:      */
120:     protected function setParameters()
121:     {
122:         parent::setParameters();
123:         // level
124:         if (!isset($this->params[0]) || !isset(Data::$errCorrLevels[$this->params[0]])) {
125:             $this->params[0] = 'L';
126:         }
127:         $this->level = Data::$errCorrLevels[$this->params[0]];
128: 
129:         // hint
130:         if (!isset($this->params[1]) || !isset(Data::$encodingModes[$this->params[1]])) {
131:             $this->params[1] = '8B';
132:         }
133:         $this->hint = Data::$encodingModes[$this->params[1]];
134: 
135:         // version
136:         if (!isset($this->params[2]) || ($this->params[2] < 0) || ($this->params[2] > Data::QRSPEC_VERSION_MAX)) {
137:             $this->params[2] = 0;
138:         }
139:         $this->version = intval($this->params[2]);
140: 
141:         // case sensitive
142:         if (!isset($this->params[3])) {
143:             $this->params[3] = 1;
144:         }
145:         $this->case_sensitive = (bool)$this->params[3];
146: 
147:         // random mask mode - number of masks to be checked
148:         if (!empty($this->params[4])) {
149:             $this->random_mask = intval($this->params[4]);
150:         }
151: 
152:         // find best mask
153:         if (!isset($this->params[5])) {
154:             $this->params[5] = 1;
155:         }
156:         $this->best_mask = (bool)$this->params[5];
157: 
158:         // default mask
159:         if (!isset($this->params[6])) {
160:             $this->params[6] = 2;
161:         }
162:         $this->default_mask = intval($this->params[6]);
163:     }
164: 
165:     /**
166:      * Get the bars array
167:      *
168:      * @throws BarcodeException in case of error
169:      */
170:     protected function setBars()
171:     {
172:         if (strlen((string)$this->code) == 0) {
173:             throw new BarcodeException('Empty input');
174:         }
175:         $this->bsObj = new ByteStream($this->hint, $this->version, $this->level);
176:         // generate the qrcode
177:         $this->processBinarySequence(
178:             $this->binarize(
179:                 $this->encodeString($this->code)
180:             )
181:         );
182:     }
183: 
184:     /**
185:      * Convert the frame in binary form
186:      *
187:      * @param array $frame Array to binarize
188:      *
189:      * @return array frame in binary form
190:      */
191:     protected function binarize($frame)
192:     {
193:         $len = count($frame);
194:         // the frame is square (width = height)
195:         foreach ($frame as &$frameLine) {
196:             for ($idx = 0; $idx < $len; ++$idx) {
197:                 $frameLine[$idx] = (ord($frameLine[$idx]) & 1) ? '1' : '0';
198:             }
199:         }
200:         return $frame;
201:     }
202: 
203:     /**
204:      * Encode the input string
205:      *
206:      * @param string $data input string to encode
207:      */
208:     protected function encodeString($data)
209:     {
210:         if (!$this->case_sensitive) {
211:             $data = $this->toUpper($data);
212:         }
213:         $split = new Split($this->bsObj, $this->hint, $this->version);
214:         $datacode = $this->bsObj->getByteStream($split->getSplittedString($data));
215:         $this->version = $this->bsObj->version;
216:         $enc = new Encoder(
217:             $this->version,
218:             $this->level,
219:             $this->random_mask,
220:             $this->best_mask,
221:             $this->default_mask
222:         );
223:         return $enc->encodeMask(-1, $datacode);
224:     }
225: 
226:     /**
227:      * Convert input string into upper case mode
228:      *
229:      * @param string $data Data
230:      *
231:      * @return
232:      */
233:     protected function toUpper($data)
234:     {
235:         $len = strlen($data);
236:         $pos = 0;
237:         
238:         while ($pos < $len) {
239:             $mode = $this->bsObj->getEncodingMode($data, $pos);
240:             if ($mode == Data::$encodingModes['KJ']) {
241:                 $pos += 2;
242:             } else {
243:                 if ((ord($data[$pos]) >= ord('a')) && (ord($data[$pos]) <= ord('z'))) {
244:                     $data[$pos] = chr(ord($data[$pos]) - 32);
245:                 }
246:                 $pos++;
247:             }
248:         }
249:         return $data;
250:     }
251: }
252: 
 

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