SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the...

26
Optical Character Recognition SDK Reference Issue 01 Date 2020-11-27 HUAWEI TECHNOLOGIES CO., LTD.

Transcript of SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the...

Page 1: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Optical Character Recognition

SDK Reference

Issue 01

Date 2020-11-27

HUAWEI TECHNOLOGIES CO., LTD.

Page 2: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Copyright © Huawei Technologies Co., Ltd. 2020. All rights reserved.

No part of this document may be reproduced or transmitted in any form or by any means without priorwritten consent of Huawei Technologies Co., Ltd. Trademarks and Permissions

and other Huawei trademarks are trademarks of Huawei Technologies Co., Ltd.All other trademarks and trade names mentioned in this document are the property of their respectiveholders. NoticeThe purchased products, services and features are stipulated by the contract made between Huawei andthe customer. All or part of the products, services and features described in this document may not bewithin the purchase scope or the usage scope. Unless otherwise specified in the contract, all statements,information, and recommendations in this document are provided "AS IS" without warranties, guaranteesor representations of any kind, either express or implied.

The information in this document is subject to change without notice. Every effort has been made in thepreparation of this document to ensure accuracy of the contents, but all statements, information, andrecommendations in this document do not constitute a warranty of any kind, express or implied.

Huawei Technologies Co., Ltd.Address: Huawei Industrial Base

Bantian, LonggangShenzhen 518129People's Republic of China

Website: https://www.huawei.com

Email: [email protected]

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. i

Page 3: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Contents

1 Introduction to OCR SDK....................................................................................................... 1

2 SDK Environment Setup.........................................................................................................22.1 Preparing a Java Development Environment................................................................................................................ 22.2 Preparing a Python Development Environment........................................................................................................... 42.3 Preparing an iOS Development Environment............................................................................................................... 52.4 Preparing an Android Development Environment.......................................................................................................62.5 Preparing a Node.js Development Environment.......................................................................................................... 7

3 Using Java SDK......................................................................................................................... 8

4 Using Python SDK................................................................................................................. 11

5 Using iOS SDK........................................................................................................................ 14

6 Using Android SDK................................................................................................................ 17

7 Using Node.js SDK................................................................................................................. 20

A Change History...................................................................................................................... 23

Optical Character RecognitionSDK Reference Contents

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. ii

Page 4: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

1 Introduction to OCR SDK

OCR OverviewOptical Character Recognition (OCR) can convert characters in images or scannedcopies into editable text, eliminating the need for manual input to improve serviceefficiency. It can recognize characters on ID cards, driving licenses, vehicle licenses,receipts, general tables, and general text.

OCR provides services through open application programming interfaces (APIs).You can obtain the inference result by accessing and invoking APIs in real time,greatly improving business efficiency.

SDK OverviewOptical Character Recognition Software Development Kit (OCR SDK for short)encapsulates the RESTful APIs provided by OCR to simplify applicationdevelopment. You can directly call API functions provided by OCR SDKs to useOCR.

Mappings Between Services and APIsTable 1-1 lists the mappings between the OCR sub-services and APIs.

Table 1-1 Mappings between services and APIs

Service API

Passport OCR POST /v1.0/ocr/passport

Thai ID Card OCR POST /v1.0/thailand-id-card

Myanmar ID Card OCR POST /v1.0/myanmar-id-card

Myanmar Driver License OCR POST /v1.0/ocr/myanmar-driver-license

Optical Character RecognitionSDK Reference 1 Introduction to OCR SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 1

Page 5: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

2 SDK Environment Setup

2.1 Preparing a Java Development Environment

Scenario

OCR JAVA SDK supports Windows, Linux, and Mac operating systems. This sectionuses Windows as an example to describe how to configure the environment. Table2-1 describes the required operating environment.

Table 2-1 Environment preparation

Item Description

Operating system (OS) Windows OS. Windows 7 or later isrecommended.

Installing JDK Basic configuration for the developmentenvironment. The version must be JDK 8 orlater.

Installing and configuringEclipse

Tool used for developing OCR applications

Procedure

The following describes how to configure the JDK 8 (64-bit) in the Windows 7environment. If you have downloaded JDK and configured the environment, skipsteps 1 to 4. The following uses Eclipse as an example to describe how to importthe SDK project. Operations of other IDEs are similar to those of Eclipse.

