Barcode Scanner with CameraX and MLKit

Image for post
Image for post
Barcode and Android icons made by Freepik from www.flaticon.com

Intro

Image for post
Image for post

Overview

1. CameraX

The Setup

implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-lifecycle:$camerax_version"
implementation "androidx.camera:camera-view:1.0.0-alpha18"
apply plugin: 'kotlin-android-extensions'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

The View

The Code

This will change a little later, but it’s enough for the camera to start streaming into the view.

2. MLKit

The Setup

implementation 'com.google.mlkit:barcode-scanning:16.0.3'

The Code

typealias BarcodeListener = (barcode: String) -> Unit

3. Connecting the Parts

cameraExecutor = Executors.newSingleThreadExecutor()
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Written by

Passionate Android developer with tons left to learn. https://www.linkedin.com/in/miguellasa/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store