[Huawei Developers] muraliameakula:
Introduction

In this article, we will learn how to implement Huawei HiAI kit using Image super resolution service into android application, so we can easily convert the high resolution images and can reduce the image quality size automatically.

You can capture a photo or old photo with low resolution and if you want to convert the picture to high resolution automatically, so this service will help us to change.

What is Huawei HiAI Service?

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. Huawei HiAI Engine provides apps with a diversity of AI capabilities using device capabilities. These capabilities are as follows:

Computer Vision (CV) Engine

Computer Vision Engine focuses to sense the ambient environment to determine, recognize, and understand the space. Its capabilities are

· Image recognition

· Facial recognition

· Text recognition

Automatic Speech Recognition (ASR) Engine

Automatic Speech Recognition Engine converts human voice into text to facilitate speech recognition.

Natural Language Understanding (NLU) Engine

Natural Language Understanding Engine works with the ASR engine to enable apps to understand human voice or text to achieve word segmentation and text entity recognition.

Requirements

1. Any operating system (MacOS, Linux and Windows).

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

3. Minimum API Level 23 is required.

4. Required EMUI 9.0.0 and later version devices.

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

How to integrate HMS Dependencies

1. First of all, we need to create an app on AppGallery Connect and add related details about HMS Core to our project. For more information check this link

2. Add the required dependencies to the build.gradle file under root folder.

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

3. Add the App level dependencies to the build.gradle file under app folder.

apply plugin: 'com.huawei.agconnect'

4. Add the required permission to the Manifestfile.xml file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.HARDWARE_TEST.camera.autofocus"/>

5. After adding them, sync your project.

How to apply for HiAI Engine Library

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

2. Click Apply for HUAWEI HiAI kit.

3. Enter required information like product name and Package name, click Next button.

4. Verify the application details and click Submit button.

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

6. Unzip downloaded SDK and add into your android project under lib folder.

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'
}
}

8. After completing this above setup now Sync your gradle file.

Let’s do code

I have created a project with empty activity let’s create UI first.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<LinearLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:text="Original Image"
android:textSize="20sp" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">

<ImageView
android:id="@+id/super_origin"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="30dp"
android:src="@drawable/emptyimage"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.8" />

</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

<LinearLayout
app:layout_constraintTop_toBottomOf="@+id/mainlayout"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintVertical_bias="0.5">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:text="After Resolution Image"
android:textSize="20sp" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">

<ImageView
android:id="@+id/super_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:src="@drawable/emptyimage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="h,4:3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.8" />

</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/btn_album"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:text="PIC From Gallery"
android:textAllCaps="true"
android:textSize="15sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.37" />

</androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

In the MainActivity.java we can create the business logic.

public class MainActivity extends AppCompatActivity {

private boolean isConnection = false;
private int REQUEST_CODE = 101;
private int REQUEST_PHOTO = 100;
private Bitmap bitmap;
private Bitmap resultBitmap;

private Button btnImage;
private ImageView originalImage;
private ImageView convertionImage;
private final String[] permission = {
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE};
private ImageSuperResolution resolution;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(permission, REQUEST_CODE);
initHiAI();
originalImage = findViewById(R.id.super_origin);
convertionImage = findViewById(R.id.super_image);
btnImage = findViewById(R.id.btn_album);
btnImage.setOnClickListener(v -> {
selectImage();
});

}

private void initHiAI() {
VisionBase.init(this, new ConnectionCallback() {
@Override
public void onServiceConnect() {
isConnection = true;
DeviceCompatibility();
}

@Override
public void onServiceDisconnect() {

}
});

}

private void DeviceCompatibility() {
resolution = new ImageSuperResolution(this);
int support = resolution.getAvailability();
if (support == 0) {
Toast.makeText(this, "Device supports HiAI Image super resolution service", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Device doesn't supports HiAI Image super resolution service", Toast.LENGTH_SHORT).show();
}
}

public void selectImage() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_PHOTO);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (data != null && requestCode == REQUEST_PHOTO) {
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), data.getData());
setBitmap();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

private void setBitmap() {
int height = bitmap.getHeight();
int width = bitmap.getWidth();
if (width <= 800 && height <= 600) {
originalImage.setImageBitmap(bitmap);
setHiAI();
} else {
Toast.makeText(this, "Image size should be below 800*600 pixels", Toast.LENGTH_SHORT).show();
}
}

private void setHiAI() {
VisionImage image = VisionImage.fromBitmap(bitmap);
SISRConfiguration paras = new SISRConfiguration
.Builder()
.setProcessMode(VisionConfiguration.MODE_OUT)
.build();
paras.setScale(SISRConfiguration.SISR_SCALE_3X);
paras.setQuality(SISRConfiguration.SISR_QUALITY_HIGH);
resolution.setSuperResolutionConfiguration(paras);
ImageResult result = new ImageResult();
int resultCode = resolution.doSuperResolution(image, result, null);
if (resultCode == 700) {
Log.d("TAG", "Wait for result.");
return;
} else if (resultCode != 0) {
Log.e("TAG", "Failed to run super-resolution, return : " + resultCode);
return;
}
if (result == null) {
Log.e("TAG", "Result is null!");
return;
}
if (result.getBitmap() == null) {
Log.e("TAG", "Result bitmap is null!");
return;
} else {
resultBitmap = result.getBitmap();
convertionImage.setImageBitmap(resultBitmap);
}
}
}

Demo

Tips and Tricks

1. Download latest Huawei HiAI SDK.

2. Set minSDK version to 23 or later.

3. Do not forget to add jar files into gradle file.

4. Image size should be must 800*600 pixels.

5. Refer this URL for supported Devices list.

Conclusion

In this article, we have learned how to convert low resolution images into high resolution pictures and to compress the actual image size. In this example we converted low quality image to 3x super resolution image.

Thanks for reading! If you enjoyed this story, please click the Like button and Follow. Feel free to leave a Comment 💬 below.

Reference

Huawei HiAI Kit URL

Original Source