| layout | default-layout |
|---|---|
| title | User Guide - Dynamsoft Barcode Reader for Android |
| description | This is the user guide of Dynamsoft Barcode Reader for Android SDK. |
| keywords | user guide, android |
| needAutoGenerateSidebar | true |
| needGenerateH3Content | true |
| noTitleIndex | true |
- Supported OS: Android 5.0 (API Level 21) or higher.
- Supported ABI: armeabi-v7a, arm64-v8a, x86 and x86_64.
- Development Environment:
- IDE: Android Studio 2024.3.2 suggested.
- JDK: Java 17 or higher.
- Gradle: 8.0 or higher.
There are two ways to add the libraries into your project - Manually and Maven.
-
Open the file
>- groovy >- kts > >1. ```groovy dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url "https://download2.dynamsoft.com/maven/aar" } } } ``` 2. ```kotlin dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url = uri("https://download2.dynamsoft.com/maven/aar") } } } ``` > If you are using gradle 6.x or older version, the maven dependencies should be configured in `[App Project Root Path]\app\build.gradle`[App Project Root Path]\settings.gradleand add the Maven repository: -
Open the file
>- groovy >- kts > >1. ```groovy dependencies { implementation 'com.dynamsoft:barcodereaderbundle:11.4.1300' } ``` 2. ```kotlin dependencies { implementation("com.dynamsoft:barcodereaderbundle:11.4.1300") } ```[App Project Root Path]\app\build.gradleand add the dependencies: -
Click Sync Now. After the synchronization is complete, the SDK is added to the project.
-
Download the SDK package from the Dynamsoft Website. After unzipping, several aar files can be found in the Dynamsoft\Libs directory:
- 📄 DynamsoftBarcodeReaderBundle.aar
- 📄 DynamsoftCaptureVisionBundle.aar
-
Copy the above .aar files to the target directory such as [App Project Root Path]\app\libs
-
Open the file
>- groovy >- kts > >1. ```groovy dependencies { implementation fileTree(dir: 'libs', include: ['*.aar']) def camerax_version = '1.4.2' implementation "androidx.camera:camera-core:$camerax_version" implementation "androidx.camera:camera-camera2:$camerax_version" implementation "androidx.camera:camera-lifecycle:$camerax_version" implementation "androidx.camera:camera-view:$camerax_version" } ``` 2. ```kotlin val camerax_version = "1.4.2" dependencies { implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.aar")))) implementation("androidx.camera:camera-core:$camerax_version") implementation("androidx.camera:camera-camera2:$camerax_version") implementation("androidx.camera:camera-lifecycle:$camerax_version") implementation("androidx.camera:camera-view:$camerax_version") } ``` > > The camera features require the camerax dependencies.[App Project Root Path]\app\build.gradleand add the reference in the dependencies: -
Click Sync Now. After the synchronization is complete, the SDK is added to the project.
In this section, we are going to explain how to create a Hello World implementation similar to our simple DecodeWithCameraEnhancer app for reading barcodes from camera video input.
-
Open Android Studio, select File > New > New Project.
-
Choose the correct template for your project. In this sample, we use Empty Views Activity.
-
When prompted, set your app name to 'DecodeWithCameraEnhancer' and set the Save location, Language, and Minimum SDK (we use 21 here).
> > - With **minSdkVersion** set to 21, your app is compatible with more than 99.6% of devices on the Google Play Store (last update: October 2023).
Add the SDK to your new project. Please read Add the Libraries section for more details.
-
Initialize the license in the file
>- Java >- Kotlin > >1. ```java import com.dynamsoft.license.LicenseManager; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", this, (isSuccess, error) -> { if (!isSuccess) { error.printStackTrace(); } }); } } } ``` 2. ```kotlin import com.dynamsoft.license.LicenseManager; class MainActivityKt : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main_kt) if (savedInstanceState == null) { LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", this) { isSuccess: Boolean, error: Exception -> if (!isSuccess) { error.printStackTrace() } } } } } ``` > >- The license string here grants a time-limited free trial which requires network connection to work. >- You can request a 30-day trial license via the [Request a Trial License](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=guide&package=android){:target="_blank"} link. >- If you download the Installation Package, it comes with a 30-day trial license by default.MainActivity.java.
-
In the Project window, open app > res > layout >
activity_main.xmland create a DCE camera view section under the root node.<com.dynamsoft.dce.CameraView android:id="@+id/camera_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
-
Import the dynamsoft camera module, initialize the camera view and bind to the created Camera Enhancer instance in the file
>- Java >- Kotlin > >1. ```java import com.dynamsoft.dce.CameraView; import com.dynamsoft.dce.CameraEnhancer; import com.dynamsoft.dce.utils.PermissionUtil; public class MainActivity extends AppCompatActivity { CameraEnhancer mCamera; @Override protected void onCreate(Bundle savedInstanceState) { ... // Add camera view for previewing video. PermissionUtil.requestCameraPermission(this); CameraView cameraView = findViewById(R.id.camera_view); mCamera = new CameraEnhancer(cameraView, this); } } ``` 2. ```kotlin import com.dynamsoft.dce.CameraView import com.dynamsoft.dce.CameraEnhancer import com.dynamsoft.dce.utils.PermissionUtil class MainActivityKt : AppCompatActivity() { private lateinit var mCamera: CameraEnhancer override fun onCreate(savedInstanceState: Bundle?) { ... PermissionUtil.requestCameraPermission(this) val cameraView: CameraView = findViewById(R.id.camera_view) mCamera = CameraEnhancer(cameraView, this) } } ```MainActivity.java.
-
Import and initialize the
>- Java >- Kotlin > >1. ```java import com.dynamsoft.cvr.CaptureVisionRouter; import com.dynamsoft.cvr.CaptureVisionRouterException; public class MainActivity extends AppCompatActivity { ... private CaptureVisionRouter mRouter; @Override protected void onCreate(Bundle savedInstanceState) { ... mRouter = new CaptureVisionRouter(this); try { mRouter.setInput(mCamera); } catch (CaptureVisionRouterException e) { throw new RuntimeException(e); } } } ``` 2. ```kotlin import com.dynamsoft.cvr.CaptureVisionRouter import com.dynamsoft.cvr.CaptureVisionRouterException class MainActivityKt : AppCompatActivity() { private lateinit var mRouter: CaptureVisionRouter override fun onCreate(savedInstanceState: Bundle?) { ... mRouter = CaptureVisionRouter(this) try { mRouter.setInput(mCamera) } catch (e: CaptureVisionRouterException) { throw RuntimeException(e) } } } ```CaptureVisionRouterand set the previously createdCameraEnhancerinstance as its input.
Important
Instances of CaptureVisionRouter are not thread-safe.
Do not access the same CaptureVisionRouter instance from multiple threads concurrently.
Create a separate instance for each thread if concurrent processing is required.
-
Create a
>- Java >- Kotlin > >1. ```java import com.dynamsoft.core.basic_structures.CapturedResultReceiver; import com.dynamsoft.dbr.DecodedBarcodesResult; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... mRouter.addResultReceiver(new CapturedResultReceiver() { @Override public void onDecodedBarcodesReceived(DecodedBarcodesResult result) { runOnUiThread(() -> showResult(result)); } }); } } ``` 2. ```kotlin import com.dynamsoft.core.basic_structures.CapturedResultReceiver import com.dynamsoft.dbr.DecodedBarcodesResult class MainActivityKt : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { ... mRouter.addResultReceiver(object : CapturedResultReceiver { override fun onDecodedBarcodesReceived(result: DecodedBarcodesResult) { runOnUiThread { showResult(result) } } }) } } ```CapturedResultReceiverand register with theCaptureVisionRouterinstance to get recognized barcode results. -
Override the
>- Java >- Kotlin > >1. ```java import com.dynamsoft.cvr.EnumPresetTemplate; import com.dynamsoft.dce.CameraEnhancerException; public class MainActivity extends AppCompatActivity { ... @Override public void onResume() { // Start video barcode reading mCamera.open(); mRouter.startCapturing(EnumPresetTemplate.PT_READ_BARCODES, new CompletionListener() { @Override public void onSuccess() {} @Override public void onFailure(int errorCode, String errorString) { runOnUiThread(() -> showDialog("Error", String.format(Locale.getDefault(), "ErrorCode: %d %nErrorMessage: %s", errorCode, errorString))); } }); super.onResume(); } @Override public void onPause() { // Stop video barcode reading mCamera.close(); mRouter.stopCapturing(); super.onPause(); } } ``` 2. ```kotlin import com.dynamsoft.cvr.EnumPresetTemplate import com.dynamsoft.dce.CameraEnhancerException public class MainActivity extends AppCompatActivity { ... public override fun onResume() { // Start video barcode reading mCamera.open() mRouter.startCapturing(EnumPresetTemplate.PT_READ_BARCODES, object : CompletionListener { override fun onSuccess() {} override fun onFailure(errorCode: Int, errorString: String?) = runOnUiThread { showDialog("Error", "ErrorCode: $errorCode ErrorMessage: $errorString") } }) super.onResume() } public override fun onPause() { // Stop video barcode reading mCamera.close() mRouter.stopCapturing() super.onPause() } } ```MainActivity.onResumeandMainActivity.onPausefunctions to start/stop video barcode scanning. After scanning starts, the sdk will automatically decode the video frames from the Camera Enhancer, then send the recognized barcode results to the callback.
Display the barcode result(s) in a dialog box.
>- Java >- Kotlin > >1. ```java ... import android.app.AlertDialog; import com.dynamsoft.dbr.BarcodeResultItem; import com.dynamsoft.dce.Feedback; public class MainActivity extends AppCompatActivity { private AlertDialog mAlertDialog; ... private void showResult(DecodedBarcodesResult result) { StringBuilder strRes = new StringBuilder(); if (result != null && result.getItems() != null && result.getItems().length > 0) { mRouter.stopCapturing(); for (int i = 0; i < result.getItems().length; i++) { BarcodeResultItem item = result.getItems()[i]; strRes.append(item.getFormatString()).append(":").append(item.getText()).append("\n\n"); } if (mAlertDialog != null && mAlertDialog.isShowing()) { return; } Feedback.vibrate(this); showDialog("Results:", strRes.toString()); } } private void showDialog(String title, String message) { if(mAlertDialog == null) { mAlertDialog = new AlertDialog.Builder(this).setCancelable(true).setPositiveButton("OK", null) .setOnDismissListener(dialog -> mRouter.startCapturing(EnumPresetTemplate.PT_READ_BARCODES, null)) .create(); } mAlertDialog.setTitle(title); mAlertDialog.setMessage(message); mAlertDialog.show(); } } ``` 2. ```kotlin ... import android.app.AlertDialog import com.dynamsoft.dbr.BarcodeResultItem import com.dynamsoft.dce.Feedback class MainActivityKt : AppCompatActivity() { private var mAlertDialog: AlertDialog? = null ... private fun showResult(result: DecodedBarcodesResult?) { val strRes = StringBuilder() if (result?.items != null && result.items.isNotEmpty()) { mRouter.stopCapturing() for (i in result.items.indices) { val item: BarcodeResultItem = result.items[i] strRes.append(item.formatString).append(":").append(item.text) .append("\n\n") } if (mAlertDialog != null && mAlertDialog!!.isShowing) { return } Feedback.vibrate(this) showDialog("Results:", strRes.toString()) } } private fun showDialog(title: String, message: String?) { if (mAlertDialog == null) { mAlertDialog = AlertDialog.Builder(this).setCancelable(true).setPositiveButton("OK", null) .setOnDismissListener { mRouter.startCapturing(EnumPresetTemplate.PT_READ_BARCODES, null) } .create() } mAlertDialog!!.setTitle(title) mAlertDialog!!.setMessage(message) mAlertDialog!!.show() } } ```Starting from v11.2.1000, Dynamsoft Barcode Reader integrates deep learning models to enhance decoding ability. Once initialized, these models remain cached in memory until they are explicitly released. If the decoding task has finished, call clearDLModelBuffers to free the associated memory.
-
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 the connected device and launches it.
You can also download the full source code of all the steps above:
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.
If you want to explore the many features of the SDK and learn how to use them to best process the images you read in your application, read the articles in Explore Features.
If you want to check how the SDK works in popular use cases, read the articles in Use Cases.
If you use the Android CameraX SDK, DecodeWithCameraX sample{:target="_blank"} will guide you on how to add barcode scanning to your app.
- Getting Started with iOS{:target="_blank"}
- Getting Started with MAUI{:target="_blank"}
- Getting Started with React Native{:target="_blank"}
- Getting Started with Flutter{:target="_blank"}
- [Getting Started with C++]({{ site.dbr_cpp }}){:target="_blank"}
- [Getting Started with Python]({{ site.dbr_python }}){:target="_blank"}
- [Getting Started with Java]({{ site.dbr_java }}){:target="_blank"}
- [Getting Started with .NET]({{ site.dbr_dotnet }}){:target="_blank"}
- [Getting Started with JS]({{ site.dbr_js }}){:target="_blank"}