Skip to main content

Android Integration

This guide will help you integrate the OCR Lite SDK into your Android application.

note

You can find sample project containing complete integration of SDK here.

Installation

info

OCR Lite SDK is built with the support for minSdk 23 and compiled on JAVA 17.

Prerequisites

Make sure you are provided with the following required credentials for integration:

S3_BUCKET_ID, S3_ACCESS_KEY, S3_SECRET_KEY and the latest communicated version.

Implementation

Include AWS maven repository (where the sdk is deployed) at the project's dependency management.

 repositories {
...
maven {
url 's3://' + S3_BUCKET_ID
credentials(AwsCredentials) {
accessKey S3_ACCESS_KEY
secretKey S3_SECRET_KEY
}
}
}

Add the following dependency in project's app/build.gradle:

dependencies {
...
implementation 'com.fetchsky.ocrlite:sdk:<version-code>'
}

Initialization

In order to launch OCR SDK follow the steps below:

1. Create Intent

Create an intent at the preceding event listener

val intent = Intent(this, com.fetchsky.ocrlitesdk.OcrActivity::class.java)

2. Append required parameters

intent.putExtra(OcrCommunication.ParamKey.IDENTITY_NUMBER.value, cnic_number)
intent.putExtra(OcrCommunication.ParamKey.IMAGE_WIDTH.value, 1000)
intent.putExtra(OcrCommunication.ParamKey.FONT.value, R.font.poppins)
intent.putExtra(OcrCommunication.ParamKey.PRIMARY_COLOR.value, "#007083")

Parameters details are as follows:

IDENTITY_NUMBER: Original identity number which is to be compared with the recognized one.

IMAGE_WIDTH: Desired image width which will correspond to the size of the captured card pictures.

FONT (optional): Resource identifier of the font family for headings and subtitles.

PRIMARY_COLOR (optional): Accent color of your app's theme which will be reflected at OCR interface.

3. Launch Activity 🚀

startActivity(intent)

Receive events

Receive messages sent from OCR activity and handle them accordingly.

1. Create a Broadcast Receiver

Events are posted with a standard receiver action:

OCR_RECEIVER_ACTION = "com.fetchsky.ocrlitesdk.OCR_MESSAGE"

Create receiver class:

private val ocrMessagesReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action.equals(OCR_RECEIVER_ACTION)) {
handleResult(intentType, payload)
}
}
}
}

2. Initialize the receiver

Initialize OCR broadcast receiver at launch:

ContextCompat.registerReceiver(
this,
ocrMessagesReceiver,
IntentFilter(OCR_RECEIVER_ACTION),
ContextCompat.RECEIVER_EXPORTED
)

Prefer using ContextCompat receiver registration for lower api level support.

3. Handle results

info

Event sent from OCR Activity will be one of the defined types listed

Extract the enum of posted event type as follows and handle them as per the need.

fun handleResult(type: String, payload: Bundle) {
val messageTypes = OcrCommunication.PostMessage.entries.associateBy { it.name }
val event = messageTypes[type]
...
}