• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!
  • Fill out your device list and let everyone know which phones you have!    Edit Your Device Inventory

Intermediate: How to Improves the quality of Image using Huawei HiAI Image super-resolution service in Android

Search This thread

muraliameakula

Senior Member
Dec 29, 2020
99
3
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.
2640091000001767951.20210604060428.78948858565844982019043352411094:50520614044239:2800:CA3ECE1D795F1772D781CED7AC9C4160D8873FFB9259ABD19738ED2C743AD04D.png


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.
Code:
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.
Code:
apply plugin: 'com.huawei.agconnect'

4. Add the required permission to the Manifestfile.xml file.
Code:
<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.
2640091000001767951.20210604062032.44846962168289790849844664332632:50520614044239:2800:1954A33E4DC5D8B436B479DDC20B1DF066C8922A57C0B2E83B130C2BDA491262.png


2. Click Apply for HUAWEI HiAI kit.
2640091000001767951.20210604062133.47775884236327509403625943579957:50520614044239:2800:DF0F22CCB0FEF8851C905580E230E343176D4AFFC95C24EC4711C08EAC316293.png

3. Enter required information like product name and Package name, click Next button.
2640091000001767951.20210604062144.14304481587583860963213978982781:50520614044239:2800:A25840C46AE2CF4935ABEDC6999AF58F4C286185952F6212878F958212101DB9.png

4. Verify the application details and click Submit button.
5. Click the Download SDK button to open the SDK list.
2640091000001767951.20210604062222.07548597629349865782346903711327:50520614044239:2800:5A49C09773F64FACF3ECCBDA50560EEC1F55B198E77F8807D7D2F6D14E767947.png

2640091000001767951.20210604062231.93318307811528483734754778110500:50520614044239:2800:E2480B54B21B01CE231AD3C1A1F5EFB40D98033765ADE95366005E9905C9F386.png

6. Unzip downloaded SDK and add into your android project under lib folder.
2640091000001767951.20210604062243.26614552450927463727619971421448:50520614044239:2800:A7B0733AEFE0D7BA1D19A2CD4A83716E8236345657115AD67C9D470B1B2F7496.png

7. Add jar files dependences into app build.gradle file.
Code:
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
Code:
<?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.
Java:
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
2640091000001767951.20210604061511.33263297970528300008312277631763:50520614044239:2800:64974CEC5D60FEC5646D139F7663D28DF9E279B7492693A6423864E0B1E5E0B9.png


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