0

Well, I've generated a QR code from other code, and I'm going to get the size of the QR code directly, and I'm going through the loop to output a color block if there's a color, or a space if there's no color. But the effect is that line height affects my QR code generation, causing the QR code to be too high, is there a better solution, or is there an extension package that can be used directly? enter image description here enter image description here

I try to replace the characters with \u2584, but the console displays a lot of \u2584, not a good result.

Characters cannot be escaped correctly, I'm sure I'm using double quotes enter image description here

I use two characters and two spaces to form a square, but this is not a good solution.enter image description here enter image description here

  • Not written in php, but you should get the idea: https://github.com/gtanner/qrcode-terminal/blob/master/lib/main.js – Philipp Dec 30 '17 at 01:49
  • @Philipp I tried his characters. I still can't. – WaitMoonMan Dec 30 '17 at 01:56
  • 1. https://packagist.org/packages/bacon/bacon-qr-code 2. [`\u2584` is `\xE2\x96\x84` in UTF8](https://codepoints.net/U+2584?lang=en) 3. [You can translate those escapes.](https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha) 4. Use two full-height blocks [and two spaces] instead of a half-height block. – Sammitch Dec 30 '17 at 02:07
  • This came to me over the long weekend: Use the half-height upper *and* lower, the full block, and a space to encode two rows of the QR code in a single line of text. – Sammitch Jan 03 '18 at 19:06

1 Answers1

0

Use the half-height upper and lower, the full block, and a space to encode two rows of the QR code in a single line of text.

Eg, based on 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]);

Example output:

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

Woops someone already made a better PR for this, oh well.

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

Sammitch
  • 30,782
  • 7
  • 50
  • 77