JMRTD ANDROID
JMRTD for Android: How to Read and Parse Machine Readable Travel Documents (MRTDs)
In the world of border control, security, and identity verification, reading and verifying travel documents, such as passports, is an important task. The JMRTD (Java Machine Readable Travel Document) library is a popular open-source library used to decode and parse machine-readable zones (MRZ) in travel documents like passports, visas, and national ID cards.
In this article, we will guide you through integrating the JMRTD library into your Android app. We'll cover how to read the MRZ (Machine Readable Zone) from documents like passports and parse the information for various uses, such as identity verification, automated document reading, and security applications.
What is JMRTD?
JMRTD is a Java-based library that helps decode and parse the Machine Readable Zone (MRZ) of travel documents, like passports and identity cards. The MRZ is a section of the document that contains critical data such as the document holder's personal information, passport number, nationality, and other vital details.
The JMRTD library allows developers to read the MRZ from an image, parse it, and use the extracted information. The library is designed to be compatible with ICAO-compliant (International Civil Aviation Organization) passports and travel documents.
Why Use JMRTD for Android?
Using JMRTD on Android is highly beneficial in scenarios where you need to:
- Automate passport reading for border control or identity verification.
- Extract traveler details for check-in processes at airports or security checkpoints.
- Create secure apps that require passport validation for events or travel.
- Develop government or private sector applications for document verification.
JMRTD helps simplify this process by providing a reliable way to decode MRZ information without building complex parsing logic from scratch.
Setting Up Your Android Project with JMRTD
To integrate the JMRTD library into your Android project, follow these steps:
Step 1: Create a New Android Project
- Open Android Studio and create a new Android project with a Basic Activity template.
- Choose Java as the programming language and set the project configuration (name, package, etc.).
Step 2: Add the JMRTD Dependency
The JMRTD library is a Java library, and to include it in your Android project, you need to add the dependency to your build.gradle file.
Currently, JMRTD is not available directly through Maven, so you might need to download the JAR file manually or include it via a local repository.
- Download the latest release of JMRTD from its GitHub repository or build it from source: JMRTD GitHub Repository.
- If you downloaded the JAR file, include it in your project by placing the
.jarfile in thelibsdirectory of your project.
Then, modify the build.gradle file to include the JAR file:
dependencies {
implementation files('libs/jmrt.jar') // Replace with your actual path to the JAR file
}
Afterward, sync the project with Gradle.
Step 3: Add Permissions for Camera Access
Since JMRTD works by scanning the MRZ from travel documents (typically using the camera), you need to add camera permissions to your Android app.
In your AndroidManifest.xml, add the following permissions:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
For Android versions 6.0 and above (API level 23+), you must also request runtime permissions for the camera. You can do this in your activity:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
}
Step 4: Setting Up the UI
The user interface should allow users to scan a passport or document and display the extracted MRZ information. The layout might consist of a button to initiate scanning and a TextView to show the parsed details.
Here's an example of the layout file (activity_main.xml):
<?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:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/scanButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan Passport" />
<TextView
android:id="@+id/mrzInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MRZ Info will appear here"
android:textSize="18sp"
android:paddingTop="20dp" />
</LinearLayout>
This layout consists of a Button to start the scan process and a TextView to display the decoded MRZ information.
Step 5: Implementing MRZ Scanning and Parsing
In the MainActivity.java file, you will need to handle scanning the passport (or MRZ document) and parsing the MRZ data using the JMRTD library. For demonstration purposes, we will assume that you will manually input an MRZ string to simulate scanning, but in a real app, you would integrate a camera scanner to capture the MRZ.
Here’s a sample implementation:
package com.example.jmrtddemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import org.jmrtd.PassportParser;
import org.jmrtd.icao.MRZ;
import org.jmrtd.icao.MRZParser;
import org.jmrtd.icao.MRZResult;
public class MainActivity extends AppCompatActivity {
private TextView mrzInfoTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mrzInfoTextView = findViewById(R.id.mrzInfo);
Button scanButton = findViewById(R.id.scanButton);
scanButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// In a real application, you would integrate camera scanning here.
// For now, we will use a hardcoded MRZ string.
String mrzString = "P<UTOABRAHAM<<GENE<JOHN<<<<<<<<<<<<<<<<<\n1234567890UTO1234567UTO1234567UTO<1234";
decodeMRZ(mrzString);
}
});
}
// Method to decode MRZ
private void decodeMRZ(String mrzString) {
try {
MRZParser parser = new MRZParser();
MRZResult result = parser.parse(mrzString);
if (result.isValid()) {
MRZ mrz = result.getMRZ();
String decodedInfo = "Document Type: " + mrz.getDocumentType() + "\n" +
"Name: " + mrz.getPrimaryId() + " " + mrz.getSecondaryId() + "\n" +
"Passport Number: " + mrz.getDocumentNumber() + "\n" +
"Nationality: " + mrz.getNationality() + "\n" +
"Date of Birth: " + mrz.getDateOfBirth() + "\n" +
"Expiration Date: " + mrz.getExpirationDate();
mrzInfoTextView.setText(decodedInfo);
} else {
mrzInfoTextView.setText("Invalid MRZ data.");
}
} catch (Exception e) {
Toast.makeText(this, "Error decoding MRZ", Toast.LENGTH_SHORT).show();
}
}
}
Explanation:
-
Camera and MRZ Parsing:
- In this example, a hardcoded MRZ string simulates a passport scan.
- We use the MRZParser from the JMRTD library to decode the MRZ data and extract key details such as the passport number, name, date of birth, and expiration date.
- The result is displayed in a TextView on the screen.
-
Error Handling:
- If the MRZ is invalid or cannot be parsed, we handle the error gracefully and notify the user.
Step 6: Testing the Application
To test the app:
- Run the app on an emulator or physical device.
- Click the "Scan Passport" button (which currently simulates an MRZ scan with a hardcoded string).
- The app will parse the MRZ data and display the decoded passport information in the TextView.
Conclusion
The JMRTD library offers an effective way to parse and decode MRZ data from travel documents like passports and identity cards. By integrating it into an Android app, you can automate document verification processes, make passport reading more efficient, and provide a higher level of security in your applications.
In this tutorial, we showed you how to:
- Set up an Android project and integrate JMRTD for MRZ parsing.
- Design a simple UI for scanning and displaying MRZ information.
- Handle MRZ parsing, including error handling and displaying results.
This setup can be further extended with features like live camera scanning (using libraries like CameraX or ZXing), database storage for frequent MRZ data, or a more complex document verification process.

0 Comments