1. Download the JDK file.

2. After the JDK file is downloaded, install the JDK as prompted. For example,install the JDK to the C:\Program Files\Java\jdk1.8.0_131 directory on thelocal PC.

Optical Character RecognitionSDK Reference 2 SDK Environment Setup

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 2

Page 6: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

3. Right-click Computer, choose Properties > Advanced System Settings >Environment Variables, and perform the following operations to configureJava environment variables:

a. Create system variable JAVA_HOME whose value is the JDK installationpath.

b. Add %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin to Path. Separatemultiple values with semicolons (;).

4. Open the command line interface (CLI) and run java -version. If theinformation shown in Figure 2-1 is displayed, the configuration is successful.

Figure 2-1 Java version information

5. Install EclipseDownload Eclipse, for example, eclipse-jee-mars-R-win32-x86_64.zip, fromhttps://www.eclipse.org/downloads/packages/. Extract and open Eclipse.Configure the correct JRE path in Window > Preferences > Java > InstalledJREs.

6. Import the Java SDK project.

a. Right-click on the Package Explorer page on the left and choose Importfrom the shortcut menu.

b. Choose General > Existing Projects into Workspace.c. Click Next, select the Maven dependency mode, and select the path

where the OCR SDK demo package is stored.d. Click Finish to import the demo project. After the demo project is

imported, open it. The project directory is as follows:

Optical Character RecognitionSDK Reference 2 SDK Environment Setup

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 3

Page 7: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Figure 2-2 Project directory

2.2 Preparing a Python Development Environment

ScenarioOCR Python SDK supports Windows, Linux, and Mac operating systems. Thissection uses Windows as an example to describe how to configure theenvironment. Table 2-2 describes the required operating environment.

Table 2-2 Environment preparation

Item Description

OS Windows OS. Windows 7 or later isrecommended.

Installing Python Python 2.7 or later is recommended.

Installing Python librarydependency

OCR Python SDK depends on the third-partyrequests 2.18.2 library package.

Optical Character RecognitionSDK Reference 2 SDK Environment Setup

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 4

Page 8: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

ProcedureThe following describes how to install and configure Python 2.7 in the Windows 7environment. If you have installed Python 2.7 or later and requests 2.18.2, skipthis section.

1. Download your desired Python installation package from https://www.python.org/downloads/release/python-2711/

2. After the Python installation package is downloaded, install it as prompted.The default path is C:\Python27.

3. Right-click Computer, choose Properties > Advanced System Settings >Environment Variables. Add C:\Python27;C:\Python27\Scripts to Path.

4. Open the command line interface (CLI) and run python –v. If the informationshown in Figure 2-3 is displayed, the configuration is successful.

Figure 2-3 Python version information

5. Run the following command in the new CMD window of PIP to install therequests package:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple requests --trusted-host https://pypi.tuna.tsinghua.edu.cn/simple

6. After the installation is complete, run the PIP command to check whether therequests package is correctly installed. See Figure 2-4.

Figure 2-4 Installation confirmation

2.3 Preparing an iOS Development Environment

ScenarioTable 2-3 describes the environment to be prepared for using OCR iOS SDK.

Optical Character RecognitionSDK Reference 2 SDK Environment Setup

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 5

Page 9: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Table 2-3 Environment preparation

Item Description

OS iOS 9.0 or later running on macOS

Installing Xcode Xcode 10.0 or later is recommended.

ProcedureXcode is an integrated development environment (IDE) for iOS-baseddevelopment. The following describes how to use Xcode 10.0 to create an iOSdevelopment environment. If you have installed and configured Xcode, skip thissection.

1. Register an Apple ID.2. Search for Xcode in App Store and click GET.3. After Xcode is downloaded, click Lanchpad. Xcode is displayed.

2.4 Preparing an Android Development Environment

ScenarioOCR Android SDK supports Windows, Linux, and Mac operating systems. Thissection uses Windows as an example to describe how to configure theenvironment. Table 2-4 describes the required operating environment.

Table 2-4 Environment preparation

Item Description

OS Windows OS. Windows 7 or later isrecommended.

Installing Android Studio Android Studio 3.1.3 or later is recommended.

