1.Copy MRZScannerDroidBinding.dll in the root folder of your project (MRZScannerAndroid\MRZScannerAndroid in our case)
2.Add reference to MRZScannerDroidBinding.dll file in your project references
3.Add the following code to the layout.xml of the activity where you want the scanner to be shown:
(it is recommended that the scanner takes full screen)
<fragment
android:id="@+id/scannerFragment"
android:name="com.scansolutions.mrzscannerlib.MRZScanner"
android:layout_width="match_parent"
android:layout_height="match_parent" />
4.Use this code to configure the scanner. The code should be added in the activity where the scanning will take place
MRZScanner mrzScanner =(MRZScanner)SupportFragmentManager.FindFragmentById(Resource.Id.scannerFragment);
mrzScanner.SetScannerType(ScannerType.ScannerTypeMrz); // Options: [ScannerTypeMrz, ScannerTypeDocImageId, ScannerTypeDocImagePassport]
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(this, "licenseKey");
string sdkVersion = MRZScanner.SdkVersion(); // Get the current SDK version of MRZScanner
5.Implement the IMRZScannerListener interface as a listener for a successful MRZ scan:
public class MainActivity : AppCompatActivity, IMRZScannerListener
{
.......
1.Copy MRZScanneriOSBinding.dll in the root folder of your project (MRZScanneriOS\MRZScanneriOS in our case)
2.Add reference to MRZScanneriOSBinding.dll file in your project references
3.Start the scanner with the following code:
MRZScannerController mrzScannerController = new MRZScannerController();
MRZScannerController.RegisterLicenseWithKey("license_key");
mrzScannerController.WeakDelegate = this;
UIViewController currentViewController = NavigationController;
if (currentViewController == null)
currentViewController = this;
currentViewController.AddChildViewController(mrzScannerController);
mrzScannerController.InitUI(currentViewController);
4.To get the result, implement the IMRZScannerDelegate:
public partial class ViewController : UIViewController, IMRZScannerDelegate { ...
Export("successfulScanWithResult:")]
public void SuccessfulScanWithResult(MRZResultDataModel mrzResultDataModel) { ...
5.Add the following row in your Info.plist file:
key: NSCameraUsageDescription
value: MRZ Scanner requires using camera
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 [ScannerTypeMrz, ScannerTypeDocImageId, ScannerTypeDocImagePassport]. Default value is ScannerTypeMrz
void SetScannerType(ScannerType scannerType)
Resume scanning after the scanner has been paused/stopped. Usually after a successful scan.
static void ResumeScanning()
string dateFormat = "dd.MM.yyyy";
void set(string dateFormat) {
MRZScanner.DateFormat = dateFormat;
}
@return the current SDK Version.
static string SdkVersion()
Specify whether the scanner should detect and return results for IDs.
@param isIDActive [true, false]. The default value is true.
static void SetIDActive(bool isIDActive)
Specify whether the scanner should detect and return results for passports.
@param isPassportActive [true, false]. The default value is true.
static void SetPassportActive(bool isPassportActive)
Specify whether the scanner should detect and return results for visas.
@param isVisaActive [true, false]. Default value is true.
static void SetVisaActive(bool isVisaActive)
Specify the maximum number of CPU threads that the scanner can use during the scanning process.
@param maxThreads number of CPU threads. The default value is 2.
static void SetMaxThreads(int maxThreads)
Set the zoom factor for the camera preview.
@param float. Default value is 1.0
void SetZoomFactor(float zoomFactor)
Register with the licence key provided to remove the asterisks (*) from the result.
@param context the activity's context.
@param key the provided licence key.
@return 0 for success, -1 if registration failed.
static int RegisterWithLicenseKey(Context context, string key)
To get your evaluation key today, register an account on https://mrzscanner.com/register
Scan a single image. (iOS Only)
@param image The UIImage to be scanned.
@return The result data model.
static MRZResultDataModel ScanImage(UIIMage image)
Scan for a single bitmap. (Android Only)
@param bitmap the image to be scanned.
@param activity the initialisation activity context.
@param listener result listener.
static void ScanBitmap(Bitmap bitmap, Activity activity, IMRZScannerListener listener)
Trigger an image picker.
@param activity the initialisation activity context.
@param listener result listener
static void ScanFromGallery(Activity activity, IMRZScannerListener listener)
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.
void SetScanningRectangle(int x, int y, int width, int height)
1.Before we start to work in Portable (Shared) project we must to configure platform specific projects (Android and iOS):
1.1Android platform specific project
- Copy MRZScannerDroidBinding.dll in your Android platform specific project
- Add reference to MRZScannerDroidBinding.dll file in your Android platform specific project references
- Create one layout.axml file and add the following code:
(It is recommended that the scanner takes full screen.)
<fragment
android:id="@+id/scannerFragment"
android:name="com.scansolutions.mrzscannerlib.MRZScanner"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- Create ViewRenderer that will help us to port our native fragment as control in portable project:
[assembly: ExportRenderer(typeof(MRZScannerForms.ScannerControl), typeof(MRZScannerForms.Droid.ScannerControlRenderer))]
namespace MRZScannerForms.Droid
{
public class ScannerControlRenderer : ViewRenderer
{
public ScannerControlRenderer(Context context) : base(context) { }
MRZScanner mrzScanner;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
Android.Views.View _scannerLayout = ((FormsAppCompatActivity)Context).LayoutInflater.Inflate(Resource.Layout.scannerFragment, this, false);
mrzScanner = (MRZScanner)((FormsAppCompatActivity)Context).SupportFragmentManager.FindFragmentById(Resource.Id.scannerFragment);
MRZScanner.RegisterWithLicenseKey(Context, "licenseKey");
if (Control == null)
SetNativeControl(_scannerLayout);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
try
{
Fragment fragment = ((FormsAppCompatActivity)Context).SupportFragmentManager.FindFragmentById(Resource.Id.scannerFragment);
if (fragment != null)
((FormsAppCompatActivity)Context).SupportFragmentManager.BeginTransaction().Remove(fragment).Commit();
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
mrzScanner = null;
}
base.Dispose(disposing);
}
}
}
- Implement the IMRZScannerListener interface as a listener for a successful MRZ scan in MainActivity:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, IMRZScannerListener { …
1.2 iOS platform specific project
- Copy MRZScanneriOSBinding.dll in your iOS platform specific project
- Add reference to MRZScanneriOSBinding.dll file in your iOS platform specific project references
- Create ViewRenderer that will help us to port our native fragment as control in portable project and implement IMRZScannerDelegate for a successful MRZ scan:
[assembly: ExportRenderer(typeof(MRZScannerForms.ScannerControl), typeof(MRZScannerForms.iOS.ScannerControlRenderer))]
namespace MRZScannerForms.iOS
{
public class ScannerControlRenderer : ViewRenderer, IMRZScannerDelegate
{
MRZScannerController mrzSannerController;
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
mrzSannerController = new MRZScannerController();
MRZScannerController.RegisterLicenseWithKey("licenseKey");
mrzSannerController.WeakDelegate = this;
UIViewController currentViewController = UIApplication.SharedApplication.KeyWindow.RootViewController.NavigationController != null ? UIApplication.SharedApplication.KeyWindow.RootViewController.NavigationController : UIApplication.SharedApplication.KeyWindow.RootViewController;
currentViewController.AddChildViewController(mrzSannerController);
mrzSannerController.InitUI(currentViewController);
if (Control == null)
SetNativeControl(mrzSannerController.View);
}
[Export("successfulScanWithResult:")]
public void SuccessfulScanWithResult(MRZResultDataModel mrzResultDataModel)
{
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (mrzSannerController != null)
{
mrzSannerController.CloseScanner();
mrzSannerController.Dispose();
mrzSannerController = null;
}
}
base.Dispose(disposing);
}
}
}
- Add the following row in your Info.plist file:
key: NSCameraUsageDescription
value: MRZ Scanner requires using camera
public class ScannerControl : Xamarin.Forms.View {
}
Here we can create bindable properties, event handlers, enums, etc. like we have in our sample applications.
<ContentPage.Content>
<!--It is recommended that the scanner takes fullscreen-->
<local:ScannerControl x:Name="scannerControl" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>