ZXing for Kotlin: Complete Guide with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ZXing for Kotlin: Complete Guide to Scanning QR Codes and Barcodes

In today's world of mobile apps and process automation, the ability to quickly read information from QR codes and barcodes has become the de facto standard. The ZXing library (pronounced "zebra crossing") is a powerful open-source tool that lets developers easily integrate recognition and generation of 1D (EAN, UPC, Code 128) and 2D (QR, Data Matrix, Aztec) codes into their applications.

This guide is designed for Kotlin developers who want to master ZXing—from basic setup to building advanced scanners with performance optimizations. We'll cover not only standard use cases but also the tricky details that often trip up beginners.

1. What Is ZXing and Why Use It?

ZXing is an image processing library specializing in barcode decoding and encoding. Originally created by Google and maintained by the community, its main advantages are cross-platform support (Java/Kotlin, C++, JavaScript, Python) and compatibility with a huge range of formats—from classic QR codes to specialized industrial codes like PDF417.

Why use ZXing in Kotlin? First, it's the gold standard for Android development. Second, the library provides ready-made classes for camera integration (via CameraX or Camera2), saving you from writing low-level video capture code. Third, ZXing works not only with live video but also with static images (gallery files), expanding use cases from scanning receipts to recognizing codes on product packaging in logistics.

2. Installation

For Kotlin (Android), installation is done via the Gradle build system. Add the following dependencies to your build.gradle file (Module: app):

dependencies {
    // Core ZXing library
    implementation("com.google.zxing:core:3.5.3")
    // Additional module for camera support (recommended)
    implementation("com.journeyapps:zxing-android-embedded:4.3.0")
}

If you're using Kotlin Multiplatform or pure server-side Kotlin (Ktor), you only need the core module:

implementation("com.google.zxing:core:3.5.3")

For desktop applications (JVM), add the dependency via Maven or Gradle similarly. After syncing your project, the library is ready to use.

3. Quick Start — Minimal Working Example

The easiest way to test ZXing is to launch the built-in scanning Activity. Simply call an Intent. Below is minimal code for an Android Activity in Kotlin:

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions

class MainActivity : AppCompatActivity() {

    // Register the contract to launch the scanner
    private val barcodeLauncher = registerForActivityResult(ScanContract()) { result ->
        if (result.contents == null) {
            Toast.makeText(this, "Scanning cancelled", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(this, "Result: ${result.contents}", Toast.LENGTH_LONG).show()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Configure scanning options
        val options = ScanOptions()
        options.setDesiredBarcodeFormats(ScanOptions.QR_CODE) // QR only
        op                    

Recommendations