Dynamsoft Code Parser - 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 2022.2.1 or higher.
Add the Libraries
The Dynamsoft Code Parser (DCP) Android SDK comes with four libraries:
File | Description |
---|---|
DynamsoftCodeParser.aar |
The Dynamsoft Code Parser library, which includes code parsing logic and related APIs. |
DynamsoftCore.aar |
The core library, which includes common basic structures and intermediate result related APIs. |
DynamsoftLicense.aar |
The license library, which includes license related APIs. |
DynamsoftCodeParserDedicator.aar |
The code parser helper library, which includes some validation functions used by the SDK. |
-
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:dynamsoftcodeparser:2.2.10' implementation 'com.dynamsoft:dynamsoftcodeparserdedicator:1.2.20' implementation 'com.dynamsoft:dynamsoftcore:3.2.30' implementation 'com.dynamsoft:dynamsoftlicense:3.2.20' }
-
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 simple HelloWorld
app for parsing text or bytes.
Note:
- Android Studio 2022.3.1 is used here in this guide.
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 (
HelloWorld
) 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
Add the SDK to your new project. Please read Add the Libraries section for more details.
Initialize the License
-
Initialize the license in the file
MainActivity.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); LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9", this, (isSuccess, error) -> { if (!isSuccess) { error.printStackTrace(); } }); } }
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 link. Offline trial license is also available by contacting us.
- If you download the Installation Package, it comes with a 30-day trial license by default.
Initialize Code Parser And Parse the Code Bytes
-
Import and initialize the
CodeParser
.import com.dynamsoft.dcp.CodeParser; public class MainActivity extends AppCompatActivity { private CodeParser mParser; @Override protected void onCreate(Bundle savedInstanceState) { ... mParser = new CodeParser(this); } }
-
In the Project window, open app > res > layout >
activity_main.xml
, create a “Parse” button control under the root node.<Button android:id="@+id/btn_parse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="50dp" android:text="Parse" android:textAllCaps="false" android:textSize="20sp" android:backgroundTint="@android:color/holo_blue_dark" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />
-
Parse the code bytes when the user clicks the “Parse” button. Here we use the passport MRZ string as an example.
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { ... final String passportMRZStr = "P<D<<ADENAUER<<KONRAD<HERMANN<JOSEPH<<<<<<<<1234567897D<<7601059M6704115<<<<<<<<<<<<<<<2"; Button ParseButton = findViewById(R.id.btn_parse); ParseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(() -> parseCode(passportMRZStr.getBytes())).start(); } }); } private void parseCode(byte[] codeBytes) { try { ParsedResultItem parsedResultItem = mParser.parse(codeBytes, ""); showResults(parsedResultItem); } catch (CodeParserException e) { e.printStackTrace(); } } }
Note:
- Check out the complete supported code types and fields for details.
Display Parsed Results
-
Display the parsed result(s) in an alert dialog box.
public class MainActivity extends AppCompatActivity { ... private void showResults(ParsedResultItem item) { StringBuilder result = new StringBuilder(); for (HashMap.Entry<String, String> entry : item.getParsedFields().entrySet()) { String key = entry.getKey(); String value = entry.getValue(); result.append(key).append(":").append(value).append("\n"); } runOnUiThread(() -> { new AlertDialog.Builder(this) .setTitle("Result") .setMessage(result.toString()) .setPositiveButton("OK", null) .setCancelable(true) .show(); }); } }
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.