Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

qr code - Android QRCode Scanner Library


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While Sean Owen and others that have worked on the original Zxing library had provided an approach to work with the barcode libraries for the past several years, Google has finally put out an official release with Google Play Services for handling qr and barcodes.

The barcode detection library is described here. The inclusion of these libraries will make for a smooth integration. I'll repost with some sample code for achieving these results from a captured image. At the moment, I wanted to update my answer for this official release. If this indeed does provide a nice way to get at this information (without jumping through hoops and complications), then I'll update with the source and check this off as an accepted answer.

The detection library that Google has provided within the past year has been a much easier library to work with. It allows for quick integration with the camera APIs and extracts the information with simplicity. This would be the component that I would suggest going forward with recognition. A quick snippet is demonstrated below for handling a Qr-code. A handful of pseudocode is left in there as well.

public final void analyzeFrameForQrCode(byte[] qrCodePictureF, int imageFormatF, XriteSize previewWindowSizeF)
{
    if(!qrCodeDetectionPossible() || qrCodePictureF == null || mIsAnalyzingQrCodeFrame)
    {
        return;
    }

    ... //Bitmap conversion code

    Frame frame = new Frame.Builder().setBitmap(pictureTaken).build();
    SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame);
    if(barcodes != null && barcodes.size() != 0)
    {
        Barcode qrCode = barcodes.valueAt(0);//barcodes.get(Barcode.QR_CODE);
        if(qrCode != null)
        {
             if(extractInformationFromQrCode(qrCode.rawValue)) {
                    mIsRequestingBarcodeDetection = false;
                    vibrateForQrCodeDiscovery();
                    ((Activity)mContext).runOnUiThread(new Runnable() {
                        @Override
                        public void run()
                        {
                            hideBarcodeDetection(true);
                        }
                    });
                }
            }
        }

     ... //Cleanup and code beyond Qr related material

   } 
}

There are of course other calls available that can be taken advantage of. But there are really only a couple lines in there. The service for analyzing the frames with the library are not there by default on devices however. So, you should check whether or not the library is available (such as when internet is not available) before calculating as well. This is a slight nuisance of it. I had assumed it would be available as updates for devices going forward as part of the support library or Google Services going out to all devices. But it needs the communication first with an external service to use these library calls. Once it does this one time then that device is good from that moment on.

In my small example, I pop a toast up after a check and then back out of the activity and let the user check their connection. This can be done with a small amount of sample code as well.

if(!mBarcodeDetector.isOperational())
{
    updateUserInstructions("The barcode library cannot be downloaded");
    return false;
}

Edit (Update):

A considerable amount of time has passed since working with the latest Google Play Services Vision libraries available for barcode detection. While the limitation for needing to download the library over the wifi is indeed a limitation, it is a one time process. And lets be honest...

...our devices will have a connection. The library itself is downloaded in the background so you don't even notice it happening unless there is trouble downloading it and then you would have to report an appropriate corrective measure such as enabling a connection to the internet for it.

One additional tidbit is that it is a little tricky sometimes with how you integrate the library into your application. Using it as a library project worked on some devices and then failed on others. Adding the jar to the build path worked across a broader number of devices (it could be all, but it solved a problem). So as such, I would do it using the secondary method when including it in your projects for now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...