| layout | default-layout |
|---|---|
| title | User Guide - Dynamsoft Barcode Reader for Android (Ready to Use UI edition) |
| description | This is the user guide of Dynamsoft Barcode Reader for Android SDK demonstrating the Ready to Use UI. |
| keywords | user guide, java, kotlin, android |
| needAutoGenerateSidebar | true |
| needGenerateH3Content | true |
| noTitleIndex | true |
This user guide will walk through the ScanSingleBarcode sample app. When creating your own project, please use this sample as a reference. This guide uses BarcodeScanner API which aim to elevate the UI creation process with less code and offer a more pleasant and intuitive UI for your app.
Note
This guide aims at scanning a single barcode with the BarcodeScanner component.
- If you have requirement for scanning multiple barcodes, you may refer to the ScanMultipleBarcodes sample or read Enable Multiple Barcode Scanning article.
- If you have more complex customization requirements for the interface, you may refer to the Foundational API Samples or [Build your APP with Foundational APIs]({{ site.android }}user-guide.html) article.
- 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 in which you can include the dynamsoftbarcodereaderbundle library in your app:
-
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") } } } ```[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.
The first thing that we are going to do is to create a fresh new project. Here are the steps on how to quickly do that
-
Open Android Studio and 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 ScanSingleBarcode 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 SDK section for more details.
Open your activity_main.xml and replace it with the following code. In the layout file, we prepared 2 UI elements:
- A "Start Scanning" button for opening the scanner view.
- A
TextViewfor displaying the barcode decoding result.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn_navigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Scanning" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text=""/>
</LinearLayout>The first step in code configuration is to include a valid license in the BarcodeScannerConfig object, which is used when launching the scanner.
We first start with the package imports and then start implementing the MainActivity class, which starts with some simple Android UI configuration and creating the TextView that will display the results, followed by defining the license via the setLicense method of BarcodeScannerConfig.
Note
- 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{:target="_blank"} link.
- If you download the Installation Package, it comes with a 30-day trial license by default.
Now that the Barcode Scanner is configured and the license has been set, it is time to implement the actions (via the launcher) to take when a barcode is scanned. Once the launcher is called, the Barcode Scanner opens the camera and begins the decoding process.
Each result comes with a resultStatus that can be one of RS_FINISHED, RS_CANCELED, or RS_EXCEPTION. The first, RS_FINISHED, indicates that the result has been decoded and is available - while RS_CANCELED indicates that the operation has been halted. If RS_EXCEPTION is the result status, then that means that an error has occurred during the barcode detection process.
Once a barcode is found, the content of the barcode (text result as well as barcode format) is outputted to the TextView object set up earlier. Continuing the code from step 3:
This next step, although optional, is highly recommended to help achieve a more smooth-looking and intuitive UI. In this setup we will configure the visibility of the torch button as well as the close button. In addition, a scan region will be defined that will limit the reading region of the Barcode Scanner to the specified dimensions. To do this, we are going back to the BarcodeScannerConfig object we used to define the license, and will make use of some of the other parameters available in the BarcodeScannerConfig class.
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.
Now that the code has been written and the project complete, it's time to run the project. During setup, all of the gradle settings should have already been configured for you, so pretty much all you need to do now is to connect a physical Android device, select the proper configuration, and click Run.
Now that your BarcodeScanner project is up and running you should be able to see a clean and simplified UI that contains all the necessary UI elements that are needed to make the barcode scanning process as easy and intuitive for the user as it can be.
For more configurations of the BarcodeScanner, please refer to the Configure Barcode Scanner section.
If you would like to work with the original framework and create your own customized UI, please refer to the Build Your APP with Foundational APIs.
If you have any questions in regards to the usage of the new specialized SDK, do not hesitate to get in touch with the Dynamsoft Support Team.