Dev Center
Table of contents

Thanks for downloading Dynamsoft Barcode Reader Package!

Your download will start shortly. If your download does not begin, click here to retry.

Getting Started with Android

Requirements

  • Operating systems:
    • Supported OS: Android 5 or higher (Android 7 or higher recommended).
    • Supported ABI: armeabi-v7a, arm64-v8a, x86 and x86_64.
  • Environment: Android Studio 3.4+.

Installation

You can download the Dynamsoft Barcode Reader (DBR) SDK from the Dynamsoft website if you don’t have the SDK installed yet.

After unzipping, the root directory of the DBR installation package is DynamsoftBarcodeReader (the [INSTALLATION FOLDER]). You can find the following two aar files under the [INSTALLATION FOLDER]\Lib directory:

File Description
DynamsoftBarcodeReaderAndroid.aar The Barcode Reader library, including all barcode decoding related algorithms and APIs.
DynamsoftCameraEnhancerAndroid.aar The Camera Enhancer library, including camera control APIs and frame preprocessing algorithm.

Build Your First Application

In this section, let’s see how to create a HelloWorld app for reading barcodes from camera video input.

Note:

  • Android Studio 4.2 is used here in this guide.
  • You can get the source code of the HelloWord app here.

Create a New Project

  1. Open Android Studio, select File > New > New Project.

  2. Choose the correct template for your project. In this sample, we use Empty Activity.

  3. When prompted, choose your app name ‘HelloWorld’ and set the Save location, Language, and Minimum SDK (we use 21 here).

    Note:

    • With minSdkVersion set to 21, your app is compatible with more than 94.1% of devices on the Google Play Store (last update: March 2021).

Include the Library

There are two ways to include the SDK into your project - local binary dependency and remote binary dependency.

Local Binary Dependency

  1. Copy the file [INSTALLATION FOLDER]\Libs\DynamsoftBarcodeReaderAndroid.aar and [INSTALLATION FOLDER]\Libs\DynamsoftCameraEnhancerAndroid.aar to the target directory HelloWorld\app\libs

  2. Open the file HelloWorld\app\build.gradle and add reference in the dependencies:

     dependencies {
         implementation fileTree(dir: 'libs', include: ['*.aar'])
     }
    
  3. Click Sync Project with Gradle Files. After the synchronization completes, the SDK is added to the project.

    Note:

    • If your Android SDK version is 28 or higher, you have to add android.enableJetifer=true in your gradle.properties file before you sync your project with Gradle files.
  4. Import the package in the file MainActivity.java

    import com.dynamsoft.dbr.*;
    import com.dynamsoft.dce.*;
    

Remote Binary Dependency

  1. Open the file HelloWorld\app\build.gradle and add the remote repository:

     repositories {
          maven {
             url "https://download2.dynamsoft.com/maven/dbr/aar"
          }
          maven {
             url "https://download2.dynamsoft.com/maven/dce/aar"
          }
     }
    
  2. Add reference in the dependencies:

    dependencies {
       implementation 'com.dynamsoft:dynamsoftbarcodereader:{version-number}@aar'
       implementation 'com.dynamsoft:dynamsoftcameraenhancer:{version-number}@aar'
    }
    

    Note:

    • Please replace {version-number} with the correct version number.
  3. Click Sync Project with Gradle Files. After the synchronization completes, the SDK is added to the project.

    Note:

    • If your Android SDK version is 28 or higher, you have to add android.enableJetifer=true in your gradle.properties file before you sync your project with Gradle files.
  4. Import the package in the file MainActivity.java

    import com.dynamsoft.dbr.*;
    import com.dynamsoft.dce.*;
    

Initialize License

   BarcodeReader.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", new DBRLicenseVerificationListener() {
      @Override
      public void DBRLicenseVerificationCallback(boolean isSuccess, Exception error) {
         if(!isSuccess){
             error.printStackTrace();
         }
      }
   });

Note:

  • Network connection is required for the license to work.
  • The string “DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9” here will grant you a time-limited public trial license.
  • If the license has expired, you can go to the customer portal to request for an extension.

