[Huawei Developers] Basavaraj.navi:
Introduction

In this article, we will learn how to integrate Image Super-Resolution using Huawei HiAI. Upscale image, reduce image noise, improves image details without changing the resolution. Share your noise-reduced image or upscale image on social media to get more likes and views.

Let us learn how easy it is to implement the HiAi Capability to manage your images, you can reduce the image noise. 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.

With the resolutions of displays being improved, as well as the wide application of retina screens, users have soaring requirements on the resolution and quality of images. However, due to reasons of network traffic, storage, and image sources, high-resolution images are hard to obtain, and image quality is significantly reduced after JPEG compression.

Features

High speed: This algorithm takes only less than 600 milliseconds to process an image with a maximum resolution of 800 x 600, thanks to the deep neural network and Huawei NPU chipset. This is nearly 50 times faster than pure CPU computing.

High image quality: The deep neural network technology of Huawei's super-resolution solution can intelligently identify and reduce noises in images at the same time, which is applicable to a wider range of real-world applications.

Lightweight size: The ultra-low ROM and RAM usage of this algorithm effectively reduces the app size on Huawei devices, so that you can focus on app function development and innovations.

Restriction on Image size

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 Image super resolution.

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'

}

}Copy 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.

ativity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="8dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="Note: The input picture resolution should not be greater than 1.3 million pixels"
android:textColor="#FF0000"
android:visibility="gone" />

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/tvLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#88FFFFFF" />

<ImageView
android:id="@+id/super_origin"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_below="@id/tvLabel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />

</RelativeLayout>

<!--The size of the target image is 3 times the size of the original image, please refer to the API description for details-->
<ImageView
android:id="@+id/super_image"
android:layout_width="360dp"
android:layout_height="360dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp" />

<Button
android:id="@+id/btnCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="take a photo"
android:visibility="gone" />

<Button
android:id="@+id/btnSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="@drawable/rectangle_shape"
android:text="Select Image"
android:textAllCaps="false"
android:textColor="#fff" />

<Button
android:id="@+id/tst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/rectangle_shape"
android:text="Image Super Resolution"
android:textAllCaps="false"
android:textColor="#fff" />

<Button
android:id="@+id/shareBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/rectangle_shape"
android:text="Share"
android:textAllCaps="false"
android:textColor="#fff" />
</LinearLayout>
</ScrollView>

MainActivity.java

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.huawei.hiai.vision.common.ConnectionCallback;
import com.huawei.hiai.vision.common.VisionBase;
import com.huawei.hiai.vision.common.VisionImage;
import com.huawei.hiai.vision.image.sr.ImageSuperResolution;
import com.huawei.hiai.vision.visionkit.common.VisionConfiguration;
import com.huawei.hiai.vision.visionkit.image.ImageResult;
import com.huawei.hiai.vision.visionkit.image.sr.SISRConfiguration;
import com.huawei.txtimgsr.R;

import java.io.ByteArrayOutputStream;

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 selectImageBtn;
private Button shareBtn;
private ImageView originalImage;
private ImageView convertedImage;
private final String[] permissions = {
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(permissions, REQUEST_CODE);
initVisionBase();
originalImage = findViewById(R.id.super_origin);
shareBtn = findViewById(R.id.shareBtn);
convertedImage = findViewById(R.id.super_image);
selectImageBtn = findViewById(R.id.btnSelect);
selectImageBtn.setOnClickListener(v -> {
selectImageFromGallery();
});

shareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
shareImageToOtherApps();
}
});
}

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

@Override
public void onServiceDisconnect() {

}
});

}

public void shareImageToOtherApps() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/*");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
i.putExtra(Intent.EXTRA_STREAM, getImageUri(this, getBitmapFromView(convertedImage)));
try {
startActivity(Intent.createChooser(i, "My Profile ..."));
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
}

public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}

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 selectImageFromGallery() {
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());
setOriginalImageBitmap();
} catch (Exception e) {
e.printStackTrace();
}
}
}

}

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

private void convertImageResolution() {
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();
convertedImage.setImageBitmap(resultBitmap);
}
}
}

Result

Tips and Tricks

Recommended image should be larger than 720px.

Multiple Image resolution currently not supported.

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 done reducing noise in the image, upscale image, converted low quality image to 3x super resolution image.

We have learnt the following concepts.

1. Introduction of Image Super resolution?

2. How to integrate Image super resolution using Huawei HiAI

3. How to Apply Huawei HiAI.

4. How to build the application.

Reference

Image Super Resolution

Apply for Huawei HiAI

Happy coding