Using SDK to Call OCR

OCR SDKs encapsulate OCR RESTful APIs to simplify development. Currently, OCR SDKs for Java, Python, iOS, Android, Node.js, PHP, C++, and C# are available.

Use the SDK for Java as an example to describe how to call an OCR API, for example, ID Card OCR. You can use the SDK by only calling API functions.

To call an OCR API using an SDK, do the following:

Step 1: Subscribing to a Service

Step 2: Configuring the Environment

Step 3: Modifying the Configuration

Step 4: Calling the Service

Preparations

You have registered an account with HUAWEI CLOUD and completed identity authentication. Your account cannot be in arrears or frozen.

Step 1: Subscribing to a Service

  1. Log in to the HUAWEI CLOUD OCR console.

    The service is deployed in the CN North-Beijing4 region by default. Select a region based on service requirements. For details about the region where each service is deployed, see Regions and Endpoints.

  2. Select and subscribe to your desired API.

    For this example, subscribe to the ID Card OCR API.

Step 2: Configuring the Environment

  1. Download the OCR SDK for Java.

    Download link: https://developer.huaweicloud.com/sdk?OCR

  2. Prepare a Java development environment.

    For details about how to configure the environment, see Preparing a Java Development Environment.

  3. Import the OCR SDK for Java project.
    1. Open Eclipse, and configure the correct JRE path in Window > Preferences > Java > Installed JREs.
    2. Right-click on the Package Explorer page, click Import, choose General > Existing Projects into Workspace, and select the path of the OCR SDK demo package.
    3. Click Finish to import the demo.
      Figure 1 Demo of OCR SDK for Java

      The OCR SDK calls the service from the CN North-Beijing4 region by default. If you select another region when subscribing to the service, replace all cn-north-4 fields in the configuration file with the actual region when performing step 3. For details about the region where each service is deployed, see Regions and Endpoints.

Step 3: Modifying the Configuration

AK/SK- or token-based authentication can be used for the demo of OCR SDK for Java. This example uses AK/SK-based authentication.

  1. Obtain an AK/SK.

    The AK/SK is the access key. To obtain the AK/SK, log in to the My Credential page and choose Access Keys > Add Access Key.

  2. For AK/SK-based authentication, configure the AK/SK in the OCR SDK for Java.

    Change the values of AK and SK of the function in the OCRDemo.java file of the demo project to the obtained AK/SK.

    Figure 2 Configuring the AK/SK
  3. Modify the OCRDemo.java configuration file in the OCR SDK for Java.
    Figure 3 Modifying the OCRDemo.java file
    Table 1 Parameters in the Main function

    Parameter

    Description

    Value

    AK

    Access Key ID (AK)

    Log in to the My Credential page and choose Access Keys > Add Access Key to obtain it.

    SK

    Secret access key (SK)

    Log in to the My Credential page and choose Access Keys > Add Access Key to obtain it.

    regionName

    Region where the service resides

    cn-north-4

    httpUri

    Uniform resource identifier

    /v1.0/ocr/id-card

    imgPath

    Image file path

    ./data/id-card-demo.jpg

    sideKey

    Optional parameter

    side

    API request parameter of ID Card OCR. The options are as follows:

    • front: front side of the ID card with the profile
    • back: back side of the ID card with the national emblem

    sideValue

    Value of the optional parameter

    The value can be front or back.

Step 4: Calling the Service

Execute the OCRDemo.java file. If status code 200 is displayed, the program is executed. The recognition result of ID Card OCR is returned in JSON format, as shown below.

{
    "result": {
        "name": "xx", 
        "sex": "Gender recognized from the image",
        "ethnicity": "Ethnicity recognized from the image",
        "birth": "1990-xx-xx", 
        "address": "Address recognized from the image",
        "number": "3892011990012xxxxx"
    }
}