ProcedureOCR Android SDK uses the Android Studio IDE for development. The followingdescribes how to install and configure Android Studio 3.1.3 in the Windows 7environment. If you have installed and configured Android Studio, skip this section.

1. Prepare a Java development environment by referring to the instructions inPreparing a Java Development Environment.

2. Configure Android Studio.Go to https://developer.android.com/studio#downloads to downloadAndroid Studio 3.1.3 for the Windows environment.

3. Start Android Studio, select Open an existing Android Studio project, andthen select the path of the OCR Android SDK package on the local PC.

Optical Character RecognitionSDK Reference 2 SDK Environment Setup

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 6

Page 10: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

4. The project targets API level 26. If the SDK does not have this version, installAndroid API 26.

2.5 Preparing a Node.js Development Environment

Scenario

OCR Node.js SDK supports Windows, Linux, and Mac operating systems. Thissection uses Windows as an example to describe how to configure theenvironment. Table 2-5 describes the required operating environment.

Table 2-5 Environment preparation

Item Description

OS Windows OS. Windows 7 or later isrecommended.

Installing Node.js Node.js v10.13.0 or later is recommended.

Installing Node.js librarydependency

OCR Node.js SDK depends on the third-partyrequest, moment, and moment-timezonelibrary packages.

Procedure

The following describes how to install and configure Node.js v10.13.0 in theWindows 7 environment. If you have installed Node.js v10.13.0 or later and therequired library packages, skip this section.

1. Configure Node.js.Go to https://nodejs.org/en/download/ to download the latest Node.js.Right-click Computer, choose Properties > Advanced System Settings >Environment Variables, and add C:\Program Files\nodejs (defaultinstallation path of node.exe) to Path.

2. Verify Node.js.Run the node –v command in the system CLI to check the Node.js version. Ifthe current version number is displayed, the configuration is successful.

3. Run the following command to configure cnpm and the software library.$ npm install -g cnpm --registry=https://registry.npm.taobao.org

If you have configured them, go to 4.4. Install a dependency package.

Go to the root directory of Node.js. Press and hold down Shift, right-click inthe window, and choose Open command window here from the shortcutmenu. Then, run the following commands:cnpm install request –savecnpm install moment --savecnpm install moment-timezone --save

Optical Character RecognitionSDK Reference 2 SDK Environment Setup

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 7

Page 11: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

3 Using Java SDK

Users can use SDK in token- or AK/SK-based authentication mode. For detailsabout how to obtain authentication messages, see Authentication. This sectiondescribes how to use SDK in token- and AK/SK-based modes.

Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/sdk?OCR, and decompress it.

NO TE

● All images in this document are composite images and used for reference only.

● A user needs to apply for enabling the service before using it for the first time. Fordetails about how to enable the service, click Subscribe. You only need to subscribe toOCR once.

AK/SK-based Authentication

This section uses Passport OCR as an example to describe how to use SDK inAK/SK-based authentication mode.

1. Obtain AK/SK. For details, see Authentication > AK/SK-basedAuthentication.

2. Configure AK/SK of the Java SDK.Change the values of AK and SK of the AKSKDemo function in theOCRDemo.java file of the demo project to the obtained AK/SK.

Figure 3-1 Modifying parameters in the OCRDemo.java file

Optical Character RecognitionSDK Reference 3 Using Java SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 8

Page 12: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

3. Modify parameters.If you use a local image file for recognition, change the image path inOCRDemo.java to the path of the file. If you use the default image of theSDK, you do not need to modify the image path.

4. Execute the OCRDemo.java file. If 200 is displayed on the console, theprogram is successfully executed. You can view the recognition result ofPassport OCR on the console.{ "result": { "country_code": "IND", "surname": "xxxx", "given_name": "ASHISH", "passport_number": "G0080xxx", "date_of_birth": "1988-xx-xx", "sex": "M", "date_of_expiry": "2017-11-25", "machine_code": "P<INDKUMAR<<ASHISH<<<<<<<<<<<<<<<<<<<<<<<<<", "machine_code2": "G0080008<2IND8808188M1711257<<<<<<<<<<<<<<<8", "extra_info": {}, "confidence": { "country_code": 0.9684, "surname": 0.9684, "given_name": 0.9684, "passport_number": 0.9276, "date_of_birth": 0.9276, "sex": 0.9276, "date_of_expiry": 0.9276, "machine_code": 0.9684, "machine_code2": 0.9276 } }}

