Skip to main content
Version: 3.11

Base Implementation

SmartScanner

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(getContext());

With the new version of SmartID Scan library you have a second constructor available.

mSmartScanner = SmartScanner(getContext(), cameraId, minScoreCameraAPI2);

cameraId : Set the camera id of your device. If you want that the library choose the best camera put NO_CAMERA_ID as value. minScoreCameraAPI2 : the minimal support for use the new camera api2 in SmartID Scan library. The options are : LEVEL_LIMITED, LEVEL_FULL, LEVEL_3, DEFAULT_MINIMAL_SCORE. DEFAULT_MINIMAL_SCORE is by default set to LEVEL_LIMITED

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(IcareDecoders.CODE_OCRB | IcareDecoders.CODE_SWISS_PLATES, IcareDecoders.CODE_BVR | IcareDecoders.CODE_CH_DRV_LICENSES);

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() :
@Override
public void onResume() {
//Checks if the application as the rights permission to run the camera
if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED{
//To improve with your own permision dialog, this is only as example
Toast toast = Toast.makeText(getContext(), R.string.request_permission, Toast.LENGTH_SHORT);
toast.show();
}
else {
//Initialize the SmartScanner library
mSmartScanner.init();
}
}
  • To ensure resources are released when not needed anymore, call release() on SmartScanner object in onPause().
@Override
public void onPause() {
mSmartScanner.release();
super.onPause();
}

Dont't forget that if you are using the new fonctionalities like zoom or some toggle buttons, to reset the value by default or save the state of your application before it will be destroyed.

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
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Displays the camera preview -->
<ch.icare.smartscan.views.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<!-- Displays targeting help. You can set target window color and width here using custom attributes -->
<ch.icare.smartscan.views.CanvasDrawer
android:id="@+id/canvasdrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimaryDark"
custom:targetWidth="4dp"
custom:targetColor="@color/colorAccent"/>

<!-- Displays the focus area -->
<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)findViewById(R.id.canvasdrawer));   
mSmartScanner.setSurfaceView((SurfaceView)findViewById(R.id.surfaceview));

CameraX

The new version of the SmartID Scan library is compatible with CameraX, the latest camera API for Android. To use it, it is necessary to make some additions :

Add following dependencies in gradle :

   def camerax_version = "1.1.0"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-view:$camerax_version"

Add a PreviewView in layout :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Wrap the CameraX preview -->
<androidx.camera.view.PreviewView
android:id="@+id/previewview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<!-- Displays the camera preview -->รŸ
<ch.icare.smartscan.views.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

<!-- Displays targeting help. You can set target window color and width here using custom attributes -->
<ch.icare.smartscan.views.CanvasDrawer
android:id="@+id/canvasdrawer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimaryDark"
custom:targetWidth="4dp"
custom:targetColor="@color/colorAccent"/>

<!-- Displays the focus area -->
<View
android:id="@+id/focus"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
android:background="@drawable/rectangle" />

</FrameLayout>

Instanciate the SmartScanner with the new constructor

PreviewView previewView = findViewById(R.id.previewview);
SmartScanner smartScanner = new SmartScanner(this, previewView);

Proceed as usual.

Permissions

Set needed features in app manifest :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smartscan.myapplication">

<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.camera" ">
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" ">
<uses-feature
android:name="android.hardware.camera.flash"
android:required="false" ">

...


</manifest>

Depending on your targeted API level (23 and +), you should also manage app permission for the camera into your code.

QR-Code with ECI support

Since the version 2.0.0, the QR code scanner is compliant to the last ISO/IEC 18004 norm. As before the library will decode the Qr-Code and parse it to give you the formatted result.

If something get's wrong during the parsing you will informed by the method getErrorParsingQrCode() and the specific error will be put in the result getResultString().

Here an example:

mSmartScanner.setOnResultListener(new OnDecoderResultFoundListener() {
@Override
public void onResultFound(DecodedResult result) {
if(result.getErrorParsingQrCode()) {
Log.d("Error" , "ERROR WHEN PARSING QR CODE: "+result.getResultString());
}
}
});

Best practices

Some fonctionalities can only be activated when the library is fully loaded. To ensure that use the listener OnDecoderLoadedListener:

mSmartScanner.setOnLoadedListener(new OnDecoderLoadedListener() {
@Override
public void onDecoderLoaded(Rect roi) {
//Put your code here
}
});