How to Integrate Dynamsoft’s Ready-to-Use Barcode Scanner API into Android Apps in Minutes

Xiao Ling
5 min readFeb 12, 2025

--

Dynamsoft has released a Ready-to-Use Barcode Scanner API for Android developers, enabling them to quickly integrate barcode scanning functionality into their apps. This component is open-source and free to customize. In this tutorial, we will walk you through the barcode scanner API and demonstrate how to integrate it into Android apps in just minutes.

Why Use the Ready-to-Use Barcode Scanner API?

Many mobile apps require barcode scanning capabilities for account login, payment, inventory management, and more. However, building a barcode scanner from scratch can be both time-consuming and complex. Dynamsoft’s Ready-to-Use Barcode Scanner API simplifies the process by providing a pre-built component that can be easily integrated into your app.

What Can the Ready-to-Use Barcode Scanner API Do?

  • Supports scanning various barcode types, including QR Code, DataMatrix, PDF417, and more.
  • Offers two scanning modes: Single Barcode and Multiple Barcodes. The Single Barcode mode is ideal for scanning one barcode at a time. If multiple barcodes are detected in this mode, a selection view is presented to the user. The Multiple Barcodes mode is designed for simultaneously scanning multiple barcodes.
  • Allows parameter customization via JSON configuration for algorithm optimization.
  • Includes a pre-designed UI for barcode scanning, featuring: A torch button for toggling the flashlight. A close button to exit the scanner. A scanning viewfinder to guide users. A scan laser animation for visual feedback.
  • Additional features include auto-focus, auto-zoom, and beep sound feedback for successful scans.

For more details, visit the Ready-to-Use Barcode Scanner API documentation.

Prerequisites

Setting Up Your Android Project

  1. Scaffold a new Android project with the “ Empty Views Activity “ template in Android Studio.

Click “ Next”, then change the “ Build Configuration Language” to “ Groovy DSL (build.gradle)

2. In the “ settings.gradle “ file, add the Dynamsoft Maven repository as follows:

pluginManagement {
repositories {
google {
content {
includeGroupByRegex("com\\.android.*")
includeGroupByRegex("com\\.google.*")
includeGroupByRegex("androidx.*")
}
}
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven {
url "https://download2.dynamsoft.com/maven/aar"
}
}
}

rootProject.name = "BarcodeScanner"
include ':app'

3. In the “ app/build.gradle “ file, add the following dependencies:

dependencies {
implementation 'com.dynamsoft:dynamsoftbarcodereaderbundle:10.4.3000'
}

Implementing an Android Barcode Scanner in Minutes

  1. Add a text view and two buttons to the “ activity_main.xml” layout file. The text view will display the barcode scanning results, while the two buttons will trigger the Single Barcode and Multiple Barcodes scanning modes.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<Button
android:id="@+id/btn_single"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_single_scan"
android:backgroundTint="@color/teal_200"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_multi"
android:layout_marginEnd="8dp" />

<Button
android:id="@+id/btn_multi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_multi_scan"
android:backgroundTint="@color/teal_200"
android:textColor="@android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/btn_single"
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/tv_result"
android:layout_width="0dp"
android:layout_height="0dp"
android:textSize="20sp"
android:textIsSelectable="true"
android:scrollbars="vertical"
android:overScrollMode="always"
android:padding="16dp"
android:background="@android:color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btn_single"
android:layout_marginBottom="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

2. In the “ MainActivity.java” file, implement the barcode scanning logic. Replace the LICENSE-KEY placeholder with your actual Dynamsoft Barcode Reader license key:

package com.example.barcodescanner;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.dynamsoft.dbr.BarcodeResultItem;
import com.dynamsoft.dbrbundle.ui.*;

public class MainActivity extends AppCompatActivity {
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
private BarcodeScannerConfig config = new BarcodeScannerConfig();
private TextView textView;
private final String LICENSE_KEY = "LICENSE-KEY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});

config.setLicense(LICENSE_KEY);

launcher = registerForActivityResult(new BarcodeScannerActivity.ResultContract(), result -> {
if (result.getResultStatus() == BarcodeScanResult.EnumResultStatus.RS_FINISHED && result.getBarcodes() != null) {
textView.setText("");
for (int i = 0; i < result.getBarcodes().length; i++) {
BarcodeResultItem barcode = result.getBarcodes()[i];
String content = String.format("Result %d:\nFormat: %s\nContent: %s\n\n", i,
barcode.getFormatString(),
barcode.getText());

textView.append(content);
}
} else if(result.getResultStatus() == BarcodeScanResult.EnumResultStatus.RS_CANCELED ){
textView.setText("Scan canceled.");
}
if (result.getErrorString() != null && !result.getErrorString().isEmpty()) {
textView.setText(result.getErrorString());
}
});
findViewById(R.id.btn_single).setOnClickListener(this::handleButtonClick);
findViewById(R.id.btn_multi).setOnClickListener(this::handleButtonClick);

textView = findViewById(R.id.tv_result);
}

