1.Add the following line to your app's build.gradle:
implementation 'com.scansolutions.mrzscanner:mrzscannerlib:1.9.2'
1.Copy mrzscannerlib.aar in the libs/ folder
2.Add the following dependencies in your app’s build.gradle file:
3.Add this code in the root of you app’s build.gradle file to link the libs/ dir:
repositories {
flatDir{
dirs 'libs'
}
}
1. Add the following code to the layout.xml of the activity where you want the scanner to be shown:
(Partial scanning view is also possible by positioning and resizing the scanner fragment)
<fragment
android:id="@+id/scannerFragment"
android:name="com.scansolutions.mrzscannerlib.MRZScanner"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2. Use this code to configure the scanner. The code should be added in the activity where the scanning will take place.
MRZScanner mrzScanner = (MRZScanner) getSupportFragmentManager().findFragmentById(R.id.scannerFragment);
mrzScanner.setScannerType(ScannerType.SCANNER_TYPE_MRZ); // Options: [SCANNER_TYPE_MRZ, SCANNER_TYPE_DOC_IMAGE_ID, SCANNER_TYPE_DOC_IMAGE_PASSPORT]
mrzScanner.setIDActive(true); // Enable/disable the ID document type
mrzScanner.setPassportActive(true); // Enable/disable the Passport document type
mrzScanner.setVisaActive(true); // Enable/disable the Visa document type
mrzScanner.setMaxThreads(2); // Set the max CPU threads that the scanner can use
mrzScanner.registerWithLicenseKey("licenseKey");
String sdkVersion = MRZScanner.sdkVersion(); // Get the current SDK version of MRZScanner
3. Implement the MRZScannerListener interface as a listener for a successful MRZ scan.
Java:
public class MainActivity extends AppCompatActivity implements MRZScannerListener { ...
Kotlin:
class MainActivity : AppCompatActivity(), MRZScannerListener { ...
Set the scanning rectangle to limit the scanning area.The parameters' values are representing percentages of the scanning preview.
@param x the top-left point of the scanning rectangle. [0,...,100]Default value: 5.
@param y the top-left point of the scanning rectangle. [0,...,100]Default value: 20.
@param width the width of the scanning rectangle. [0,...,100]Default value: 90.
@param height the height of the scanning rectangle. [0,...,100]Default value: 60.
public void setScanningRectangle(int x, int y, int width, int height)
Specify whether the scanner should detect and return result for IDs.
@param isIDActive [true, false]. The default value is true.
public void setIDActive(boolean isIDActive)
Stop and close the MRZ Scanner
public void closeScanner()
Specify whether the scanner should detect and return result for passports.
@param isPassportActive [true, false]. The default value is true.
public void setPassportActive(boolean isPassportActive)
Specify whether the scanner should detect and return result for visas.
@param isVisaActive [true, false]. Default value is true.
public void setVisaActive(boolean isVisaActive)
Specify the maximum number of CPU threads that the scanner can use during the scanning process.
@param maxThreads number of CPU threads. Default value is 2.
public void setMaxThreads(int maxThreads)
Set the zoom factor for the camera preview.
@param float. Default value is 1.0
public static void setZoomFactor(float zoomFactor)
Specify which scanner type you want to use. There are two options: "MRZ Scanner" and "Document Image scanner".
The "MRZ Scanner" option is used to scan for MRZ.
The "Document image scanner" is used for capturing front and back image of the ID documents.
@param scannerType [SCANNER_TYPE_MRZ, SCANNER_TYPE_DOC_IMAGE_ID, SCANNER_TYPE_DOC_IMAGE_PASSPORT]. Default value is SCANNER_TYPE_MRZ
public void setScannerType(ScannerType scannerType)
Resume scanning after the scanner has been paused/stopped. Usually after a successful scan.
public void resumeScanning()
Register with the licence key provided to remove the asterisks (*) from the result.
@param key the provided licence key.
@return 0 for success, -1 if registration failed.
public int registerWithLicenseKey(String key)
To get your evaluation key today, register an account on https://mrzscanner.com/register
public static String sdkVersion()
Set the date format in which the parsed dates are formatted.
@param dateFormat the pattern describing the date format. Example: "dd.MM.YYYY"
@return the currently set date format in which the parsed dates are formatted.
public static void setDateFormat(String dateFormat)
Scan for a single bitmap.
@param bitmap the image to the scanned.
@param activity the initialization activity context.
public static void scanBitmap(final Bitmap bitmap, final Activity activity, final MRZScannerListener listener)
Trigger image picker.
@param activity the initialization activity context.
public static void scanFromGallery(final Activity activity, final MRZScannerListener listener)
Show a warning alert dialog if the camera's resolution is lower than 5 MP and if the camera lacks the AutoFocus feature.
@param shouldWarn [true, false]. Default value is true.
public static void warnIncompatibleCamera(boolean shouldWarn)
Set valid issuing country codes. Successful scans with country codes not included in the array will be ignored.
@param validIssuingCountries array with active country codes. Example value: new String[]{"foo", "bar"}.
public static void setValidCountryCode(String[] validIssuingCountries)
Set custom overlay image for the scanner.
@param customImage bitmap representation of the overlay image
public void setCustomImageOverlay(Bitmap customImage)
Set custom overlay image for the scanner.
@param customImageResId resource id of the overlay image
public void setCustomImageOverlay(int customImageResId)
Enable scan from gallery. If enabled, a "scan from gallery" button will appear in the scanner preview.
@param enabled indicates whether the "scan from gallery" is enabled. Default value: false.
public void enableScanFromGallery(boolean enabled)
Enable upside-down scanning. If enabled, the scanner will also try the upside-down format of the preview.
@param enabled indicates whether the "upside-down" feature is enabled. Default value: false.
public static void setEnableUpsideDownScanning(boolean enabled)
Specify whether validation of country codes is enabled.
@param enabled indicates whether validation is enabled. Default: true.
public static void setValidateCountryCodesEnabled(Boolean enabled)
Turn flash on or off.
@param on true = on, false = off. Default value: false.
public void toggleFlash(Boolean on)
Enable vibration on a successful scan.
@param enabled indicates whether the vibration feature is enabled. Default value: true.
public static void setEnableVibrationOnSuccess(boolean enabled)
Continuous scanning. If enabled, after a successful scan, the scanner view will not be paused.
See setIgnoreDuplicates for further continuous scanning behavior changes.
@param enabled indicates whether the scanner should stay active after a successful scan. Default value: false.
public void setContinuousScanningEnabled(boolean enabled)
Ignore duplicates when scanning continuously.
@param ignore indicates whether the scanner should repeat the last successful scan. Default value: true.
public void setIgnoreDuplicates(boolean ignore)