NO TE

To call other OCR services, change the value of parameter httpUri of the AKSKDemofunction to the specific URI. For the URI list, see Mappings Between Services andAPIs.

Token-based AuthenticationThis section uses Passport OCR as an example to describe how to use SDK intoken-based authentication mode.

1. Open the OCRDemo.java file in the OCRDemo package. Change the valuesof username, password, and domainName in the TokenDemo function tothe actual username, password, and domain name registered with the system.If the user is not an IAM user, the domain name and username are the same.

Optical Character RecognitionSDK Reference 3 Using Java SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 9

Page 13: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Figure 3-2 Changing the username, password, and domain name in theOCRDemo.java file

2. Execute the code. The recognition result of Passport OCR is displayed.

Status CodeFor details about status code, see Status Code.

Error CodeFor details about error code, see Error Code.

Optical Character RecognitionSDK Reference 3 Using Java SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 10

Page 14: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

4 Using Python SDK

Users can use SDK in token- or AK/SK-based authentication mode. For detailsabout how to obtain authentication messages, see Authentication. This sectiondescribes how to use SDK in token- and AK/SK-based modes.

Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/sdk?OCR, and decompress it.

NO TE

A user needs to apply for enabling the service before using it for the first time. For detailsabout how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based AuthenticationThis section uses Passport OCR as an example to describe how to use SDK inAK/SK-based authentication mode.

1. Obtain AK/SK. For details, see Authentication > AK/SK-basedAuthentication.

2. Configure AK/SK of the Python SDK.Change the values of AK and SK of the aksk_request function in theOCRDemo.py file of the demo project to the obtained AK/SK.

Figure 4-1 Modifying parameters in the OCRDemo.py file

3. Modify parameters.If you use a local image file for recognition, change the image path inOCRDemo.py to the path of the file. If you use the default image of the SDK,you do not need to modify the image path.

Optical Character RecognitionSDK Reference 4 Using Python SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 11

Page 15: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

4. Execute the OCRDemo.py file. If 200 is displayed on the console, the programis successfully executed. You can view the recognition result of Passport OCRon the console.{ "result": { "country_code": "IND", "surname": "xxxx", "given_name": "ASHISH", "passport_number": "G0080xxx", "date_of_birth": "1988-xx-xx", "sex": "M", "date_of_expiry": "2017-11-25", "machine_code": "P<INDKUMAR<<ASHISH<<<<<<<<<<<<<<<<<<<<<<<<<", "machine_code2": "G0080008<2IND8808188M1711257<<<<<<<<<<<<<<<8", "extra_info": {}, "confidence": { "country_code": 0.9684, "surname": 0.9684, "given_name": 0.9684, "passport_number": 0.9276, "date_of_birth": 0.9276, "sex": 0.9276, "date_of_expiry": 0.9276, "machine_code": 0.9684, "machine_code2": 0.9276 } }}

NO TE

To call other OCR services, change the value of parameter req_uri of theaksk_request function to the specific URI. For the URI list, see Mappings BetweenServices and APIs.

Token-based Authentication

This section uses Passport OCR as an example to describe how to use SDK intoken-based authentication mode.

1. Open the OCRDemo.py file. Change the values of username, password, anddomain_name in the token_request function to the actual username,password, and domain name registered with the system. If the user is not anIAM user, the domain name and username are the same.

Figure 4-2 Changing the username, password, and domain name in theOCRDemo.py file

Optical Character RecognitionSDK Reference 4 Using Python SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 12

Page 16: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

2. Execute the code. The recognition result of Passport OCR is displayed.

Status CodeFor details about status code, see Status Code.

Error CodeFor details about error code, see Error Code.

Optical Character RecognitionSDK Reference 4 Using Python SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 13

Page 17: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

5 Using iOS SDK

Users can use SDK in token- or AK/SK-based authentication mode. For detailsabout how to obtain authentication messages, see Authentication. This sectiondescribes how to use SDK in token- and AK/SK-based modes.

Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/sdk?OCR, and decompress it.

NO TE

A user needs to apply for enabling the service before using it for the first time. For detailsabout how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based AuthenticationThis section uses Passport OCR as an example to describe how to use SDK inAK/SK-based authentication mode.

