Building Barcode/QR code scanner for Android using Google ML Kit and CameraX
In this article, we will learn how to create Barcode scanner using Google ML Kit and Jetpack CameraX .
June 3,2020 A standalone library for on-device ML, which you can use with or without Firebase.
IntroductionWhat’s CameraX?CameraX is a Jetpack support library, built to help you make camera app development easier. It provides a consistent and easy-to-use API surface that works across most Android devices, with backward-compatibility to Android 5.0 (API level 21).While it leverages the capabilities of camera2, it uses a simpler, use case-based approach that is lifecycle-aware.
What’s Google ML Kit?ML Kit brings Google’s machine learning expertise to mobile developers in a powerful and easy-to-use package. Make your iOS and Android apps more engaging, personalized, and helpful with solutions that are optimized to run on device.
ML Kit’s Barcode Scanning APIWith ML Kit’s barcode scanning API, you can read data encoded using most standard barcode formats. Barcode scanning happens on the device, and doesn’t require a network connection.
Setting up the project
// ViewModel and LiveData implementation "androidx.lifecycle:lifecycle-livedata:2.2.0" implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0" // Barcode model dependencies implementation 'com.google.mlkit:barcode-scanning:16.0.1' // CameraX dependencies implementation "androidx.camera:camera-camera2:1.0.0-beta06" implementation "androidx.camera:camera-lifecycle:1.0.0-beta06" implementation "androidx.camera:camera-view:1.0.0-alpha13"
android { ,,,,compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } ,,,, }
3. Open your AndroidManifest.xml file to add required permissions and google play service meta-data:
<uses-sdk tools:overrideLibrary=" androidx.camera.camera2, androidx.camera.core, androidx.camera.view, androidx.camera.lifecycle" /> <uses-feature android:name="android.hardware.camera" /><uses-permission android:name="android.permission.CAMERA" /><!-- Add google service in your application --> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>,,,
4. Add PreviewView to the main activity layout (activity_main.xml).
PreviewView
Custom View that displays the camera feed for CameraX’s Preview use case.This class manages the Surface lifecycle, as well as the preview aspect ratio and orientation. Internally, it uses either a
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.camera.view.PreviewView android:id="@+id/preview_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
5. Now we need to check camera permission, here’s a code to check if camera permission was granted and request it.
Request Camera permission in android activity
Implement Preview use case
CameraX introduces use cases, which allow you to focus on the task you need to get done instead of spending time managing device-specific nuances. There are several basic use cases: Preview: get an image on the display Image analysis: access a buffer seamlessly for use in your algorithms, such as to pass into MLKit, we will use it to detect barcode. Image capture: save high-quality images
6. Implement camera preview use case To use a- Let’s do this in a view model, create new class CameraXViewModel, Create a livedata instance of b- Add a listener to the
CameraXViewModel
c- Now, Let’s setup camera in MainActivity, Create new function
private var lensFacing = CameraSelector.LENS_FACING_BACK cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()
Observe
Setup cameraX by observing processCameraProvider from CameraXViewModel
d- Inside
Bind camera preview use case
e- Please check my code sample on GitHub to get how to detect the most suitable ratio for dimensions provided, I added function there, you can see this on the full code here on branch camera_preview. By this we handled camera preview, now let’s handle analyzer part to detect bar code.
Detecting Barcode 🚀🚀🚀We’ve a great way to implement this using 7. Implement ImageAnalysis use case a- Create new function
analysisUseCase = ImageAnalysis.Builder() .setTargetAspectRatio(screenAspectRatio) .setTargetRotation(previewView!!.display.rotation) .build()analysisUseCase?.setAnalyzer(cameraExecutor, ImageAnalysis.Analyzer { imageProxy -> processImageProxy(barcodeScanner, imageProxy) })
b- In
Process camera image proxy function
Once the image being analyzed it’s closed by calling ImageProxy.close()
BTW, Implementing preview, image capture and image analysis concurrently will not work for Android Studio’s device emulator if you are running Q or lower. We recommend using a real device to test this portion of the code lab.
This’s how to detect barcode with new ML Kit which’s released on 30 June/2020, you can see full code here on Github.
References: https://developer.android.com/training/camerax https://developers.google.com/ml-kit/vision/barcode-scanning/android
Android News
Our Engineering Roadmap
By Mark Ng We just completed our engineering road map for our Android apps at Australia Post. Each year we get together and try to decide on what we are going to do from an engineering perspective for the next 12 months. Each team gets to decide on what should be done now, what they want to complete by the end of the year and whats on the horizon for next year.
By
ProAndroidDev -
droidcon News
Tech Showcases, Developer Resources & Partners
EmployerBrandingHeader
jobs.droidcon.com
![]() Latest Android Jobs
Kotlin Weekly
![]() Your weekly dose of Kotlin
ProAndroidDev
![]() Android Tech Blogs, Case Studies and Step-by-Step Coding
Zalando
![]() Meet one of Berlin's top employers
Academy for App Success
![]() Google Play resources tailored for the global droidcon community |
Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.