2017-12-30 17 views
0

음, 다른 코드에서 QR 코드를 생성했습니다. QR 코드의 크기를 직접 가져올 것입니다. 색상이있는 경우 색상 블록을 출력하는 루프를 통과하거나 색이없는 경우 공간. 그러나 줄 높이가 QR 코드 생성에 영향을 미치므로 QR 코드가 너무 높거나, 더 나은 해결책이 있습니까? 아니면 직접 사용할 수있는 확장 패키지가 있습니까? enter image description here enter image description here콘솔에 2 차원 코드를 출력하고 싶습니까?

나는 \의 u2584와 문자를 대체하려고 노력하지만, 콘솔 \ u2584을 많이 안 좋은 결과를 표시합니다.

문자 내가 따옴표 enter image description here

나는 사각형을 형성하는 두 개의 문자와 두 개의 공백을 사용을 사용하고 확신 제대로 탈출 할 수없는, 그러나 이것은 좋은 해결책이 아니다. enter image description here enter image description here

+0

는 PHP로 작성된,하지만 당신은 아이디어를 얻을해야하지 : https://github.com/gtanner/qrcode-terminal/blob/master/lib/main.js – Philipp

+0

@Philipp 나는 그의 문자 시도를 . 나는 아직도 할 수 없다. – WaitMoonMan

+0

1. https://packagist.org/packages/bacon/bacon-qr-code 2. ['\ u2584 '는 UTF8에서'\ xE2 \ x96 \ x84'입니다.] (https://codepoints.net/U+ 2584? lang = en) 3. [이스케이프를 번역 할 수 있습니다.] (https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf -8-encoded-cha) 4. 반 높이 블록 대신 두 개의 전체 높이 블록 [및 두 개의 공백]을 사용하십시오. – Sammitch

답변

0

텍스트의 한 줄에 QR 코드의 두 행을 인코딩하기 위해 반자 높이, 전체 블록 및 공백을 사용하십시오. bacon/bacon-qr-code

<?php 
require(__DIR__.'/vendor/autoload.php'); 
use BaconQrCode\Encoder\QrCode; 

class HalfText extends \BaconQrCode\Renderer\Text\Plain { 
    protected $fullBlock = "\xE2\x96\x88"; 
    protected $emptyBlock = "\x20"; 
    protected $halfUpBlock = "\xE2\x96\x80"; 
    protected $halfDnBlock = "\xE2\x96\x84"; 

    public function render(QrCode $qrCode) { 
     $result = ''; 
     $matrix = $qrCode->getMatrix(); 
     $width = $matrix->getWidth(); 

     // Top margin 
     for ($x = 0; $x < $this->margin; $x++) { 
      $result .= str_repeat($this->emptyBlock, $width + 2 * $this->margin) . PHP_EOL; 
     } 

     // Body 
     $array = $matrix->getArray(); 

     for($y=0, $height=count($array); $y<$height; $y+=2) { 
      $result .= str_repeat($this->emptyBlock, $this->margin); // left margin 
      $oddBottom = ! key_exists($y+1, $array); 
      for($x=0, $width=count($array[$y]); $x<$width; $x++) { 
       $top = $array[$y][$x]; 
       $bottom = $oddBottom ? 0 : $array[$y+1][$x]; 
       switch(($top << 1) | $bottom) { 
        case 0: 
         $result .= $this->emptyBlock; 
         break; 
        case 1: 
         $result .= $this->halfDnBlock; 
         break; 
        case 2: 
         $result .= $this->halfUpBlock; 
         break; 
        case 3: 
         $result .= $this->fullBlock; 
         break; 
        default: 
         throw new BaconQrCode\Exception\OutOfBoundsException(); 
       } 
      } 
      $result .= str_repeat($this->emptyBlock, $this->margin); // right margin 
      $result .= PHP_EOL; 
     } 

     // Bottom margin 
     for ($x = 0; $x < $this->margin; $x++) { 
      $result .= str_repeat($this->emptyBlock, $width + 2 * $this->margin) . PHP_EOL; 
     } 

     return $result; 
    } 
} 

// testing 
use \BaconQrCode\Writer; 

if($argc !== 2) { 
    exit(1); 
} 

$r = new HalfText(); 
$w = new Writer($r); 

echo $w->writeString($argv[1]); 

예 출력에 따라

예는 :

█▀▀▀▀▀█ █▄▄█ █▀▀▀▀▀█ 
█ ███ █ █ █ ███ █ 
█ ▀▀▀ █ █▄ █ █ ▀▀▀ █ 
▀▀▀▀▀▀▀ █ ▀ █ ▀▀▀▀▀▀▀ 
▀▀█ ▄█▀ ███ ▄█▀▀█▄ ██ 
    ▀▄█▀█▀▀ ▄█▀▄ ▄██▀ ▀ 
    ▀▀ ▀ ▀ ▄█▀ ██▄ ▄▄ ▀▄ 
█▀▀▀▀▀█ ▄▀ ▀▄ ███▀▄▀ 
█ ███ █ ▀▀ ▄█▄ ▄ █▀ 
█ ▀▀▀ █ █▀▄▄█▄ ▀█▀▀▀▀ 
▀▀▀▀▀▀▀ ▀ ▀▀ ▀ ▀ 

Woops 누군가가 이미 더 나은 PR을했다, 오, 잘.

https://github.com/Bacon/BaconQrCode/pull/25

+0

고마워,이게 내가 원하는거야. – WaitMoonMan