Scan QR Code/Barcode with Google Code Scanner

Pankaj Rai 🇮🇳
ProAndroidDev
Published in
3 min readApr 4, 2023

Scanning a QR or Barcode is one of the most common use cases across mobile applications. As live scanning of QR/Barcode requires a live feed of the camera hence it becomes necessity to request camera permission.

Now, the question is can we avoid asking for the camera permission and still achieve the same use case?
The shortest answer to this question is YES.

Google Code Scanner
It provides a complete solution for scanning codes without requiring your app to request camera permission. This adds up to the user's privacy as the app is no longer requesting camera permission and hence will not have access to the camera.

How does it work?
As the app is no longer requesting the camera permission hence it’s not feasible to scan the code within the app by opening an interface with CameraX, so what to do now?
Well, Google Code Scanner is well-defined for a very specific use case of scanning the code and it does nothing beyond that hence it is quite practical to take advantage of Google Play Services to achieve this task. So it delegates the task of scanning the code to Google Play services and returning only the scan results to the app.

How about security?
All image processing occurs on the device and Google doesn’t store the results or image data. Also as the app no longer requests camera permission just to scan code hence it gives confidence to the users while using the app.

When should I use it?
As it delegates the scanning part to Google Play Services hence changing the interface provided by the service will not be practical so if the requirement is to just scan without any custom UI then this might be a great option.

Let’s add Google Code Scanner

Add the mavenCentral() in settings.gradle

Add the dependency of a code scanner in the build.gradle

Add meta-data in the manifest file so that Google Play services automatically download the scanner module to the device while your app is installed from the Play Store. In case you do not add it to the meta-data then it will download the scanner module the first time it is used.

In case you know about the format that the app should scan then you may provide those as the setBarcodeFormats, this is helpful to increase the scanning speed.

Finally, create the scanner object by specifying the context and option (or just context) to GmsBarcodeScanning and thereafter use this object to scan code like following

Whenever the scan is successful it will trigger the addOnSuccessListener which will provide the barcode object. You may find the type of QR code or barcode, and read the value out of it by calling the “barcode.rawValue”

Adding such kind of scan functionality is as simple as adding a lib and calling the startScan(), also because it’s delegating the scan to Google Play Services hence it will not have much impact on the app size.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Pankaj Rai 🇮🇳

Software Engineer | GDE Android & Firebase | YouTuber — All Techies

Responses (2)

Recommended from Medium

Lists

See more recommendations