Android realizes the fast integrated QR code scanning function based on zxing

QR code scanning has always been a lot of application scenarios. Zxing, an open source project of Android, provides a complete and mature solution. If it is only for the purpose of rapid development, it can quickly integrate into its own project by slightly cutting the projects officially provided by zxing according to its own project needs. The following is a detailed demonstration and introduction of how to quickly integrate the QR code scanning function into your own project based on the source code officially provided by zxing.

(step 1): go to zxing's official homepage to download the latest project code package. Zxing's official homepage in GitHub: https://github.com/zxing , download and unzip. After decompression, there are several project directories under the root directory, of which: Android is the project we need. Import it into eclispse.

(step 2): zxing's Android project needs to reference two key library files: android-core-x.x.x.jar and core-x.x.x.jar, where x.x.x represents the version number. By the time this blog post was published, the version was 3.2.0. These two key android-core-x.x.x.jar and core-x.x.x.jar files can actually be compiled and generated from the source code downloaded in the first step. There are specific compilation schemes on the Internet, but in a simple period, the compiled files can also be downloaded directly from zxing's official website, The download link of Android core is: http://repo1.maven.org/maven2/com/google/zxing/android-core/ , another zxing core download link is: http://repo1.maven.org/maven2/com/google/zxing/core/ , select the latest version of the library file or the version number you need. After downloading, it is similar to the import and storage files in other Android projects, Import it into the LIBS directory in the Android project. If there is no LIBS, create a new directory named LIBS and put the two library files in it.

(step 3): as a demonstration, we will create a new mainactiviy as the initiator activity of the project, and the app will start our own mainactivity. Observe the androidmanifest.xml file officially provided by zxing:

In fact, zxing's official project has been used to provide integration code for third parties. For example, the key activity:. \ SRC \ com \ Google \ zxing \ client \ Android \ captureactivity.java has provided intent actions accessed from various portals in the statement. Therefore, in our newly created mainactivity, we can directly and implicitly specify an intent action and start it:

Because the purpose of starting zxing's captureactivity is not to start zxing's captureactivity to obtain QR code scanning results, we start it in the way of startactivityforresult(). Accordingly, we need to rewrite: protected void onactivityresult (int requestcode, intent data) to callback and wait for the returned result.

(step 4): this step is the focus. Methods in captureactivity.java under. \ SRC \ com \ Google \ zxing \ client \ Android \ Directory: public void handledecode (result rawresult, bitmap barcode, float scalefactor); This method is a callback function. The scanning module written in the zxing project returns the callback method after scanning. The official original public void handledecode (result rawresult, float scalefactor) method of zxing is as follows:

We will simplify this method and customize the content we need. In order to meet the needs of our own projects, the modified code of this method is:

(step 5): this step is relatively simple. It is the remaining finishing work. Modify the androidmanifest.xml file and start our mainactivity as the main activity. Take zxing's captureactivity as an ordinary activity.

Modified androidmanifest.xml file:

Note: This article is only the simplest example, which demonstrates how to make the minimum amount of changes in your own project based on the complete code provided by the official zxing project, and quickly integrate the QR code scanning function into your own project for my use. If you need more detailed adjustments, you need to deeply customize and adapt the source code.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>