Dev Center
Swift
Objective-C
Table of contents

User Guide for Android Edition

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 releated 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]\Lib\DynamsoftBarcodeReaderAndroid.aar and [INSTALLATION FOLDER]\Lib\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 Now. After the synchronization completes, the SDK is added to the project.

  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 Now. After the synchronization completes, the SDK is added to the project.

  4. Import the package in the file MainActivity.java

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

Initialize Camera Module

  1. Initialize the license.

    CameraEnhancer.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", new DCELicenseVerificationListener() {
       @Override
       public void DCELicenseVerificationCallback(boolean isSuccess, Exception error) {
          if(!isSuccess){
                error.printStackTrace();
          }
       }
    });
    

    Note:

    • Network connection is required for the license to work.
    • “DLS2***” is a default 7-day trial license used in the sample.
    • If the license has expired, please request a trial license through the customer portal.
  2. Create an instance of Camera Enhancer.

    CameraEnhancer mCameraEnhancer;
    mCameraEnhancer = new CameraEnhancer(MainActivity.this);
    
  3. 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" />
    
  4. Initialize the camera view and bind to the Camera Enhancer object.

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

Initialize Barcode Reader

  1. Create an instance of Dynamsoft Barcode Reader.

    BarcodeReader reader;
    reader = new BarcodeReader();
    
  2. Initialize the license.

    DMDLSConnectionParameters dbrParameters = new DMDLSConnectionParameters();
    dbrParameters.organizationID = "200001";
    reader.initLicenseFromDLS(dbrParameters, new DBRDLSLicenseVerificationListener() {
          @Override
          public void DLSLicenseVerificationCallback(boolean isSuccessful, Exception e) {
             if (!isSuccessful) {
                e.printStackTrace();
             }
          }
    });
    

    Note:

    • Network connection is required for the license to work.
    • The organization id 200001 here will grant you a time-limited public trial license.
    • If the license has expired, please request a trial license through the customer portal.
  3. Create text callback to obtain the recognized barcode results.

    TextResultCallback mTextResultCallback = new TextResultCallback() {
    // Obtain the recognized barcode results and display.
    @Override
       public void textResultCallback(int i, TextResult[] textResults, Object userData) {
             (MainActivity.this).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                   showResult(textResults);
                }
             });
       }
    };
    
  4. Create settings of video barcode reading and bind to Barcode Reader object

    // Create settings of video barcode reading.
    DCESettingParameters dceSettingParameters = new DCESettingParameters();
    
    // This cameraInstance 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.
    dceSettingParameters.cameraInstance = mCameraEnhancer;
    
    // Make this setting to get the result. The result will be an object that contains text result and other barcode information.
    dceSettingParameters.textResultCallback = mTextResultCallback;
    
    // Bind the Camera Enhancer instance to the Barcode Reader instance.
    reader.SetCameraEnhancerParam(dceSettingParameters);
    
  5. 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.StartCameraEnhancer();
       super.onResume();
    }
    
    @Override
    public void onPause() {
       // Stop video barcode reading
       reader.StopCameraEnhancer();
       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 recognized barcode result.

     <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("");
    }
    

You can download the complete source code here.

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.

Further Barcode Reading Settings

Decoding Methods

Regular barcode reading settings and modes parameter settings are available via PublicRuntimeSettings and JSON templates. The following typical settings you might find helpful:

For more scanning settings guide, please check the How To Guide section.

This page is compatible for:

Version 7.5.0

Is this page helpful?

YesYes NoNo

In this article:

version 8.8.0

  • Latest version (10.2.10)
  • Version 10.x
    • Version 10.0.21
    • Version 10.0.20
  • Version 9.x
    • Version 9.6.20
    • Version 9.6.11
    • Version 9.6.10
    • Version 9.6.0
    • Version 9.4.0
    • Version 9.2.13
    • Version 9.2.11
    • Version 9.2.10
    • Version 9.0.2
    • Version 9.0.1
    • Version 9.0.0
  • Version 8.x
    • Version 8.9.3
    • Version 8.9.1
    • 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.x
    • Version 7.6.0
    • Version 7.5.0
Change +