Android zxing 库生成的二维码无法使用大多数二维码扫描仪进行扫描 [英] QR code generated by Android zxing library doesn't scan with most QR code scanners

查看:16

我发这个是为了回答我自己的问题(为了防止其他人遇到这个问题,传播这个词.)

我正在使用 ZXing 的 Android 库生成二维码.二维码生成正确,我可以显示它(在使用 QRCode.getMatrix().getArray() 手动渲染后).Android 市场上可用的二维码阅读器,包括 ZXing 的扫描仪本身!

此外,每当我为 Encoder,它会忽略它并使用一些随机级别(通常是级别 Q)进行编码.

我用这段代码生成了二维码:

<前>二维码;尝试{code = Encoder.encode("...QRCODEDATA...", ErrorCorrectionLevel.L);}抓住(作家异常前){log("获取二维码失败");返回空;}

...然后,在获得 QRCode 对象后,我像这样绘制位图:

<代码>byte[][] bitArray = qrCode.getMatrix().getArray();

 if(bitArray == null || bitArray.length <1)返回空;for(int x = 0;x 

这是我的结果.


它看起来不错,但它不会扫描.少数二维码扫描仪仍然会扫描它,但是大多数不会.怎么回事?

解决方案

这个问题的答案:

实际上是翻转了二维码.尽管 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.

flipped QR code