In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
I would like to share with you how to use the official QR code scanning library launched by Google in Android. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it.
I believe that QR code scanning is not uncommon now, and almost all App will support this function.
Here I would like to ask you a question: how do you all add QR code scanning to your App?
I believe a large number of friends will say that they are using open source libraries such as ZXing or ZBar.
But have you ever thought about why Google does not provide an official QR code scanning library when the QR code function is so common?
Anyway, I haven't thought about it. Looking for open source when there is demand may have become the normal thinking of many Android developers.
But to my surprise, the official QR code scanning library, it is really coming.
Because I am the GDE of Google, sometimes I can experience some API that Google is not officially open to the public and is still under development.
Not long ago, I received an email from Google that read as follows:
When I read the email that Google was going to launch a QR code and bar code scanning library, I was immediately attracted and told Google that I wanted to try something new.
After several twists and turns, I finally got through the whole process of this library. Generally speaking, the experience is not bad, there are advantages and disadvantages, let me analyze them one by one.
The first advantage is that API is very simple, stupid interface, beginners can use. The cost is much lower than that of ZXing and ZBar.
Second, more protect the privacy of users. Because its function is based on the Intent mechanism and entrusts the Android system to adjust the mobile camera, the system scans the results and then returns the information to us, so that we do not have to apply for camera permission in our own App.
As for the disadvantages, the biggest problem is that it relies on Google Play Service, so for domestic mobile phones, it is basically impossible to use (although there are other solutions below). This is not a disadvantage if your products are aimed at overseas markets.
In addition, I tested that the recognition efficiency of QR code is not very high, at least it can not be compared with that of Wechat at present. But after all, the later upgrade and maintenance depends on Google, and the service is guaranteed. I believe that the identification efficiency will be significantly improved in the future.
So much for the pros and cons, and if you're interested in this library and want to try it like me, then the next part of this article is to teach you how to integrate and use this library for QR code scanning.
First of all, make sure that your phone has Google Play Service installed, and the version is above 21.45.15, otherwise the later code will not work.
In addition, this library is not online yet, so it cannot be integrated into our project through online maven repository. Instead, download the library locally and integrate it through a local maven repository.
We do it step by step.
The download address of this library was originally on Google Drive, but because I need permission to access the download, I uploaded it to Baidu network disk and shared it with you.
Link: https://pan.baidu.com/s/1yhVQqQUZ9goLaT8P4OzTVA?pwd=1234
Extraction code: 1234
After the download is complete, create a .m2 / repository directory under your computer's currently logged-in user directory, and move the downloaded library to this directory to extract (note that the extracted top-level directory must be the com directory).
Linux or Mac users can use the following command to do the above, Windows users please do it manually.
Mkdir-p ~ / .m2/repository
Cd / .m2/repository
Unzip
Next, create a new project in Android Studio and add the following configuration to the build.gradle file in the project root:
Allprojects {
Repositories {
Google ()
MavenCentral ()
MavenLocal () / / added
}
}
Be sure to add the line mavenLocal () so that it can be loaded into the QR code scan library that 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 this QR code scanning library of Google into our project.
Then we introduce the usage, which is even easier to use. Because its essence is to initiate an Intent to adjust the camera program of the system, and then get the value of the scan result. There is no need to declare permissions or apply for permissions dynamically, which can be done by calling an API. The code is as follows:
Class MainActivity: AppCompatActivity () {override fun onCreate (savedInstanceState: Bundle?) {super.onCreate (savedInstanceState) setContentView (R.layout.activity_main) val button = findViewById (R.id.button) val textView = findViewById (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, we call the getClient () method of GmsBarcodeScanning to get an instance of GmsBarcodeScanner.
Then call startScan () to start scanning the QR code and call the addOnSuccessListener () method to register a callback listener. When we get the scan result of the QR code, we display the scan result on TextView.
The overall code is very simple, along with the layout of activity_main.xml, which is also very simple:
Now run the program, and the effect is as follows:
As you can see, the entire QR code scanning interface is provided to us by Google, and the scan box is also a typical Google style. We only need to call API to get the scan results, and the access cost is very low.
However, such a convenient QR code scanning library can only benefit overseas developers. Because there is no Google Play Service in China, we cannot directly enjoy the convenience brought by this library.
So I also put this question to Google, for Chinese developers, what if they also want to use this QR code scanning library?
The answer is that Google binds ML Kit barcode scanner as part of Google Play Service and installs it on various phones, and if we don't have Google Play Service on our phones, then we need to integrate the full version of ML Kit barcode scanner into our project.
The above is all the contents of the article "how to use the official QR Code scanning Library launched by Google in Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.