Is Android finally going to launch Google's official QR code scanning library?

Is Android finally going to launch Google's official QR code scanning library?

I believe that QR code scanning is no longer a novelty to everyone, and almost all apps support this function.

Here I want to ask everyone a question, how do you add the QR code scanning function to your app?

I believe that many of my friends will say that they use open source libraries such as ZXing or ZBar.

But have you ever thought about it? QR code is so common, why doesn't Google provide an official QR code scanning library?

Anyway, I haven't thought about it. If there is a demand, look for open source. This may have become the normal thinking of many Android developers.

But what I didn’t expect was that the official QR code scanning library is really coming.

Since I am Google's GDE, sometimes I can experience in advance some APIs that Google has not yet officially opened to the public and are still under development.

Not long ago, I received an email from Google that read:

When I saw in the email that Google was going to launch a QR code and barcode scanning library, I was immediately attracted to it and expressed to Google my desire to try it out.

After several attempts, I finally got the whole process of this library to run smoothly. Overall, the experience is good, with both advantages and disadvantages. Let me analyze them one by one.

The first advantage is that the API is very simple, a fool-proof interface that even beginners can use, and the cost of studying is much lower than that of ZXing and ZBar.

Secondly, it better protects user privacy because its function is based on the Intent mechanism to entrust the Android system to call up the phone camera. After the system scans the result, it returns the information to us, so we don't need to apply for camera permissions in our own App.

As for the disadvantages, the biggest problem is that it relies on Google Play Service, so it is basically completely unusable for domestic mobile phones (but there are other solutions below). If your product is for the overseas market, this is not a disadvantage.

In addition, I tested it and found that the recognition efficiency of the QR code is not very high, at least it cannot be compared with the recognition efficiency of WeChat. However, after all, the subsequent upgrades and maintenance are all done by Google, and the service is guaranteed. I believe that the recognition efficiency will be significantly improved in the future.

That’s all for the advantages and disadvantages. If you are interested in this library and want to try it out like me, the rest of this article will teach you how to integrate and use this library for QR code scanning.

First, make sure that your phone has Google Play Service installed and the version is above 21.45.15, otherwise the following code will not work.

In addition, this library is not yet online, so it cannot be integrated into our project through the online Maven repository. Instead, we need to download this library to the local computer first, and then integrate it through the local Maven repository.

We proceed step by step.

The download address of this library was originally on Google Drive, but since permissions are required to access the download, I uploaded it to Baidu Netdisk to share with everyone.

Link: https://pan.baidu.com/s/13nhR3ZXsa9ELBIX6giUCQg Extraction code: gfs8

After the download is complete, create a .m2/repository directory in the current logged-in user directory of your computer, and move the downloaded library to this directory and unzip it (note that the top-level directory after unzipping must be the com directory).

Linux or Mac users can use the following commands to complete the above operations, while Windows users can perform the operations manually.

 mkdir -p ~/ .m2 / repository
cd ~/ .m2 / repository
unzip < downloaded SDK zip file >

Next, create a new project in Android Studio and add the following configuration to the build.gradle file in the project root directory:

 allprojects {
repositories {
google ( )
mavenCentral ( )
mavenLocal ( ) // added
}
}

Note that you must add the line mavenLocal() so that you can load the QR code scanning library we downloaded locally.

Then add the following dependencies to the build.gradle file in the app directory:

 dependencies {
...
implementation 'com.google.android.gms:play-services-code-scanner:16.0.0-eap1'
}

So far, we have successfully integrated Google's QR code scanning library into our project.

Next, let's introduce the usage, which is even simpler. Because its essence is to initiate an Intent to call the system's camera program, and then get the scan result value. No permission declaration is required, nor is dynamic permission application required. Just call an API to get it done. The code is as follows:

 class MainActivity : AppCompatActivity ( ) {

override fun onCreate ( savedInstanceState : Bundle? ) {
super .onCreate ( savedInstanceState )
setContentView ( R .layout .activity_main )
val button = findViewById < Button > ( R .id .button )
val textView = findViewById < TextView > ( R .id .text_view )
button .setOnClickListener {
val scanner = GmsBarcodeScanning .getClient ( applicationContext )
scanner .startScan ( ) .addOnSuccessListener {
val result = it .rawValue
textView .text = result
}
}
}

}

Here, when we click a button, the getClient() method of GmsBarcodeScanning is called to get an instance of GmsBarcodeScanner.

Then call startScan() to start scanning the QR code, and call addOnSuccessListener() to register a callback listener. When the QR code scanning result is obtained, we will display the scanned result on the TextView.

The overall code is very simple. In addition, the layout content of activity_main.xml is also very simple:

 < ?xml version = "1.0" encoding = "utf-8" ? >
< LinearLayout xmlns : android = "http://schemas.android.com/apk/res/android"
android : layout_width = "match_parent"
android : layout_height = "match_parent"
android : orientation = "vertical" >
< Button
android : id = "@+id/button"
android : layout_width = "wrap_content"
android : layout_height = "wrap_content"
android : text = "Scan Code" />

< TextView
android : id = "@+id/text_view"
android : layout_width = "wrap_content"
android : layout_height = "wrap_content" />
</LinearLayout>

Now run the program, the effect is as shown below:

As you can see, the entire QR code scanning interface is provided by Google, and the scanning box is also in a typical Google style. We only need to call the API to get the scanning results, and the access cost is very low.

However, such a convenient QR code scanning library can only benefit overseas developers. Since there is no Google Play Service in China, we cannot directly enjoy the convenience brought by this library.

So I also asked this question to Google. What should Chinese developers do if they also want to use this QR code scanning library?

The answer I got was that Google bundled ML Kit barcode scanner as part of Google Play Service and installed it on various mobile phones. If our phone does not have Google Play Service, we need to integrate the full version of ML Kit barcode scanner into our project.

I also got the complete version of the integration document, the link is as follows:

https://developers.google.com/ml-kit/vision/barcode-scanning/android

The above document also includes the function of how to parse a QR code from an image or a Bitmap object. Please refer to the document for details. I will not explain it here.

<<:  The pictures downloaded from WeChat cannot be Photoshopped! Teach you how to make PS support WebP

>>:  iOS 15.4 released: What's new in iOS 15.4

Recommend

The festive flower beds in our memories must have its presence

As National Day approaches, I would like to intro...

Hey, Facebook is going to "copy" WeChat?

[[259310]] Facebook is learning from WeChat and i...

Eleven Trends Predicted for New Consumer Brands in 2022

The collective rise of new consumer brands is und...

Credit card interest suspension installment conditions

Credit card interest suspension installment condi...

Campus mobile Internet: a tough piece of fat to swallow

In the minds of entrepreneurs in the mobile Inter...

How can Internet + smart hardware save your privacy?

In recent years, with the popularization of the I...

6 information flow industry cases and delivery data, form costs exposed!

Today, Qingguajun will share with you the analysi...

February Marketing Node Reminder [Dry Goods Collection]

February 2020 is coming soon, and it is also a mo...

Why is the CPU frequency of Xiaomi 5, which costs 1999 yuan, so low?

The three versions of Xiaomi 5 all have the same ...