This tutorial shows how to create an MRTD extraction and processing application in Android using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS MRTD SDK technology in a an Android Java application. |
Completion Time | 30 minutes |
Android Studio Project | Download tutorial project (104 KB) |
Platform | Android (Java) |
IDE | Android Studio |
Runtime License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project and working with the OcrEngine by reviewing the Add References and Set a License and Recognize-Text-from-Images with OCR tutorials, before working on the Detect and Extract MRTD - Android Java tutorial.
Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If you do not have that project, follow the steps in that tutorial to create it.
Ensure that you add the needed Artifacts to the project.
For a complete list of which LEADTOOLS libraries are required for your application, refer to Files to be Included With Your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
In the Project Explorer window, open the activity_main.xml
file found in the app/src/main/res/layout
directory. Below the RasterImageViewer XML code add a new RUN OCR
button.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0"
android:background="@android:color/black">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Image From Gallery"
android:onClick="onSelectImage"/>
<leadtools.controls.RasterImageViewer
android:id="@+id/rasterimageviewer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".96"
android:background="@android:color/white"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read MRTD"
android:onClick="readMRTD"/>
</LinearLayout>
In MainActivity.java
, add the following import statements before the MainActivity
class.
import leadtools.forms.commands.MRTDErrors;
import leadtools.forms.commands.MRTDReader;
Add the following member variables to the MainActivity
class.
private MRTDReader mrtdReader;
Update the onCreate()
function as shown below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mrtdReader = new MRTDReader();
}
Create a readMRTD(View v)
function to detect if an MRTD or MRZ is present. If one is present parse the value.
public void readMRTD(View v )
{
RasterImage image = mViewer.getImage();
if(image != null)
{
try
{
mrtdReader.setOcrEngine(ocrEngine);
mrtdReader.processImage(image);
{
if (mrtdReader.getErrors() == MRTDErrors.NO_ERROR.getValue())
for (var val : mrtdReader.getResults().entrySet())
{
sb.append(val.getKey().toString());
sb.append(val.getValue().getReadableValue());
sb.append(val.getValue().getMrzCharacters());
sb.append((val.getValue().isValid()));
sb.append("\n\n");
}
if(sb.length() != 0)
{
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "No detection found: " + mrtdReader.getErrors(), Toast.LENGTH_LONG).show();
}
}
}
catch(Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(this, "No File Is Loaded", Toast.LENGTH_LONG).show();
}
}
Press Shift + F10 to run the application. Follow the steps below to test the application.
SELECT IMAGE FROM GALLERY
button to load in an image.Select the READ MRTD CODE
button to read the image and output the results.
This tutorial showed how to use the MRTDReader
class to read MRZ and MRTD data.