Ejemplo n.º 1
0
 /**
  * @param Input $input
  */
 public function __construct(Input $input)
 {
     $spec = array(0, 0, 0, 0, 0);
     $this->dataCode = $input->getByteStream();
     if (is_null($this->dataCode)) {
         throw new InvalidConfigException('null input string');
     }
     Specifications::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec);
     $this->version = $input->getVersion();
     $this->b1 = Specifications::rsBlockNum1($spec);
     $this->dataLength = Specifications::rsDataLength($spec);
     $this->eccLength = Specifications::rsEccLength($spec);
     $this->eccCode = array_fill(0, $this->eccLength, 0);
     $this->blocks = Specifications::rsBlockNum($spec);
     $ret = $this->init($spec);
     if ($ret < 0) {
         throw new \Exception('block alloc error');
     }
     $this->count = 0;
 }
Ejemplo n.º 2
0
 /**
  * @param Input $input
  * @param $mask
  *
  * @return $this|null
  * @throws \yii\base\InvalidParamException
  */
 public function encodeMask(Input $input, $mask)
 {
     if ($input->getVersion() < 0 || $input->getVersion() > Enum::QRSPEC_VERSION_MAX) {
         throw new InvalidParamException('wrong version');
     }
     if ($input->getErrorCorrectionLevel() > Enum::QR_ECLEVEL_H) {
         throw new InvalidParamException('wrong level');
     }
     $raw = new RawCode($input);
     if ($this->benchmark) {
         Tools::markTime('after_raw');
     }
     $version = $raw->version;
     $width = Specifications::getWidth($version);
     $frame = Specifications::newFrame($version);
     $filler = new FrameFiller($width, $frame);
     if (is_null($filler)) {
         return null;
     }
     // inteleaved data and ecc codes
     for ($i = 0; $i < $raw->dataLength + $raw->eccLength; $i++) {
         $code = $raw->getCode();
         $bit = 0x80;
         for ($j = 0; $j < 8; $j++) {
             $addr = $filler->next();
             $filler->setFrameAt($addr, 0x2 | ($bit & $code) != 0);
             $bit = $bit >> 1;
         }
     }
     if ($this->benchmark) {
         Tools::markTime('after_filler');
     }
     unset($raw);
     // remainder bits
     $j = Specifications::getRemainder($version);
     for ($i = 0; $i < $j; $i++) {
         $addr = $filler->next();
         $filler->setFrameAt($addr, 0x2);
     }
     $frame = $filler->frame;
     unset($filler);
     // masking
     $maskObj = new Mask();
     if ($mask < 0) {
         if (Enum::QR_FIND_BEST_MASK) {
             $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
         } else {
             $masked = $maskObj->makeMask($width, $frame, intval(Enum::QR_DEFAULT_MASK) % 8, $input->getErrorCorrectionLevel());
         }
     } else {
         $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
     }
     if (is_null($masked)) {
         return null;
     }
     if ($this->benchmark) {
         Tools::markTime('after_mask');
     }
     $this->version = $version;
     $this->width = $width;
     $this->data = $masked;
     return $this;
 }