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

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:  * Estimate.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: 
 21: /**
 22:  * Com\Tecnick\Barcode\Type\Square\QrCode\Estimate
 23:  *
 24:  * @since       2015-02-21
 25:  * @category    Library
 26:  * @package     Barcode
 27:  * @author      Nicola Asuni <info@tecnick.com>
 28:  * @copyright   2010-2016 Nicola Asuni - Tecnick.com LTD
 29:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 30:  * @link        https://github.com/tecnickcom/tc-lib-barcode
 31:  */
 32: abstract class Estimate
 33: {
 34:     /**
 35:      * estimateBitsModeNum
 36:      *
 37:      * @param int $size
 38:      *
 39:      * @return int number of bits
 40:      */
 41:     public function estimateBitsModeNum($size)
 42:     {
 43:         $wdt = (int)($size / 3);
 44:         $bits = ($wdt * 10);
 45:         switch ($size - ($wdt * 3)) {
 46:             case 1:
 47:                 $bits += 4;
 48:                 break;
 49:             case 2:
 50:                 $bits += 7;
 51:                 break;
 52:         }
 53:         return $bits;
 54:     }
 55: 
 56:     /**
 57:      * estimateBitsModeAn
 58:      *
 59:      * @param int $size
 60:      *
 61:      * @return int number of bits
 62:      */
 63:     public function estimateBitsModeAn($size)
 64:     {
 65:         $bits = (int)($size * 5.5); // (size / 2 ) * 11
 66:         if ($size & 1) {
 67:             $bits += 6;
 68:         }
 69:         return $bits;
 70:     }
 71: 
 72:     /**
 73:      * estimateBitsMode8
 74:      *
 75:      * @param int $size
 76:      *
 77:      * @return int number of bits
 78:      */
 79:     public function estimateBitsMode8($size)
 80:     {
 81:         return (int)($size * 8);
 82:     }
 83: 
 84:     /**
 85:      * estimateBitsModeKanji
 86:      *
 87:      * @param int $size
 88:      *
 89:      * @return int number of bits
 90:      */
 91:     public function estimateBitsModeKanji($size)
 92:     {
 93:         return (int)($size * 6.5); // (size / 2 ) * 13
 94:     }
 95: 
 96:     /**
 97:      * Estimate version
 98:      *
 99:      * @param array $items
100:      * @param int   $level
101:      *
102:      * @return int version
103:      */
104:     public function estimateVersion($items, $level)
105:     {
106:         $version = 0;
107:         $prev = 0;
108:         do {
109:             $prev = $version;
110:             $bits = $this->estimateBitStreamSize($items, $prev);
111:             $version = $this->getMinimumVersion((int)(($bits + 7) / 8), $level);
112:             if ($version < 0) {
113:                 return -1;
114:             }
115:         } while ($version > $prev);
116:         return $version;
117:     }
118: 
119:     /**
120:      * Return a version number that satisfies the input code length.
121:      *
122:      * @param int $size  Input code length (bytes)
123:      * @param int $level Error correction level
124:      *
125:      * @return int version number
126:      *
127:      * @throws BarcodeException
128:      */
129:     protected function getMinimumVersion($size, $level)
130:     {
131:         for ($idx = 1; $idx <= Data::QRSPEC_VERSION_MAX; ++$idx) {
132:             $words = (Data::$capacity[$idx][Data::QRCAP_WORDS] - Data::$capacity[$idx][Data::QRCAP_EC][$level]);
133:             if ($words >= $size) {
134:                 return $idx;
135:             }
136:         }
137:         throw new BarcodeException(
138:             'The size of input data is greater than Data::QR capacity, try to lower the error correction mode'
139:         );
140:     }
141: 
142:     /**
143:      * estimateBitStreamSize
144:      *
145:      * @param array $items
146:      * @param int   $version
147:      *
148:      * @return int bits
149:      */
150:     protected function estimateBitStreamSize($items, $version)
151:     {
152:         $bits = 0;
153:         if ($version == 0) {
154:             $version = 1;
155:         }
156:         foreach ($items as $item) {
157:             switch ($item['mode']) {
158:                 case Data::$encodingModes['NM']:
159:                     $bits = $this->estimateBitsModeNum($item['size']);
160:                     break;
161:                 case Data::$encodingModes['AN']:
162:                     $bits = $this->estimateBitsModeAn($item['size']);
163:                     break;
164:                 case Data::$encodingModes['8B']:
165:                     $bits = $this->estimateBitsMode8($item['size']);
166:                     break;
167:                 case Data::$encodingModes['KJ']:
168:                     $bits = $this->estimateBitsModeKanji($item['size']);
169:                     break;
170:                 case Data::$encodingModes['ST']:
171:                     return Data::STRUCTURE_HEADER_BITS;
172:                 default:
173:                     return 0;
174:             }
175:             $len = $this->getLengthIndicator($item['mode'], $version);
176:             $mod = 1 << $len;
177:             $num = (int)(($item['size'] + $mod - 1) / $mod);
178:             $bits += $num * (4 + $len);
179:         }
180:         return $bits;
181:     }
182: }
183: 
 

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