[Huawei Developers] Basavaraj.navi:
Introduction

In this article we will learn how to integrate Code Recognition. We will build the contact saving application from QR code using Huawei HiAI.

Code recognition identifies the QR codes and bar codes to obtain the contained information, based on which the service framework is provided.

This API can be used to parse QR codes and bar codes in 11 scenarios including Wi-Fi and SMS, providing effective code detection and result-based service capabilities. This API can be widely used in apps that require code scanning services.

Software requirements

Any operating system (MacOS, Linux and Windows).

Any IDE with Android SDK installed (IntelliJ, Android Studio).

HiAI SDK.

Minimum API Level 23 is required.

Required EMUI 9.0.0 and later version devices.

Required process kirin 990/985/980/970/ 825Full/820Full/810Full/ 720Full/710Full

How to integrate Code Recognition.

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.

Bitmap bitmap;
List<Barcode> codes;
private void initVisionBase() {
VisionBase.init(this, new ConnectionCallback() {
@Override
public void onServiceConnect() {
}

@Override
public void onServiceDisconnect() {

}
});
}

private void saveContact() {
if (codes != null && codes.size() > 0) {
Log.d("New data: ", "" + new Gson().toJson(codes));
String contactInfo = new Gson().toJson(codes.get(0));
ContactInfo info = new Gson().fromJson(contactInfo, ContactInfo.class);
Intent i = new Intent(Intent.ACTION_INSERT);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);
i.putExtra(ContactsContract.Intents.Insert.NAME, info.getContactInfo().getPerson().getName());
i.putExtra(ContactsContract.Intents.Insert.PHONE, info.getContactInfo().getPhones().get(0).getNumber());
i.putExtra(ContactsContract.Intents.Insert.EMAIL, info.getContactInfo().getEmails().get(0).getAddress());
if (Integer.valueOf(Build.VERSION.SDK) > 14)
i.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 +
startActivityForResult(i, PICK_CONTACT_REQUEST);
} else {
Log.e("Data", "No Data");
}
}

class QRCodeAsync extends AsyncTask<Void, Void, List<Barcode>> {

Context context;

public QRCodeAsync(Context context) {
this.context = context;
}

@Override
protected List<Barcode> doInBackground(Void... voids) {
BarcodeDetector mBarcodeDetector = new BarcodeDetector(context);//Construct Detector.
VisionImage image = VisionImage.fromBitmap(bitmap);
ZxingBarcodeConfiguration config = new ZxingBarcodeConfiguration.Builder()
.setProcessMode(VisionTextConfiguration.MODE_IN)
.build();
mBarcodeDetector.setConfiguration(config);
mBarcodeDetector.detect(image, null, new VisionCallback<List<Barcode>>() {
@Override
public void onResult(List<Barcode> barcodes) {
if (barcodes != null && barcodes.size() > 0) {
codes = barcodes;
} else {
Log.e("Data", "No Data");
}
}

@Override
public void onError(int i) {
}

@Override
public void onProcessing(float v) {
}
});
return codes;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
}

}

Result

Tips and Tricks

An error code is returned if the size of an image input to the old API exceeds 20 MP. In this case, rescale the image for improved input efficiency and lower memory usage.

There are no restrictions on the resolution of the image input to the new API. However, an image larger than 224 x 224 in size and less than 20 MP is recommended.

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 contact saving application and parsing the QR code image from gallery.

We have learnt the following concepts.

Introduction of Code Recognition?

How to integrate Code Recognition using Huawei HiAI

How to Apply Huawei HiAI

How to build the application

Reference

Code Recognition

Apply for Huawei HiAI

Happy coding