且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android zxing 库生成的二维码无法使用大多数二维码扫描仪进行扫描

更新时间:2022-10-19 12:59:48

这个问题的答案:

实际上是翻转了二维码.尽管 ZXing 文档没有解释如何索引 qrCode.getMatrix().getArray() 返回的数组,但它假设您将其索引为 [y][x],然后在 (x,y).问题中发布的代码将数组索引为 [x][y],它沿 Y=X 线翻转图像.

生成的二维码看似合法,但只有智能"扫描仪才能检测到这种翻转并对其进行扫描.

纠错级别位也在对角,所以如果你手动验证(看图像右下角的几个位),看起来库忽略了纠错设置.

I am posting this to answer my own question (to spread the word in case anyone else has had this issue.)

I am generating a QR code using ZXing's Android library. The QR code generates properly and I am able to display it (after rendering it out manually using QRCode.getMatrix().getArray().) However, the QR code generated does not scan with most of the QR code readers available on the Android market, including ZXing's scanner itself!

Additionally, whenever I set the error correction level for Encoder, it ignores it and encodes with some random level (usually level Q).

I generate the QR code with this piece of code:


    QRCode code;

    try
    {
            code = Encoder.encode("...QRCODEDATA...", ErrorCorrectionLevel.L);
    }
    catch(WriterException ex)
    {
            log("Failed to obtain a QR code");
            return null;
    }
    

...and then, after obtaining the QRCode object, I draw the bitmap like so:

byte[][] bitArray = qrCode.getMatrix().getArray();

        if(bitArray == null || bitArray.length < 1)
            return null;

        for(int x = 0;x < bitArray.length;x++)
        {
            for(int y = 0;y < bitArray[x].length;y++)
            {
                if(bitArray[x][y] == 0)
                    bitmapDrawCell(x,y,WHITE);
                else
                    bitmapDrawCell(x,y,BLACK);
            }
        }

Here's what I end up with.


It looks right, but it won't scan. A handful of QR code scanner will still scan it, but most will not. What's going on?

The answer to this issue:

The QR code is actually flipped. Although the ZXing documentation does not explain how to index into the array that qrCode.getMatrix().getArray() returns, it assumes that you will index it as [y][x], and then draw that cell at (x,y). The code posted in the question indexes the array as [x][y], which flips the image along the Y=X line.

The resulting QR code seems legitimate, but only 'smart' scanners can detect this sort of flipping and scan it.

The error correction level bits are also on the opposite corner, so if you were to verify by hand (looking at a few bits in the lower right corner of the image), it would appear that the library is ignoring the error correction settings.