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



我正在使用 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;

            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)

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