Thanks for downloading Dynamsoft Label Recognizer Package!
Your download will start shortly. If your download does not begin, click here to retry.
Dynamsoft Label Recognizer - Android User Guide
- Dynamsoft Label Recognizer - Android User Guide
Requirements
- Supported OS: Android 5.0 (API Level 21) or higher.
- Supported ABI: armeabi-v7a, arm64-v8a, x86 and x86_64.
- Development Environment: Android Studio 3.4+ (Android Studio 4.2+ recommended).
Add the Libraries
The Dynamsoft Label Recognizer (DLR) Android SDK comes with seven libraries:
File | Description | Mandatory/Optional |
---|---|---|
DynamsoftLabelRecognizer.aar |
The Dynamsoft Label Recognizer module identifies and recognizes text labels such as passport MRZs, ID cards, and VIN numbers. | Mandatory |
DynamsoftCore.aar |
The Dynamsoft Core module lays the foundation for Dynamsoft SDKs based on the DCV (Dynamsoft Capture Vision) architecture. It encapsulates the basic classes, interfaces, and enumerations shared by these SDKs. | Mandatory |
DynamsoftCaptureVisionRouter.aar |
The Dynamsoft Capture Vision Router module is the cornerstone of the Dynamsoft Capture Vision (DCV) architecture. It focuses on coordinating batch image processing and provides APIs for setting up image sources and result receivers, configuring workflows with parameters, and controlling processes. | Mandatory |
DynamsoftImageProcessing.aar |
The Dynamsoft Image Processing module facilitates digital image processing and supports operations for other modules, including the Barcode Reader, Label Recognizer, and Document Normalizer. | Mandatory |
DynamsoftNeuralNetwork.aar |
The Dynamsoft Neural Network module allows SDKs compliant with the DCV (Dynamsoft Capture Vision) architecture to leverage the power of deep learning when processing digital images. | Mandatory |
DynamsoftLicense.aar |
The Dynamsoft License module manages the licensing aspects of Dynamsoft SDKs based on the DCV (Dynamsoft Capture Vision) architecture. | Mandatory |
DynamsoftCameraEnhancer.aar |
The Dynamsoft Camera Enhancer module controls the camera, transforming it into an image source for the DCV (Dynamsoft Capture Vision) architecture through ISA implementation. It also enhances image quality during acquisition and provides basic viewers for user interaction. | Optional |
DynamsoftUtility.aar |
The Dynamsoft Utility module defines auxiliary classes, including the ImageManager, and implementations of the CRF (Captured Result Filter) and ISA (Image Source Adapter) . These are shared by all Dynamsoft SDKs based on the DCV (Dynamsoft Capture Vision) architecture. | Optional |
There are two ways to add the libraries into your project - Manually or via Maven.
Add the Libraries Manually
-
Download the SDK package from the Dynamsoft Website. After unzipping, seven aar files can be found in the Dynamsoft\Libs directory:
- DynamsoftCaptureVisionRouter.aar
- DynamsoftLabelRecognizer.aar
- DynamsoftCore.aar
- DynamsoftImageProcessing.aar
- DynamsoftNeuralNetwork.aar
- DynamsoftLicense.aar
- DynamsoftUtility.aar
- DynamsoftCameraEnhancer.aar
-
Copy the above seven aar files to the target directory such as [App Project Root Path]\app\libs
-
Open the file [App Project Root Path]\app\build.gradle and add the reference in the dependencies:
dependencies { implementation fileTree(dir: 'libs', include: ['*.aar']) def camerax_version = '1.1.0' 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" }
Note:
DCE 4.x is based on Android CameraX, so you need to add the CameraX dependency manually.
-
Click Sync Now. After the synchronization is complete, the SDK is added to the project.
Add the Libraries via Maven
-
Open the file [App Project Root Path]\app\build.gradle and add the Maven repository:
repositories { maven { url "https://download2.dynamsoft.com/maven/aar" } }
-
Add the references in the dependencies:
dependencies { implementation 'com.dynamsoft:dynamsoftlabelrecognizerbundle:3.2.3000' }
-
Click Sync Now. After the synchronization is complete, the SDK is added to the project.
Build Your First Application
In this section, we are going to explain how to create a Hello World implementation similar to our simple ReadTextLinesWithCameraEnhancer
app for recognizing text from camera video input.
Note:
- Android Studio 2022.3.1 is used here in this guide.
- You can get similar source code from
Create a New Project
-
Open Android Studio and select New Project… in the File > New > New Project… menu to create a new project.
-
Choose the correct template for your project. In this sample, we’ll use
Empty Activity
. -
When prompted, choose your app name (
ReadTextLinesWithCameraEnhancer
) and set the Save location, Language, and Minimum SDK (21)Note: With minSdkVersion set to 21, your app is available on more than 94.1% of devices on the Google Play Store (last update: March 2021).
Include the Library
To add the SDK to your new project, please read add the libraries section for more details.
Deploy the CharacterModels
A CharacterModel
is a file that trained to support the text line recognition. Before implementing the label recognizing tasks, you have to include the required CharacterModels
in your project first.
-
Right-click on the app folder, click New->Directory and select the src\main\assets to create an assets folder.
-
Under the assets folder, create a folder named CharacterModel
-
Copy your
CharacterModel
file(s) to the assets folder. In this guide we put the NumberLetter.data to theCharacterModel
folder.
Initialize the License
-
Initialize the license in the file
MainActivity.java
.- Java
- Kotlin
-
import com.dynamsoft.license.LicenseManager; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", this, (isSuccess, error) -> { if (!isSuccess) { error.printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Toast ts = Toast.makeText(getBaseContext(), "error: " + error.getMessage(), Toast.LENGTH_LONG); ts.show(); } }); } }); } }
-
import com.dynamsoft.license.LicenseManager; class MainActivityKt : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main_kt) LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", this) { isSuccess, error -> if (!isSuccess) { error.printStackTrace() runOnUiThread { val ts = Toast.makeText( baseContext, "error: " + error.message, Toast.LENGTH_LONG ) ts.show() } } } } }
Note:
- Network connection is required for the license to work.
- The license string here will grant you a 24 hour trial license.
- You can request a 30-day trial license via the Request a Trial License link. Offline trial license is also available by contacting us.
Initialize the Camera Module
-
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.CameraView android:id="@+id/dce_camera_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
-
Import the dynamsoft camera module, initialize the
CameraView
and bind to the createdCameraEnhancer
instance in the fileMainActivity.java
.- Java
- Kotlin
-
import com.dynamsoft.dce.CameraView; import com.dynamsoft.dce.CameraEnhancer; import com.dynamsoft.dce.utils.PermissionUtil; public class MainActivity extends AppCompatActivity { private CameraEnhancer mCamera; @Override protected void onCreate(Bundle savedInstanceState) { ... // Add camera view for previewing video. PermissionUtil.requestCameraPermission(this); CameraView cameraView = findViewById(R.id.dce_camera_view); mCamera = new CameraEnhancer(cameraView, this); } }
-
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.dce_camera_view) mCamera = CameraEnhancer(cameraView, this) } }
-
Define a scan region for recognition.
- Java
- Kotlin
-
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... DSRect region = new DSRect(0.1f, 0.4f, 0.9f, 0.6f, true); try { mCamera.setScanRegion(region); } catch (CameraEnhancerException e) { e.printStackTrace(); } } }
-
class MainActivityKt : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { ... val region = DSRect(0.1f, 0.4f, 0.9f, 0.6f, true) try { mCamera.scanRegion = region } catch (e: CameraEnhancerException) { e.printStackTrace() } } }
Initialize Capture Vision Router
-
Import and initialize the
CaptureVisionRouter
and set the previously createdCameraEnhancer
instance as its input.- Java
- Kotlin
-
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); } } }
-
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) } } }
-
Create a
CapturedResultReceiver
and register with theCaptureVisionRouter
instance to get recognized textline results.- Java
- Kotlin
-
import com.dynamsoft.core.basic_structures.CapturedResultReceiver; import com.dynamsoft.dlr.RecognizedTextLinesResult; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... mRouter.addResultReceiver(new CapturedResultReceiver() { @Override public void onRecognizedTextLinesReceived(RecognizedTextLinesResult result) { runOnUiThread(() -> showResults(result.getItems())); } }); } }
-
import com.dynamsoft.core.basic_structures.CapturedResultReceiver import com.dynamsoft.dlr.RecognizedTextLinesResult class MainActivityKt : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { ... mRouter.addResultReceiver(object : CapturedResultReceiver { override fun onRecognizedTextLinesReceived(result: RecognizedTextLinesResult) { showResults(result.items) } }) } }
-
Override the
MainActivity.onResume
andMainActivity.onPause
functions to start and stop video text recognition, respectively. After starting recognition, the library will automatically recognize text in video frames from the Camera Enhancer, then send the recognized text line results to the callback.- Java
- Kotlin
-
import com.dynamsoft.cvr.EnumPresetTemplate; import com.dynamsoft.dce.CameraEnhancerException; public class MainActivity extends AppCompatActivity { ... @Override public void onResume() { try { mCamera.open(); } catch (CameraEnhancerException e) { e.printStackTrace(); } mRouter.startCapturing(EnumPresetTemplate.PT_RECOGNIZE_TEXT_LINES, new CompletionListener() { @Override public void onSuccess() { } @Override public void onFailure(int errorCode, String errorString) { runOnUiThread(() -> Toast.makeText(MainActivity.this, errorString, Toast.LENGTH_SHORT).show()); } }); super.onResume(); } @Override public void onPause() { try { mCamera.close(); } catch (CameraEnhancerException e) { e.printStackTrace(); } mRouter.stopCapturing(); super.onPause(); } }
-
import com.dynamsoft.cvr.EnumPresetTemplate import com.dynamsoft.dce.CameraEnhancerException public class MainActivity extends AppCompatActivity { ... public override fun onResume() { try { mCamera.open() } catch (e: CameraEnhancerException) { e.printStackTrace() } mRouter.startCapturing( EnumPresetTemplate.PT_RECOGNIZE_TEXT_LINES, object : CompletionListener { override fun onSuccess() {} override fun onFailure(errorCode: Int, errorString: String) { runOnUiThread { Toast.makeText( this@MainActivity, errorString, Toast.LENGTH_SHORT ).show() } } }) super.onResume() } public override fun onPause() { try { mCamera.close() } catch (e: CameraEnhancerException) { e.printStackTrace() } mRouter.stopCapturing() super.onPause() } }
Display Recognized Textline Results
-
In the Project window, open app > res > layout >
activity_main.xml
, create a text view control under the root node.... <TextView android:id="@+id/tv_res" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:padding="15dp" android:lineSpacingMultiplier="1.0" android:gravity="center" android:textColor="@color/white" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" />
-
Display the text result(s) in the text view.
- Java
- Kotlin
-
... public class MainActivity extends AppCompatActivity { ... private TextView tvRes; @Override protected void onCreate(Bundle savedInstanceState) { ... tvRes = findViewById(R.id.tv_res); } private void showResults(TextLineResultItem[] results) { StringBuilder resultBuilder = new StringBuilder(); if (results != null) { for (TextLineResultItem result : results) { resultBuilder.append(result.getText()).append("\n\n"); } } runOnUiThread(() -> tvRes.setText(resultBuilder.toString())); } }
-
... class MainActivityKt : AppCompatActivity() { ... private lateinit var tvRes: TextView override fun onCreate(savedInstanceState: Bundle?) { ... tvRes = findViewById(R.id.tv_res) } private fun showResults(results: Array<TextLineResultItem>?) { val resultBuilder = StringBuilder() if (results != null) { for (result in results) { resultBuilder.append(result.text).append("\n\n") } } runOnUiThread { tvRes.text = resultBuilder.toString() } } }
Build and Run the Project
-
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: