Posted on: Friday, 19 July 2019
This is a sample application for using MRZ Scanner, the main purpose is to serve as a guide for implementing and using the library. It contains examples of the scanner practical integration.
To skip this guide, go to the link below to be redirected to the downloading page.
https://mrzscanner.com/download
https://github.com/Realmrzscanner/MRZScanner-SampleApp-Android
public class ScannerActivity extends AppCompatActivity implements MRZScannerListener
repositories {
flatDir {
dirs 'libs'
}
}
.
.
.
dependencies {
implementation(name: 'mrzscannerlib', ext: 'aar')
}
Returns the scanner results for ScannerType.SCANNER_TYPE_MRZ, you can further on pick in whatever format you want them to be when assigning to a variable.
Returns the scanner results for ScannerType.SCANNER_TYPE_DOC_IMAGE_ID & SCANNER_TYPE_DOC_IMAGE_PASSPORT, returns a bitmap.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
mrzScanner.setMaxThreads(2);
mrzScanner = (MRZScanner) getSupportFragmentManager().findFragmentById(R.id.scannerFragment);
getScannerTypeKey();
}
2. Make sure to define the Scanner fragment in your Activity.
@Override
public void successfulScanWithResult(MRZResultModel mrzResultModel) {
successfulMrzScan = new JSONObject();
try {
successfulMrzScan.put("document_type_raw", mrzResultModel.document_type_raw);
successfulMrzScan.put("document_type_readable", mrzResultModel.document_type_readable);
successfulMrzScan.put("issuing_country", mrzResultModel.issuing_country);
successfulMrzScan.put("surname", mrzResultModel.surnamesReadable());
successfulMrzScan.put("document_number", mrzResultModel.document_number);
successfulMrzScan.put("nationality", mrzResultModel.nationality);
successfulMrzScan.put("dob_raw", mrzResultModel.dob_raw);
successfulMrzScan.put("dob_readable", mrzResultModel.dob_readable);
successfulMrzScan.put("sex", mrzResultModel.sex);
successfulMrzScan.put("est_issuing_date_raw", mrzResultModel.est_issuing_date_raw);
successfulMrzScan.put("est_issuing_date_readable", mrzResultModel.est_issuing_date_readable);
successfulMrzScan.put("expiration_date_raw", mrzResultModel.expiration_date_raw);
successfulMrzScan.put("expiration_date_readable", mrzResultModel.expiration_date_readable);
successfulMrzScan.put("given_names_readable", mrzResultModel.givenNamesReadable());
successfulMrzScan.put("optionals", mrzResultModel.optionalsReadable());
sendResultMrz(successfulMrzScan);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void successfulScanWithDocumentImage(Bitmap bitmap)
{
}
@Override
public void scanImageFailed() {
}
@Override
public void permissionsWereDenied() {
}
3. We are using an intent to launch the barcode scanner from an initial class CheckInActivity.class, the intent will use onActivityResult method so that scan results can be returned.
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
String requiredValue = data.getStringExtra("key");
JSONObject jsonObj = new JSONObject(requiredValue);
addResultToEditText(jsonObj);
}
} catch (Exception ex) {
Toast.makeText(this, "Activity Result Failed",
Toast.LENGTH_SHORT).show();
}
}
4. I am adding a button to the layout that will start the scan and several text view to display the scan data (as shown above).
private void startScanner() {
startActivityForResult(new Intent(getApplicationContext(),ScannerActivity.class),REQUEST_CODE);
}
<fragment
android:id="@+id/scannerFragment"
android:name="com.scansolutions.mrzscannerlib.MRZScanner"
android:layout_width="match_parent"
android:layout_height="match_parent" />