Bacon / BaconQrCode

QR Code Generator for PHP
BSD 2-Clause "Simplified" License
1.88k stars 212 forks source link

Kanji Mode QR code is broken #172

Open askdkc opened 8 months ago

askdkc commented 8 months ago

Although a recent pull request (https://github.com/Bacon/BaconQrCode/pull/170) fixed encoding errors for Shift-JIS, generating QR codes in Kanji mode still presents issues with readability. For instance, attempting to generate a QR code using only Kanji characters results in an unreadable code.

For Kanji Mode example:

use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;

$renderer = new ImageRenderer(
  new RendererStyle(400),
  new ImagickImageBackEnd()
);
$writer = new Writer($renderer);
$writer->writeFile("あいうえお", "qrcode-alphanum.png", "SHIFT-JIS"); // Kanji Mode is used

Generated QR code (unreadable)

qrcode-kanjimode

However, by including alphanumeric characters and switching to Byte mode, a readable QR code can be generated.

For Byte Mode example:

use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;

$renderer = new ImageRenderer(
  new RendererStyle(400),
  new ImagickImageBackEnd()
);
$writer = new Writer($renderer);
$writer->writeFile("あいうえお123", "qrcode-alphanum.png", "SHIFT-JIS"); // Byte Mode is used

Generated QR code (readable)

qrcode-bytemode

How to fix:

I think we should just simply use Byte Mode to generate Shift-JIS encoded QR Code by changing this code:

https://github.com/Bacon/BaconQrCode/blob/c01758cc4bf2eb4225b92dae7d766c1a4b069185/src/Encoder/Encoder.php#L165-L167

to this:

if (null !== $encoding && 0 === strcasecmp($encoding, 'SHIFT-JIS')) {
    return Mode::BYTE();
}