예제 #1
0
파일: Input.php 프로젝트: acgrid/adbot
 public function swipe(array $rectangle)
 {
     $this->scr->translateRect($rectangle);
     $retry = 0;
     while (!$this->adb->swipeLine($rectangle)) {
         if (++$retry > $this->retryLimit) {
             $this->logger->error('Abandon retry for tapping screen.');
             return $this->adb->shell->returnCode;
         } else {
             $this->logger->warning('Retry %u for tapping screen.', [$retry]);
         }
         $this->delay->delayOffset($this->retryDelay, $this->retryOffset);
     }
     return true;
 }
예제 #2
0
파일: Base.php 프로젝트: acgrid/adbot
 public function ocr($rule, array &$rect, array $override = [])
 {
     if (!isset($this->rules[$rule])) {
         throw new \InvalidArgumentException("Undefined OCR rule '{$rule}'.");
     }
     $this->rule =& $this->rules[$rule];
     $this->setConfig($override);
     $this->align = self::getRectAlign($rect);
     $this->result = '';
     if ($this->mode === self::SCAN_FIXED) {
         $minX = min($rect[Position::X1], $rect[Position::X2]);
         $maxX = max($rect[Position::X1], $rect[Position::X2]);
         if ($this->width == 0) {
             throw new \InvalidArgumentException('OCR Width is not initialized or passed.');
         }
         $scanAt = $this->align == self::ALIGN_LEFT ? $minX : $maxX - $this->width;
         $this->logger->debug('Ready to OCR with fixed stepping, Align=%s, Range=%.4f-%.4f, Start=%.4f, Width=%.4f, Margin=%.4f, Step=%.4f', [$this->align, $minX, $maxX, $scanAt, $this->width, $this->margin, $this->ocrStep()]);
         while ($scanAt >= $minX && $scanAt + $this->width <= $maxX) {
             $ocrRect = Position::makeRectangle($scanAt, $rect[Position::Y1], $scanAt + $this->width, $rect[Position::Y2]);
             $char = $this->ocrChar($this->rule, $ocrRect);
             if (is_null($char) && $this->ocrFailure() === Manager::RET_FINISH) {
                 break;
             }
             $this->ocrConcat($char);
             $scanAt += $this->ocrStep();
             $this->logger->debug('Forward=%.4f', [$scanAt]);
         }
     } elseif ($this->mode === self::SCAN_ADAPTIVE) {
         $this->screen->translateRect($rect);
         $this->scanCache = [];
         $minX = min($rect[Position::X1], $rect[Position::X2]);
         $maxX = max($rect[Position::X1], $rect[Position::X2]);
         $minY = min($rect[Position::Y1], $rect[Position::Y2]);
         $maxY = max($rect[Position::Y1], $rect[Position::Y2]);
         $scanAt = $rect[Position::X1];
         $step = $this->align === self::ALIGN_LEFT ? 1 : -1;
         $this->logger->debug('Ready to OCR with adaptive width. Align=%s, Range=%u-%u, Step=%d', [$this->align, $minX, $maxX, $step]);
         while ($scanAt >= $minX && $scanAt <= $maxX) {
             $Y1 = $maxY;
             $Y2 = $minY;
             // Get X1
             while ($scanAt >= $minX && $scanAt <= $maxX) {
                 if ($Y1 = $this->searchInLine(Position::Y, $scanAt, $minY, $maxY)) {
                     break;
                 }
                 $scanAt += $step;
             }
             $X1 = $scanAt;
             // Get X2, Y1
             while ($scanAt >= $minX && $scanAt <= $maxX && ($foundY = $this->searchInLine(Position::Y, $scanAt, $minY, $maxY))) {
                 if ($foundY < $Y1) {
                     $Y1 = $foundY;
                 }
                 $scanAt += $step;
             }
             $X2 = $scanAt;
             if ($scanAt < $minX || $scanAt > $maxX) {
                 break;
             }
             // Get Y2
             for ($scanY = $maxY; $scanY > $Y1; $scanY--) {
                 if ($this->searchInLine(Position::X, $scanY, $X1, $X2)) {
                     $Y2 = $scanY;
                     break;
                 }
             }
             // OCR
             $ocrRect = Position::makeRectangle($X1, $Y1, $X2, $Y2);
             $this->logger->info('Possible character from (%u,%u) to (%u,%u)', [$X1, $Y1, $X2, $Y2]);
             $char = $this->ocrChar($this->rule, $ocrRect);
             if (is_null($char) && $this->ocrFailure() === Manager::RET_FINISH) {
                 break;
             }
             $this->ocrConcat($char);
             $scanAt += $step;
             $this->logger->debug('Forward=%u', [$scanAt]);
         }
     } else {
         throw new \LogicException("OCR scan mode is exceptional.");
     }
     return $this->ocrComplete($this->result);
 }