Xamarin Implementation
Interaction with the SmartID Scan library is done with a SmartScanner object. Instantiate it inside onCreate() method in your Activity and pass it a reference to your context.
mSmartScanner = new SmartScanner(Context);
With the new version of SmartID Scan library you have a second constructor available.
mSmartScanner = SmartScanner( Context, CameraId, MinScoreCameraAPI2);
CameraId : Set the camera id of your device. If you want that the library choose the best camera put SmartScanner.NoCameraId
as value.
MinScoreCameraAPI2 : the minimal support for use the new camera Api2 in SmartID Scan library.
The options are : LevelLimited, LevelFull, Level3, DefaultMinimalScore. DefaultMinimalScore is by default set to LevelLimited
For more information about levels support on android
Now set the decoding type according to the decoding library you downloaded, using SetDecodingType() method call on the SmartScanner object. This method allows use of bitwise operators if you have multiple decoding options :
mSmartScanner.SetDecodingType(CodeOcrb | Code1d2d , CodeBvr | CodeQrc);
When using multiple decoders you will need probably to set manually the size of the region on interest (ROI) to have better results.
As SmartScanner involves calls to the Android Camera API, it is tightly coupled with the Activity lifecycle.
- Call Init() method of SmartScanner object in OnResume() :
public override void OnResume()
{
base.OnResume();
if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
{
if (ContextCompat.CheckSelfPermission(Application.Context, Manifest.Permission.Camera) != (int)Permission.Granted)
{
// Camera permission has not been granted
// To improve with your own permision dialog, this is only as example
Toast.MakeText(Application.Context, Resource.String.request_permission, ToastLength.Short).Show();
}
else
{
// Camera permissions is already available, show the camera preview.
mSmartScanner.Init();
}
}
else
{
// Initialize the SmartScanner library
mSmartScanner.Init();
}
}
- To ensure resources are released when not needed anymore, call Release() on SmartScanner object in OnPause().
public override void OnPause()
{
mSmartScanner.Release();
base.OnPause();
}
Layout and Views
Define a layout, with one CanvasDrawer for target display and one SurfaceView for camera preview, laying on top of each other :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ch.icare.smartscan.views.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ch.icare.smartscan.views.CanvasDrawer
android:id="@+id/canvasdrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#644b4b4b" />
<View
android:id="@+id/focus"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
android:background="@drawable/rectangle" />
</FrameLayout>
Pass those two views to SmartScanner inside Activity or Fragment. Ensure you are fullscreen to prevent distorted preview from camera :
mSmartScanner.SetCanvasDrawer((CanvasDrawer)root.FindViewById(Resource.Id.canvasdrawer)); // Target
mSmartScanner.SetTextureView((AutoFitTextureView)root.FindViewById(Resource.Id.texture)); // Camera preview
Permissions
Set needed features in app manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="ch.icare.demo_xamarin">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera2.full" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
</application>
</manifest>
Depending on your targeted API level (23 and +), you should also manage app permission for the camera into your code.
Best practices
Some fonctionalities can only be activated when the library is fully loaded. To ensure that use the listener IOnDecoderLoadedListener:
public void OnDecoderLoaded(Rect roi)
{
// Your code here
}
Documentation
The documentation provided it's created from JavaDoc. After binding (aar to ddl) the SmartScan library it's converted to respect C# syntax.