ANDROID JMRTD EXAMPLE
Android JMRTD Example: How to Use the JMRTD Library for Passport Reading
The JMRTD library (Java Machine Readable Travel Document) is a Java-based library that provides a way to decode and parse machine-readable travel documents (MRTDs), such as passports and identity cards. This can be particularly useful for Android applications in scenarios like border control, document verification, or automated passport reading.
In this tutorial, we’ll explore how to integrate the JMRTD library into an Android app and demonstrate how to read and extract information from a passport’s Machine Readable Zone (MRZ).
What is JMRTD?
JMRTD is an open-source library that enables developers to decode and parse MRZ (Machine Readable Zone) data in passports, ID cards, and other travel documents. The MRZ typically contains vital information such as:
- Passport number
- Name of the passport holder
- Date of birth
- Expiration date
This information is encoded in a specific format that can be read by scanners and parsed by applications using the JMRTD library.
Requirements
Before we start coding, ensure you have the following:
- Android Studio installed on your computer.
- Basic knowledge of Java/Kotlin for Android app development.
- JMRTD library integrated into your Android project.
Step 1: Set Up the Android Project
-
Create a new Android project in Android Studio:
- Open Android Studio and select File > New > New Project.
- Choose a Basic Activity template.
- Set up your project (e.g., project name, language, etc.).
-
Add Dependencies: To use the JMRTD library in your Android project, you need to include it as a dependency. You can do this by adding it to your
build.gradlefile:
dependencies {
implementation 'org.jmrtd:jmrtd:1.2'
// Add other dependencies here
}
Then, sync the project with Gradle.
Note: If you can't find the library directly on Maven, you may need to download it manually or include it as a local JAR file.
Step 2: Add Permissions for Camera Access
To read the MRZ from passports, we typically use a camera to scan the image. Therefore, you'll need to request camera permissions in your Android app.
Open AndroidManifest.xml and 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" />
Also, make sure to request runtime permissions if you are targeting Android 6.0 (API 23) or higher:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
}
Step 3: Set Up the User Interface (UI)
You can create a simple layout with a Button to trigger the passport scan and a TextView to display the decoded MRZ information.
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>
In this layout:
- The
Buttontriggers the passport scanning process. - The
TextViewwill display the decoded MRZ data.
Step 4: Integrating the JMRTD Library for MRZ Decoding
Now that the basic setup is complete, let's implement the logic to read the MRZ from a passport image and decode it using the JMRTD library.
MainActivity.java:
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 app, you would integrate a camera scanner here.
// For this example, we are using a static MRZ string.
String mrzString = "P<UTOABRAHAM<<GENE<JOHN<<<<<<<<<<<<<<<<<\n1234567890UTO1234567UTO1234567UTO<1234";
decodeMRZ(mrzString);
}
});
}
// Method to decode the 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();
}
}
}
Step 5: Test the App
- When the user clicks on the Scan Passport button, it triggers the decoding process.
- In this example, instead of scanning a passport, we're using a hardcoded MRZ string for demonstration purposes. In a real app, you would integrate a camera scan to capture the MRZ from a passport image.
- The
decodeMRZ()method uses the JMRTD library's MRZParser to parse the MRZ string and extract the relevant passport information such as document type, name, passport number, nationality, birth date, and expiration date. - The decoded information is displayed in the
TextView.
Step 6: (Optional) Integrate Camera Scanning for MRZ Capture
To actually scan passports, you would need to integrate a camera scanning library such as ZXing or Google's ML Kit for Optical Character Recognition (OCR) to extract the MRZ from the passport image.
For instance, you can use the CameraX library to access the device's camera, capture the image, and then use an OCR library to extract the MRZ data.
Conclusion
In this tutorial, we demonstrated how to integrate the JMRTD library into an Android app to decode MRZ data from travel documents like passports. You learned how to:
- Set up the Android project and add the necessary dependencies.
- Create an interface for scanning and displaying MRZ information.
- Use the JMRTD library to decode MRZ data.
In a real-world app, you can combine this with a camera scanning tool to scan passports in real time. This setup can be useful in applications related to immigration, border control, or any situation where you need to automate passport verification and reading.

0 Comments