[Huawei Developers] John Wo:
Introduction

In an age of digital living, maintaining interpersonal connections has never been more important. It's a common ritual for professionals to exchange business cards, but this can come with certain hassles – having to input the contact information for each new contact, including names, companies, and addresses. That's where business card recognition comes into the picture.

How It Works

The business card recognition service utilizes the optical character recognition (OCR) technology to digitize text on a card, converting it into an editable format, and classifying the recognized information by category. When you have integrated the service into your app, your users will be able to collect information on a business card simply by snapping a picture, scanning the QR code on the card, or importing the card image.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106085412.45928797994507009333856783674485:50511110020443:2800:AB945E02AEF19538B4B2F5F04372A8E072DC29E2EDF2188C4810DA78ECB66CD1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106085412.45928797994507009333856783674485:50511110020443:2800:AB945E02AEF19538B4B2F5F04372A8E072DC29E2EDF2188C4810DA78ECB66CD1.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106085430.68185212739181808540547524278383:50511110020443:2800:097E2730177299741CB9802187A6D1B961A6E1F260BF425925D2E892A3B1C864.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106085430.68185212739181808540547524278383:50511110020443:2800:097E2730177299741CB9802187A6D1B961A6E1F260BF425925D2E892A3B1C864.png[/IMG]

Preparations

Before API development, there are a few things that you'll need to do in preparation, for example, configuring the Maven repository address of the HMS Core SDK in your project, and integrating the SDK for the business card recognition service.

Install Android Studio

Official download website

Installation guide

Add Huawei Maven Repository to the Project-Level build.gradle File

Open the build.gradle File in the Root Directory of Your Android Studio Project

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106085807.78107832783841683917761417698020:50511110020443:2800:AE3CEB6CD23CBC85CC61B48D54B7BD7BD1203742023DED7594842424E3C72AAC.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106085807.78107832783841683917761417698020:50511110020443:2800:AE3CEB6CD23CBC85CC61B48D54B7BD7BD1203742023DED7594842424E3C72AAC.png[/IMG]

Add the Maven Repository Address

Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.

repositories {
maven { url 'https://developer.huawei.com/repo/' }

}
}
Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.
allprojects {
repositories {
maven { url 'https://developer.huawei.com/repo/' }

}
}

Import the SDK

dependencies {
// Text recognition SDK.
implementation 'com.huawei.hms:ml-computer-vision-ocr:2.0.1.300'
// Text recognition model.
implementation 'com.huawei.hms:ml-computer-vision-ocr-cn-model:2.0.1.300'
implementation 'com.huawei.hms:ml-computer-vision-ocr-jk-model:2.0.1.300'
implementation 'com.huawei.hms:ml-computer-vision-ocr-latin-model:2.0.1.300'
}

Add the Manifest File

<manifest
...
<meta-data
android:name="com.huawei.hms.ml.DEPENDENCY"
android:value="ocr" />
...
</manifest>

Add Permissions

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus" />

Submit a Dynamic Permission Application

if (!(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
requestCameraPermission();
}

Development

1. Create the text analyzer MLTextAnalyzer to recognize text within images. Use the custom parameter MLLocalTextSetting to configure the on-device text analyzer.

MLLocalTextSetting setting = new MLLocalTextSetting.Factory()
.setOCRMode(MLLocalTextSetting.OCR_DETECT_MODE)
.setLanguage("zh")
.create();
MLTextAnalyzer analyzer = MLAnalyzerFactory.getInstance()
.getLocalTextAnalyzer(setting);

2. Use android.graphics.Bitmap to create an MLFrame. Supported image formats include JPG, JPEG, PNG, and BMP. It is recommended that the aspect ratio for landscape business cards be 2:1 and that for portrait business cards be 1:2.

MLFrame frame = MLFrame.fromBitmap(bitmap);

3. Pass the MLFrame object to the asyncAnalyseFrame method for text recognition.

Task<MLText> task = analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLText>() {
@Override
public void onSuccess(MLText text) {
// Recognition success.

}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Recognition failure.

}
});

4. After recognition is complete, stop the analyzer to release recognition resources.

try {
if (analyzer != null) {
analyzer.stop();
}
} catch (IOException e) {
// IOException
} catch (Exception e) {
// Exception
}

More Information

We have developed a demo app that demonstrates the effects of the business card recognition service.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106095314.97893368821468875053411435365357:50511110020443:2800:2D54995EC8049AF43905B45D791B57F7B9C834D92970F7960A7ED74D210BBC87.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/431/408/2640065000000431408.20201106095314.97893368821468875053411435365357:50511110020443:2800:2D54995EC8049AF43905B45D791B57F7B9C834D92970F7960A7ED74D210BBC87.gif[/IMG]

Reference

Official website of Huawei Developers

Development Guide

HMS Core official community on Reddit

Demo and sample code

Discussions on Stack Overflow