QR code generation and scanning in Android

0. Preface

Today's article mainly describes the generation and scanning of QR code. Why do you talk about QR code when using the current popular zxing? Because QR code is too common, any android app will have QR code scanning. This article aims to help students in need to quickly complete the function of QR code generation and scanning.

1. Use of zxing

After downloading the project from GitHub, you can see the overall code structure as follows:

We only need to copy all the code under the zxing package to our project. In addition to these, we also need the zxing jar package, and finally the corresponding resource files, including the IDS file under the values file, the resource file in the raw file (which can be replaced), and the activity under the layout file_ Capture.xml (can be customized accordingly) and image resources.

2. Implementation of generating QR code

When all the above work is ready, we can create our QR code. How to generate QR code?

Encodingutils, a QR code generation tool class, is required. The QR code is generated by calling the createqrcode () method in the tool class. The method parameters are described as follows:

What is completed below is the generated QR code of a Baidu address, and the middle logo is a small Android robot. And save the picture locally to facilitate the subsequent test of the local reading function of QR code.

See the following effects:

3. Implementation of reading QR code

3.1 camera scanning mode

For QR code scanning, you need to use the captureactivity class to open the captureactivity interface and scan. After scanning, call back the onactivityresult() method to get the scanned result from onactivityresult(). The effect is not demonstrated because the simulator is used. The detailed code is as follows:

3.2 local image scanning method

To scan local images, we need to make corresponding modifications in captureactivity. Therefore, I added a button at the bottom of the scanning interface to select local images. The layout code is not shown here. Let's directly look at the event processing after clicking.

After opening the system picture library and selecting a picture, you need to override the onactivityresult() method to return the picture information.

Get picture path photo_ After path, the scanningImage () method is called for scanning. In the Zxing source code, the results are all stored in the Result result set. After the result is obtained, the result is returned. Read the captureactivity source code to know that the final result result set is passed to the handledecode () method.

After obtaining the picture path, its QR code information needs to be wrapped into a result object, so the picture needs to be parsed:

Get the bitmap according to the path, parse it into a result object through the decode method in qrcodereader and return it, and finally pass it to the handledecode method. The effect of running the program is as follows. What is scanned is the previously defined Baidu address.

Finally, don't forget to declare permissions and captureactivity.

You can refer to this article: a simple method for Android to scan and generate QR codes

The above is the QR code generation and scanning function in Android introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>