Thanks for downloading Dynamsoft Barcode Reader Package!
Your download will start shortly. If your download does not begin, click here to retry.
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. |
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.
Open Android Studio, select File > New > New Project.
Choose the correct template for your project. In this sample, we use Empty Activity.
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).
There are two ways to include the SDK into your project - local binary dependency and remote binary dependency.
Copy the file [INSTALLATION FOLDER]\Libs\DynamsoftBarcodeReaderAndroid.aar
and [INSTALLATION FOLDER]\Libs\DynamsoftCameraEnhancerAndroid.aar
to the target directory HelloWorld\app\libs
Open the file HelloWorld\app\build.gradle
and add reference in the dependencies:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar'])
}
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 yourgradle.properties
file before you sync your project with Gradle files.
Import the package in the file MainActivity.java
import com.dynamsoft.dbr.*;
import com.dynamsoft.dce.*;
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"
}
}
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.
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 yourgradle.properties
file before you sync your project with Gradle files.
Import the package in the file MainActivity.java
import com.dynamsoft.dbr.*;
import com.dynamsoft.dce.*;
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.
Create an instance of Camera Enhancer.
CameraEnhancer mCameraEnhancer;
mCameraEnhancer = new CameraEnhancer(MainActivity.this);
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" />
Initialize the camera view and bind to the Camera Enhancer object.
DCECameraView cameraView;
cameraView = findViewById(R.id.cameraView);
mCameraEnhancer.setCameraView(cameraView);
Create an instance of Dynamsoft Barcode Reader.
BarcodeReader reader;
reader = new BarcodeReader();
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);
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();
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();
}
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"/>
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("");
}
Select the device that you want to run your app on from the target device drop-down menu in the toolbar.
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:
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.
latest version