1. Obtain AK/SK. For details, see Authentication > AK/SK-basedAuthentication.

2. Configure AK/SK of the iOS SDK.Change the values of AK and SK of the akskOcrService function in theViewController.m file of the demo project to the obtained AK/SK.

Figure 5-1 Modifying parameters in the ViewController.m file

3. If you use a local image file for recognition, modify the image parameter inthe akskOcrService function of the ViewController.m file. If you use thedefault image of the SDK, you do not need to modify the image path.

4. Execute the akskOcrService function. If 200 is displayed on the console, theprogram is successfully executed. You can view the of Passport OCR on theconsole.

Optical Character RecognitionSDK Reference 5 Using iOS SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 14

Page 18: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

{ "result": { "country_code": "IND", "surname": "xxxx", "given_name": "ASHISH", "passport_number": "G008xxxx", "date_of_birth": "1988-xx-xx", "sex": "M", "date_of_expiry": "2017-11-25", "machine_code": "P<INDKUMAR<<ASHISH<<<<<<<<<<<<<<<<<<<<<<<<<", "machine_code2": "G0080008<2IND8808188M1711257<<<<<<<<<<<<<<<8", "extra_info": {}, "confidence": { "country_code": 0.9684, "surname": 0.9684, "given_name": 0.9684, "passport_number": 0.9276, "date_of_birth": 0.9276, "sex": 0.9276, "date_of_expiry": 0.9276, "machine_code": 0.9684, "machine_code2": 0.9276 } }}

NO TE

To call other OCR services, change the value of parameter uri of the akskOcrServicefunction to the specific URI. For the URI list, see Mappings Between Services andAPIs.

Token-based Authentication

This section uses Passport OCR as an example to describe how to use SDK intoken-based authentication mode.

1. Open the ViewController.m file. Change the values of username, password,and domainName in the tokenOcrService function to the actual username,password, and domain name registered with the system. If the user is not anIAM user, the domain name and username are the same.

Figure 5-2 Changing the username, password, and domain name in theViewController.m file

2. Execute the code. The recognition result of Passport OCR is displayed.

Status Code

For details about status code, see Status Code.

Optical Character RecognitionSDK Reference 5 Using iOS SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 15

Page 19: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Error CodeFor details about error code, see Error Code.

Optical Character RecognitionSDK Reference 5 Using iOS SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 16

Page 20: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

6 Using Android SDK

Users can use SDK in token- or AK/SK-based authentication mode. For detailsabout how to obtain authentication messages, see Authentication. This sectiondescribes how to use SDK in token- and AK/SK-based modes.

Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/sdk?OCR, and decompress it.

NO TE

A user needs to apply for enabling the service before using it for the first time. For detailsabout how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based AuthenticationThis section uses Passport OCR as an example to describe how to use SDK inAK/SK-based authentication mode.

1. Obtain AK/SK. For details, see Authentication > AK/SK-basedAuthentication.

2. Configure AK/SK of the Android SDK.Change the values of AK and SK of the akskOcrService function in theMainActivity.java file of the demo project to the obtained AK/SK.

Figure 6-1 Modifying parameters in the MainActivity.java file

3. Modify parameters.If you use a local image file for recognition, modify the Bitmap parameter inthe akskOcrService function of the MainActivity.java file. If you use thedefault image of the SDK, you do not need to modify the image path.

Optical Character RecognitionSDK Reference 6 Using Android SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 17

Page 21: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

4. Execute the akskOcrService function. If 200 is displayed on the console, theprogram is successfully executed. You can view the recognition result on theconsole.{ "result": { "country_code": "IND", "surname": "xxxx", "given_name": "ASHISH", "passport_number": "G008xxxx", "date_of_birth": "1988-xx-xx", "sex": "M", "date_of_expiry": "2017-11-25", "machine_code": "P<INDKUMAR<<ASHISH<<<<<<<<<<<<<<<<<<<<<<<<<", "machine_code2": "G0080008<2IND8808188M1711257<<<<<<<<<<<<<<<8", "extra_info": {}, "confidence": { "country_code": 0.9684, "surname": 0.9684, "given_name": 0.9684, "passport_number": 0.9276, "date_of_birth": 0.9276, "sex": 0.9276, "date_of_expiry": 0.9276, "machine_code": 0.9684, "machine_code2": 0.9276 } }}

