[Huawei Developers] Basavaraj.navi:
Introduction

In this article, we will learn how to integrate Huawei Document Skew Correction using Huawei HiAI. We will build the Document correction application. Where the application will detect document in the image.

About application:

Usually user Struggles a lot while uploading or filling any form due to document issue. This application helps them to take picture from the camera or from the gallery, it automatically detects document from the image.

Document skew correction is used to improve the document photography process by automatically identifying the document in an image. This actually returns the position of the document in the original image.

Document skew correction also adjusts the shooting angle of the document based on the position information of the document in the original image. This function has excellent performance in scenarios where photos of old photos, paper letters, and drawings are taken for electronic storage.

Features

Document detection: Recognizes documents in images and returns the location information of the documents in the original images.

Document correction: Corrects the document shooting angle based on the document location information in the original images, where areas to be corrected can be customized.

How to integrate Document Skew Correction

Configure the application on the AGC.

Apply for HiAI Engine Library.

Client application development process.

Configure application on the AGC

Follow the steps

Step 1: We need to register as a developer account in AppGallery Connect. If you are already a developer ignore this step.

Step 2: Create an app by referring to Creating a Project and Creating an App in the Project

Step 3: Set the data storage location based on the current location.

Step 4: Generating a Signing Certificate Fingerprint.

Step 5: Configuring the Signing Certificate Fingerprint.

Step 6: Download your agconnect-services.json file, paste it into the app root directory.

Apply for HiAI Engine Library

What is Huawei HiAI?

HiAI is Huawei’s AI computing platform. HUAWEI HiAI is a mobile terminal–oriented artificial intelligence (AI) computing platform that constructs three layers of ecology: service capability openness, application capability openness, and chip capability openness. The three-layer open platform that integrates terminals, chips, and the cloud brings more extraordinary experience for users and developers.

How to apply for HiAI Engine?

Follow the steps

Step 1: Navigate to this URL, choose App Service > Development and click HUAWEI HiAI.

Step 2: Click Apply for HUAWEI HiAI kit.

Step 3: Enter required information like Product name and Package name, click Next button.

Step 4: Verify the application details and click Submit button.

Step 5: Click the Download SDK button to open the SDK list.

Step 6: Unzip downloaded SDK and add into your android project under libs folder.

Step 7: Add jar files dependences into app build.gradle file.

implementation fileTree(include: ['*.aar', '*.jar'], dir: 'libs')
implementation 'com.google.code.gson:gson:2.8.6'
repositories {
flatDir {
dirs 'libs'
}
}

Client application development process

Follow the steps.

Step 1: Create an Android application in the Android studio (Any IDE which is your favorite).

Step 2: Add the App level Gradle dependencies. Choose inside project Android > app > build.gradle.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'

Root level gradle dependencies.

maven { url 'https://developer.huawei.com/repo/' }
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Step 3: Add permission in AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

Step 4: Build application.

1. Initialize Vision base.

private void initVisionBase(){
VisionBase.init(this, new ConnectionCallback() {
[USER=439709]@override[/USER]
public void onServiceConnect() {
mHandler.sendEmptyMessage(MSG_SERIVCE_CONNECTED);
}

[USER=439709]@override[/USER]
public void onServiceDisconnect() {
mHandler.sendEmptyMessage(MSG_SERIVCE_DISCONNECTED);
}
});
}

2. Request Read and write external storage Run time permissions.

private void requestPermissions() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int permission1 = ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permission2 = ActivityCompat.checkSelfPermission(this,
Manifest.permission.CAMERA);
if (permission1 != PackageManager.PERMISSION_GRANTED || permission2 != PackageManager
.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 0x0010);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

3. Create the DocRefine instance

DocRefine docResolution = new DocRefine(this);

4. Create VisionImage instance. And add bitmap image input.

Bitmap bitmap = BitmapFactory.decodeFile(filePath);
VisionImage img = VisionImage.fromBitmap(bitmap);
DocCoordinates docCoordinates = new DocCoordinates();
int result = docResolution.docDetect(img, docCoordinates, null);
ImageResult imgResult = new ImageResult();
int resultCode = docResolution.docRefine(img, docCoordinates, imgResult, null)
Bitmap bmp = imgResult.getBitmap();

Result

Tips and Tricks

Recommended image width and height: 1080 px and 2560 px.

Multi-thread invoking is currently not supported.

The document detection and correction API can only be called by 64-bit apps.

If you are taking Video from a camera or gallery make sure your app has camera and storage permission.

Add the downloaded huawei-hiai-vision-ove-10.0.4.307.aar, huawei-hiai-pdk-1.0.0.aar file to libs folder.

Check dependencies added properly.

Latest HMS Core APK is required.

Min SDK is 21. Otherwise you will get Manifest merge issue.

Conclusion

In this article, We have built an application where that detect the document in the image, and correct the document and it gives a result. we have learnt the following concepts.

1. What is Document skew correction?

2. Feature of Document skew correction.

3. How to integrate Document Skew correction using Huawei HiAI

4. How to Apply Huawei HiAI

5. How to build the application

Reference

Document skew correction

Apply for Huawei HiAI

Happy coding