QR码使用zxing android解码图像

时间:2015-08-20 14:28:24

标签: android zxing

我正在Android上做一个简单的应用程序,如下所示:

将QR码图像放入应用程序的Drawable文件中。通过ButtonClick,它应该被解码并显示结果(使用Zxing库)。

我在Java上做了相同的应用程序(然后解码使用BufferedImageLuminanceSource类)。

在我的Android应用程序中,我使用了RGBLuminanceSource类,如下所示:

LuminanceSource source = new RGBLuminanceSource(width, height, pixels)BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

我面临的问题是:图像必须太小而不能被Android应用程序解码(我必须尝试多种尺寸才能最终得到一个QR码图像被解码的图像)。同时,使用Java应用程序中的BufferedImageLuminanceSource可以轻松解码相同的图像,而无需调整大小。

如何避免此调整大小问题?

1 个答案:

答案 0 :(得分:5)

为时已晚,但对其他人有帮助,

因此我们可以使用Zxing库从Bitmap获取Qr代码信息。

 Bitmap generatedQRCode;
    int width = generatedQRCode.getWidth();
    int height = generatedQRCode.getHeight();
    int[] pixels = new int[width * height];
    generatedQRCode.getPixels(pixels, 0, width, 0, 0, width, height);

    RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));

    Reader reader = new MultiFormatReader();
    Result result = null;
    try {
        result = reader.decode(binaryBitmap);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    String text = result.getText();
    textViewQRCode.setText(" CONTENT: " + text);