代码之家  ›  专栏  ›  技术社区  ›  WaitMoonMan

我想在控制台上输出一个二维码?

  •  0
  • WaitMoonMan  · 技术社区  · 7 年前

    我已经从其他代码生成了一个二维码,我将直接得到二维码的大小,我将通过循环输出一个色块,如果有颜色,或者如果没有颜色,则输出一个空格。但结果是,线条高度影响了我的二维码生成,导致二维码太高,是否有更好的解决方案,或者是否有可以直接使用的扩展包? enter image description here enter image description here

    我尝试将字符替换为\u2584,但控制台显示了大量\u2584,这不是一个好结果。

    字符无法正确转义,我确信我使用了双引号 enter image description here

    我用两个字符和两个空格组成一个正方形,但这不是一个好的解决方案。 enter image description here enter image description here

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sammitch    7 年前

    使用半高上下、全块和空格在一行文本中编码两行二维码。

    例如,基于 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有人已经为此做了更好的公关,哦,好吧。

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