NO TE

To call other OCR services, change the value of parameter uri of the akskOcrServicefunction to the specific URI. For the URI list, see Mappings Between Services andAPIs.

Token-based AuthenticationThis section uses Passport OCR as an example to describe how to use SDK intoken-based authentication mode.

1. Open the MainActivity.java file. Change the values of userName, password,and domainName in the tokenOcrService function to the actual username,password, and domain name registered with the system. If the user is not anIAM user, the domain name and username are the same.

Figure 6-2 Changing the username, password, and domain name in theMainActivity.java file

2. Execute the code. The recognition result of Passport OCR is displayed.

Optical Character RecognitionSDK Reference 6 Using Android SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 18

Page 22: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Status CodeFor details about status code, see Status Code.

Error CodeFor details about error code, see Error Code.

Optical Character RecognitionSDK Reference 6 Using Android SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 19

Page 23: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

7 Using Node.js SDK

Users can use SDK in token- or AK/SK-based authentication mode. For detailsabout how to obtain authentication messages, see Authentication. This sectiondescribes how to use SDK in token- and AK/SK-based modes.

Download OCR Java SDK from https://developer.huaweicloud.com/intl/en-us/sdk?OCR, and decompress it.

NO TE

A user needs to apply for enabling the service before using it for the first time. For detailsabout how to enable the service, click Subscribe. You only need to subscribe to OCR once.

AK/SK-based Authentication

This section uses Passport OCR as an example to describe how to use SDK inAK/SK-based authentication mode.

1. Obtain AK/SK. For details, see Authentication > AK/SK-basedAuthentication.

2. Configure AK/SK of the Node.js SDK.Change the values of appKey and appSecret in the demo.js file of the demoproject to the obtained AK/SK.

Figure 7-1 Modifying parameters in the demo.js file

3. If you use a local image file for recognition, change the image path indemo.js to the path of the file. If you use the default image of the SDK, youdo not need to modify the image path.

Optical Character RecognitionSDK Reference 7 Using Node.js SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 20

Page 24: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

4. Execute the demo.js file. If 200 is displayed on the console, the program issuccessfully executed. You can view the recognition result of Passport OCR onthe console.{ "result": { "country_code": "IND", "surname": "xxxx", "given_name": "ASHISH", "passport_number": "G008xxxx", "date_of_birth": "1988-xx-xx", "sex": "M", "date_of_expiry": "2017-11-25", "machine_code": "P<INDKUMAR<<ASHISH<<<<<<<<<<<<<<<<<<<<<<<<<", "machine_code2": "G0080008<2IND8808188M1711257<<<<<<<<<<<<<<<8", "extra_info": {}, "confidence": { "country_code": 0.9684, "surname": 0.9684, "given_name": 0.9684, "passport_number": 0.9276, "date_of_birth": 0.9276, "sex": 0.9276, "date_of_expiry": 0.9276, "machine_code": 0.9684, "machine_code2": 0.9276 } }}

NO TE

To call other OCR services, change the first parameter of therequestOcrServiceBase64 function to the specific URI. For the URI list, see MappingsBetween Services and APIs.

Token-based AuthenticationThis section uses Passport OCR as an example to describe how to use SDK intoken-based authentication mode.

1. Open the demo.js file. Change the values of username, password, anddomainName in the main function to the actual username, password, anddomain name registered with the system. If the user is not an IAM user, thedomain name and username are the same.

Figure 7-2 Changing the username, password, and domain name in thedemo.js file

2. Execute the code. The recognition result of Passport OCR is displayed.

Status CodeFor details about status code, see Status Code.

Optical Character RecognitionSDK Reference 7 Using Node.js SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 21

Page 25: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

Error CodeFor details about error code, see Error Code.

Optical Character RecognitionSDK Reference 7 Using Node.js SDK

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 22

Page 26: SDK Reference - Huawei · Installing Node.js library dependency OCR Node.js SDK depends on the third-party request, moment, and moment-timezone library packages. Procedure The following

A Change History

Release Date What's New

2020-03-25 This is the first official release.

Optical Character RecognitionSDK Reference A Change History

Issue 01 (2020-11-27) Copyright © Huawei Technologies Co., Ltd. 23