android zxing parsing QR code pictures and generating QR code pictures

Posted by zackat on Fri, 31 Jan 2020 14:44:30 +0100

About generating QR code pictures: Using zxing package to generate QR code under Android The QRCodeUtil provided in is everything. Very convenient.

About parsing the QR code picture, get the content:

public Result parseInfoFromBitmap(Bitmap bitmap) {
    int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    LogUtils.w("### pixels dest==" + Arrays.toString(pixels));

    RGBLuminanceSource source = new RGBLuminanceSource(bitmap.getWidth(),
            bitmap.getHeight(), pixels);
    GlobalHistogramBinarizer binarizer = new GlobalHistogramBinarizer(source);
    BinaryBitmap image = new BinaryBitmap(binarizer);
    Result result = null;
    try {
        result = new QRCodeReader().decode(image);
        return result;
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }

    return null;

}

Call:

view..setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LogUtils.w("Scan QR code");
        String path = new File(getCacheDir(), "zx.jpg").getAbsolutePath();
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        Result result = parseInfoFromBitmap(bitmap);
        if (result != null) {
            LogUtils.w(result);
            LogUtils.i("result info==" + result.getText());
        }
    }
});

Premise: add dependency in build.gradle:

implementation 'com.google.zxing:core:3.3.1'

I'm 3.3.0 here. I can also use the updated version, such as 3.3.1.

That's it. Pictures. You can take pictures by yourself, or you can choose among them. The only logic provided here is to parse the QR code image.

The logic of QR code image generation is provided in the link at the top of me.

Above.

Topics: Android Gradle Google