Initialize Camera Module

  1. Create an instance of Camera Enhancer.

    CameraEnhancer mCameraEnhancer;
    mCameraEnhancer = new CameraEnhancer(MainActivity.this);
    
  2. In the Project window, open app > res > layout > activity_main.xml and create a DCE camera view section under the root node.

    <com.dynamsoft.dce.DCECameraView
       android:id="@+id/cameraView"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:layout_editor_absoluteX="25dp"
       tools:layout_editor_absoluteY="0dp" />
    
  3. Initialize the camera view and bind to the Camera Enhancer object.

     DCECameraView cameraView;
    
     cameraView = findViewById(R.id.cameraView);
     mCameraEnhancer.setCameraView(cameraView);
    

Initialize Barcode Reader

  1. Create an instance of Dynamsoft Barcode Reader.

    BarcodeReader reader;
    reader = new BarcodeReader();
    
  2. Create a barcode result listener and register with the barcode reader instance to get recognized barcode results.

    TextResultListener mTextResultListener = new TextResultListener() {
    // Obtain the recognized barcode results and display.
    @Override
       public void textResultCallback(int id, ImageData imageData, TextResult[] textResults) {
             (MainActivity.this).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                   showResult(textResults);
                }
             });
       }
    };
    reader.setTextResultListener(mTextResultListener);
    
  3. Bind the camera enhancer instance as an image source to the barcode reader instance and start scanning.

    // Bind the Camera Enhancer instance to the Barcode Reader instance.
    // The mCameraEnhancer is the instance of the Dynamsoft Camera Enhancer.
    // The Barcode Reader will use this instance to take control of the camera and acquire frames from the camera to start the barcode decoding process.
    reader.setCameraEnhancer(mCameraEnhancer);
    // Start the barcode scanning thread.
    reader.startScanning();
    
  4. Override the MainActivity.onResume and MainActivity.onPause functions to start/stop video barcode scanning. After scanning starts, the Barcode Reader will automatically invoke the decodeBuffer API to process the video frames from the Camera Enhancer, then send the recognized barcode results to the text result callback.

    @Override
    public void onResume() {
    // Start video barcode reading
       reader.startScanning();
       try {
          mCameraEnhancer.open();
       } catch (CameraEnhancerException e) {
          e.printStackTrace();
       }
       super.onResume();
    }
    
    @Override
    public void onPause() {
       // Stop video barcode reading
       reader.stopScanning();
       try {
          mCameraEnhancer.close();
       } catch (CameraEnhancerException e) {
          e.printStackTrace();
       }
       super.onPause();
    }
    

Additional Steps

  1. In the Project window, open app > res > layout > activity_main.xml, create a text view section under the root node to display the recognized barcode results. The following sample code is a TextView under RelativeLayout

     <TextView
         android:id="@+id/tv_res"
         android:layout_width="match_parent"
         android:layout_height="200dp"
         android:layout_marginTop="430dp"
         android:textSize="16sp"
         android:gravity="center"
         android:scrollbars="vertical"
         android:textColor="@color/white"
         android:visibility="visible"/>
    
  2. Display barcode result in TextView.

    TextView tvRes;
    
    // Add TextView to display recognized barcode results.
    tvRes = findViewById(R.id.tv_res);
    
    private void showResult(TextResult[] results) {
    if (results != null && results.length > 0) {
       String strRes = "";
       for (int i = 0; i < results.length; i++)
             strRes += results[i].barcodeText + "\n\n";
       tvRes.setText(strRes);
    } else {
       tvRes.setText("");
    }
    

Build and Run the Project

  1. Select the device that you want to run your app on from the target device drop-down menu in the toolbar.

  2. Click the Run app button, then Android Studio installs your app on your connected device and starts it.

You can download the complete source code here:

Next Steps

From this page, you have learned how to create a simple video barcode decoding app. In the next steps, the following pages will help you on adding configurations to enhance your barcode reader.

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

latest version

  • Latest version
  • Version 9.0.0
  • Version 8.9.3
  • Version 8.9.0
  • Version 8.8.0
  • Version 8.6.0
  • Version 8.4.0
  • Version 8.2.1
  • Version 8.2.0
  • Version 8.1.2
  • Version 8.1.0
  • Version 8.0.0
  • Version 7.6.0
  • Version 7.5.0
Change +
© 2003–2022 Dynamsoft. All rights reserved.
Privacy Statement / Site Map / Home / Purchase / Support