[Huawei Developers] Basavaraj.navi:
Introduction

In this article, we will learn how to convert image to document such as PPT or pdf format. As for users, it is inefficient to manually organize, edit, or improve the note-taking snapshots at conferences or images of paper documents.

Document converter enables apps to convert document images into electronic documents conveniently, such as PPT files. It can recognize documents and the texts in images, and return the recognized content to the client, which will restore the results into a PPT file.

How to integrate Document Converter

1. Configure the application on the AGC.

2. Apply for HiAI Engine Library.

3. 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'

}

}Copy codeCopy code

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.

Request the runtime permission

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();
}
}

private void convertDocument() {

DocConverter docConverter = new DocConverter(this);//Construct Detector.

VisionImage image = VisionImage.fromBitmap(mBitmap);

VisionTextConfiguration config = new VisionTextConfiguration.Builder()
.setAppType(VisionTextConfiguration.APP_NORMAL)
.setProcessMode(VisionTextConfiguration.MODE_IN)
.setDetectType(TextDetectType.TYPE_TEXT_DETECT_FOCUS_SHOOT)
.setLanguage(VisionTextConfiguration.ENGLISH)
.build();

DocConverterConfiguration docConfig = new DocConverterConfiguration.Builder().build();
docConfig.setTextConfig(config);

MyCallBack cb = new MyCallBack();
int result_code = docConverter.detectSlide(image, cb);
}

class MyCallBack implements SlideCallback {
public void onDocDetect(DocCoordinates coor) {
Log.d("MainActivity", coor.toString());
}

public void onDocRefine(Bitmap bitmap) {
}

public void onSuperResolution(Bitmap bitmap) {
//Set super resolution image to image view
}

public void onTextRecognition(Text text) {
Log.d("MainActivity", text.getValue());
mTxtViewResult.setText(text.getValue());
}

public void onError(int errorCode) {
Log.d("MainActivity", "Error code: "+errorCode);
}
}

Result

Tips and Tricks

An image with a width ranging from 1080 pixels to 2560 pixels is recommended.

Multi-thread invoking is currently not supported.

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

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 learnt what is the document convertor using Huawei HiAI using android and java. We have learnt how to convert the image to pdf. Huawei HiAI gives immense control on the text from image.

Reference

Document convertor

Apply for Huawei HiAI