private void handleButtonClick(View v) {
int id = v.getId();
if (id == R.id.btn_single) {
config.setScanningMode(EnumScanningMode.SM_SINGLE);
} else if (id == R.id.btn_multi) {
config.setScanningMode(EnumScanningMode.SM_MULTIPLE);
}
launcher.launch(config);
}
}

3. Run the app on an Android device . Click the Single Barcode button to scan a single barcode or the Multiple Barcodes button to scan multiple barcodes.

Customize the Barcode Scanner UI from the Source Code

If the default UI does not meet your requirements, you can customize the barcode scanner UI by modifying the source code. The source code is available on GitHub: https://github.com/Dynamsoft/barcode-reader-mobile/tree/main/android/src/DynamsoftBarcodeReaderBundle.

Steps to Add the Barcode Scanner Source Code to Your Android Project

  1. Clone the source code from GitHub.
  2. Create a new module named dbrbundle in your Android project. Ensure the package name is com.dynamsoft.dbrbundle.

3. Copy the source code files from the cloned repository into the dbrbundle module.

4. Update the build.gradle file in the dbrbundle module.

plugins {
id "com.android.library"
}

ext {
releaseVersion = "10.4.3001"
}

def localPublishFileExists = file('local_publish.gradle').exists()

if (localPublishFileExists) {
apply from: 'local_publish.gradle'
}

android {
namespace 'com.dynamsoft.dbrbundle'
compileSdk 33

defaultConfig {
minSdk 21
targetSdk 33
versionCode 1
versionName "10.4.2003"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
api "com.dynamsoft:dynamsoftlicense:3.4.40"
api "com.dynamsoft:dynamsoftcore:3.4.30"
api "com.dynamsoft:dynamsoftcameraenhancer:4.2.22"
api "com.dynamsoft:dynamsoftcapturevisionrouter:2.4.30"
api "com.dynamsoft:dynamsoftbarcodereader:10.4.30"
api "com.dynamsoft:dynamsoftimageprocessing:2.4.31"
api "com.dynamsoft:dynamsoftutility:1.4.30"

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
}

5. In app/build.gradle, replace the dependency with the local module:

dependencies {
// implementation 'com.dynamsoft:dynamsoftbarcodereaderbundle:10.4.3001'

implementation project(':dbrbundle')
}

Customize the Barcode Scanner UI

Suppose you want to display multiple barcode results in real-time and return the results by clicking a button. Follow these steps:

  1. Add a button to layout/activity_scanner_barcode.xml:
<Button
android:id="@+id/btn_capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@id/action_view"
android:layout_marginBottom="16dp"
android:text="Capture"
android:src="@drawable/circle"/>

2. Modify the Java code in BarcodeScannerActivity.java:

private Button btnCapture;
private DecodedBarcodesResult mResult;

protected void onCreate(@Nullable Bundle savedInstanceState) {
...

btnCapture = findViewById(R.id.btn_capture);
...
}

protected void onResume() {
...

mRouter.addResultReceiver(new CapturedResultReceiver() {
@Override
public void onDecodedBarcodesReceived(@NonNull DecodedBarcodesResult result) {
if (scanMode == EnumScanningMode.SM_SINGLE) {
resultSingle(result);
} else {
// resultMultiple(result);
mResult = result;
}
}
});

mRouter.startCapturing(templateName, new CompletionListener() {
@Override
public void onSuccess() {
...
initCaptureButton();
}

...
});
}

private void initCaptureButton() {
if (scanMode == EnumScanningMode.SM_SINGLE) {
btnCapture.setVisibility(View.GONE);
}
else {
btnCapture.setVisibility(View.VISIBLE);
btnCapture.setOnClickListener(v -> {
if (mResult != null) {
resultOK(BarcodeScanResult.EnumResultStatus.RS_FINISHED, mResult.getItems());
finish();
}
});
}
}

3. Re-run the app to see the changes.

Source Code

https://github.com/yushulx/android-camera-barcode-mrz-document-scanner/tree/main/examples/10.x/BarcodeScanner

Originally published at https://www.dynamsoft.com on February 12, 2025.

--

--

Xiao Ling
Xiao Ling

Written by Xiao Ling

Manager of Dynamsoft Open Source Projects | Tech Lover

No responses yet