Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
feat(TE-726): Introduces NFC for Xamarin Android Native Examples App
Browse files Browse the repository at this point in the history
* (Native SDK binaries with the latest changes not yet included)
  • Loading branch information
ricardocolombo committed Jul 27, 2021
1 parent ba876b5 commit e3ac798
Show file tree
Hide file tree
Showing 22 changed files with 541 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Anyline")]
[assembly: AssemblyProduct("AnylineXamarinSDK.Droid")]
[assembly: AssemblyCopyright("Copyright © Anyline GmbH")]
[assembly: AssemblyCopyright("Copyright © 2021 Anyline GmbH")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Anyline")]
[assembly: AssemblyProduct("AnylineXamarinSDK.iOS")]
[assembly: AssemblyCopyright("Copyright © Anyline GmbH")]
[assembly: AssemblyCopyright("Copyright © 2021 Anyline GmbH")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
6 changes: 6 additions & 0 deletions Examples/AnylineExamples.Droid/AnylineExamples.Droid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<Compile Include="ScanActivity.cs" />
<Compile Include="ScanResultListener.cs" />
<Compile Include="Util.cs" />
<Compile Include="NFC\MRZScanActivity.cs" />
<Compile Include="NFC\NFCScanActivity.cs" />
</ItemGroup>
<ItemGroup>
<AndroidAsset Include="Assets\energy_config_dial.json" />
Expand Down Expand Up @@ -136,6 +138,7 @@
<AndroidResource Include="Resources\mipmap-xxxhdpi\ic_launcher.png" />
<AndroidResource Include="Resources\mipmap-xxxhdpi\ic_launcher_foreground.png" />
<AndroidResource Include="Resources\mipmap-xxxhdpi\ic_launcher_round.png" />
<AndroidResource Include="Resources\layout\activity_nfc.axml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Anyline.Xamarin.SDK.Droid" Version="33.0.0" />
Expand Down Expand Up @@ -182,6 +185,9 @@
<Generator>MSBuild:UpdateGeneratedFiles</Generator>
</AndroidResource>
</ItemGroup>
<ItemGroup>
<Folder Include="NFC\" />
</ItemGroup>
<Import Project="..\AnylineExamples.Shared\AnylineExamples.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
8 changes: 4 additions & 4 deletions Examples/AnylineExamples.Droid/Assets/id_config_mrz.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"id": "ID_PLUGIN",
"idPlugin": {
"mrzConfig": {
"faceDetection": true,
"mrzFieldScanOptions": {
"vizAddress": "optional",
"vizDateOfIssue": "optional"
"mrzMinFieldConfidences": {
"documentNumber": 90,
"dateOfBirth": 90,
"dateOfExpiry": 90
}
}
}
Expand Down
21 changes: 16 additions & 5 deletions Examples/AnylineExamples.Droid/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void OnCreate(Bundle savedInstanceState)
{
System.Diagnostics.Debug.WriteLine("Permission Granted!");
}

// INSERT YOUR LICENSE KEY HERE
string licenseKey = "ewogICJsaWNlbnNlS2V5VmVyc2lvbiI6IDIsCiAgImRlYnVnUmVwb3J0aW5nIjogIm9uIiwKICAiaW1hZ2VSZXBvcnRDYWNoaW5nIjogdHJ1ZSwKICAibWFqb3JWZXJzaW9uIjogIjI1IiwKICAibWF4RGF5c05vdFJlcG9ydGVkIjogNSwKICAiYWR2YW5jZWRCYXJjb2RlIjogdHJ1ZSwKICAibXVsdGlCYXJjb2RlIjogdHJ1ZSwKICAic3VwcG9ydGVkQmFyY29kZUZvcm1hdHMiOiBbCiAgICAiQUxMIgogIF0sCiAgInBpbmdSZXBvcnRpbmciOiB0cnVlLAogICJwbGF0Zm9ybSI6IFsKICAgICJpT1MiLAogICAgIkFuZHJvaWQiCiAgXSwKICAic2NvcGUiOiBbCiAgICAiQUxMIgogIF0sCiAgInNob3dQb3BVcEFmdGVyRXhwaXJ5IjogdHJ1ZSwKICAic2hvd1dhdGVybWFyayI6IHRydWUsCiAgInRvbGVyYW5jZURheXMiOiA5MCwKICAidmFsaWQiOiAiMjAyMS0xMi0zMSIsCiAgImlvc0lkZW50aWZpZXIiOiBbCiAgICAiY29tLmFueWxpbmUueGFtYXJpbi5leGFtcGxlcyIsCiAgICAiY29tLmFueWxpbmUueGFtYXJpbi5mb3Jtcy5leGFtcGxlcyIKICBdLAogICJhbmRyb2lkSWRlbnRpZmllciI6IFsKICAgICJjb20uYW55bGluZS54YW1hcmluLmV4YW1wbGVzIiwKICAgICJjb20uYW55bGluZS54YW1hcmluLmZvcm1zLmV4YW1wbGVzIgogIF0KfQpHUXhMK3NHQ3ZPWG50NGlqc1RGNzQ1dWZaSCtpZGw4VVUrRnBaWGVPSlJjVnNzRmduemJkL0JLZVh1N09iS25tYVpSbExqeldQS3BuclgvOGsrSEtDa0dQWDMraXhGQSs1SG0yeGRoRkZsQVRKUnBzVWdFUFo4R1BTazd2UENlNVBKejF0cU1kdjFraVhLMHkzRVRKVzBaNm4xeFQvdTBxaTRjZXVueTd6MU1pOXp1L05CZGk0dldRSnE5TTVCaUNobk13RDhKNDQ3WHNFazNMMndlOERtZjdzdDZrVkdWbkNJUktUQllqRzVjTEV6S09aY0YrMXAwand6UDcxN05IR21IMktHM2V1cCsremNud0tiaVY0SS83azNmb3hIY2pITUlyUjZrSkw4NzU2RGxBTTJ6bSs2VGtWTENLSmtRRDRScDlwMDFmaWIyTEZWUzZocWFSaHc9PQ==";
try
Expand Down Expand Up @@ -100,12 +100,23 @@ private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs a)
return;
try
{
var intent = new Intent(ApplicationContext, typeof(ScanActivity));

intent.PutExtra("jsonPath", jsonPath);
intent.PutExtra("title", item.Model.Name);
StartActivity(intent);
if (item.Model.Name == "Scan NFC of Passports")
{
var intent = new Intent(ApplicationContext, typeof(NFC.MRZScanActivity));

intent.PutExtra("jsonPath", jsonPath);
intent.PutExtra("title", item.Model.Name);
StartActivity(intent);
}
else
{
var intent = new Intent(ApplicationContext, typeof(ScanActivity));

intent.PutExtra("jsonPath", jsonPath);
intent.PutExtra("title", item.Model.Name);
StartActivity(intent);
}
}
catch (Java.Lang.ClassNotFoundException e)
{
Expand Down
177 changes: 177 additions & 0 deletions Examples/AnylineExamples.Droid/NFC/MRZScanActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using System;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using IO.Anyline.Camera;
using IO.Anyline.View;

using Android.Support.V7.App;
using Android.Util;
using IO.Anyline.Plugin.ID;
using IO.Anyline.Plugin;
using Anyline.Droid.NFC;

namespace AnylineExamples.Droid.NFC
{
[Activity(Label = "", MainLauncher = false, //ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait,
HardwareAccelerated = true)]
public class MRZScanActivity : AppCompatActivity, IScanResultListener
{
public static readonly string TAG = typeof(ScanActivity).Name;

private ScanView _scanView;
private bool _isInitialized = false;

public MRZScanActivity()
{
}

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

try
{
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);

// we pass the title from the previous activity
Title = Intent.GetStringExtra("title");

Window.SetFlags(WindowManagerFlags.KeepScreenOn, WindowManagerFlags.KeepScreenOn);
SetContentView(Resource.Layout.scan_activity);

_scanView = FindViewById<ScanView>(Resource.Id.scan_view);

// the initialization parses the json configuration and builds the whole use-case
_scanView.Init("id_config_mrz.json");

// Activates Face Detection if the MRZ Scanner was initialized
(((_scanView.ScanViewPlugin as IdScanViewPlugin)?.ScanPlugin as IdScanPlugin)?.IdConfig as MrzConfig)?.EnableFaceDetection(true);

_scanView.ScanViewPlugin.AddScanResultListener(this);

// handle camera open events
_scanView.CameraOpened += ScanView_CameraOpened;

// handle camera error events
_scanView.CameraError += ScanView_CameraError;

_isInitialized = true;
}
catch (Exception e)
{
Util.ShowError(e.ToString(), this);
}
}

protected override void OnResume()
{
base.OnResume();

try
{
if (_isInitialized)
_scanView.Start();
}
catch (Exception e)
{
Log.Debug(TAG, e.ToString());
}
}

protected override void OnPause()
{
base.OnPause();
StopScanning();
}

private void ScanView_CameraError(object sender, CameraErrorEventArgs e)
{
Log.Debug(TAG, e.ToString());
}

private void ScanView_CameraOpened(object sender, CameraOpenedEventArgs args)
{

}

public void OnResult(Java.Lang.Object result)
{
var scanResult = result as ScanResult;

var mrzIdentification = scanResult.Result as MrzIdentification;


// Gets the data necessary for the NFC reading
string passportNumber = mrzIdentification.DocumentNumber.Trim();
Java.Util.Date dateOfBirth = mrzIdentification.DateOfBirthObject;
Java.Util.Date expirationDate = mrzIdentification.DateOfExpiryObject;
var dateFormat = new Android.Icu.Text.SimpleDateFormat("yyMMdd");

// The passport number passed to the NFC chip must have a trailing < if there is one in the MRZ string.
string passportNumberForNFC = string.Copy(passportNumber);

while (passportNumberForNFC.Length < 9)
{
passportNumberForNFC += "<";
}

// Open the Activity responsible for listening to the NFC calls and reading the chip.
// We use data from the MRZ to authenticate with the chip.

var nfcActivity = new Intent(this, typeof(NFCScanActivity));
nfcActivity.PutExtra("pn", passportNumberForNFC);
nfcActivity.PutExtra("db", dateFormat.Format(dateOfBirth));
nfcActivity.PutExtra("de", dateFormat.Format(expirationDate));
StartActivity(nfcActivity);
}

#region going back & cleanup
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
StopScanning();
Finish();
return true;
default:
return base.OnOptionsItemSelected(item);
}
}

private void StopScanning()
{
if (_scanView != null)
{
_scanView.Stop();
_scanView.CameraView.ReleaseCamera();
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
try
{
if (_scanView != null)
{
_scanView.Dispose();
_scanView.CameraOpened -= ScanView_CameraOpened;
_scanView.CameraError -= ScanView_CameraError;
_scanView = null;

GC.Collect();

_isInitialized = false;
}
}
catch (Exception) { }
Finish();
}
#endregion
}
}
Loading

0 comments on commit e3ac798

Please sign in to comment.