[Huawei Developers] muraliameakula:
Introduction

In this article, we will learn how to implement Huawei HiAI kit using Text Recognition service into android application, this service helps us to extract the data from screen shots and photos.

Now a days everybody lazy to type the content, there are many reasons why we want to integrate this service into our apps. User can capture or pic image from gallery to retrieve the text, so that user can edit the content easily.

UseCase: Using this HiAI kit, user can extract the unreadble image content to make useful, let's start.

Requirements

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

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

3. HiAI SDK.

4. Minimum API Level 23 is required.

5. Required EMUI 9.0.0 and later version devices.

6. 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. Download agconnect-services.json file from AGC and add into app’s root directory.

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

maven {url '<a href="https://developer.huawei.com/repo/" target="_blank">https://developer.huawei.com/repo/'}</a>
classpath 'com.huawei.agconnect:agcp:1.4.1.300'

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

apply plugin: 'com.huawei.agconnect'

5. 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"/>

6. Now, 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.

implementationfileTree(<b><span style="font-size: 10.0pt;font-family: Consolas;">include</span></b>: [<b><span style="font-size: 10.0pt;">'*.aar'</span></b>, <b><span style="font-size: 10.0pt;">'*.jar'</span></b>], <b><span style="font-size: 10.0pt;">dir</span></b>: <b><span style="font-size: 10.0pt;">'libs'</span></b>)
implementation <b><span style="font-size: 10.0pt;font-family: Consolas;">'com.google.code.gson:gson:2.8.6'
</span></b>repositories <b>{
</b>flatDir <b>{
</b>dirs <b><span style="font-size: 10.0pt;line-height: 115.0%;font-family: Consolas;color: green;">'libs'
}
}</span></b><b><span style="font-size: 10.0pt;font-family: Consolas;">
</span></b>

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

Let’s do code

I have created a project on Android studio with empty activity let’s start coding.

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 conversionImage;
private TextView textView;
private TextView contentText;
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);
conversionImage = findViewById(R.id.super_image);
textView = findViewById(R.id.text);
contentText = findViewById(R.id.content_text);
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 <= 1440 && height <= 15210) {
originalImage.setImageBitmap(bitmap);
setTextHiAI();
} else {
Toast.makeText(this, "Image size should be below 1440*15210 pixels", Toast.LENGTH_SHORT).show();
}
}

private void setTextHiAI() {
textView.setText("Extraction Text");
contentText.setVisibility(View.VISIBLE);
TextDetector detector = new TextDetector(this);
VisionImage image = VisionImage.fromBitmap(bitmap);
TextConfiguration config = new TextConfiguration();
config.setEngineType(TextConfiguration.AUTO);
config.setEngineType(TextDetectType.TYPE_TEXT_DETECT_FOCUS_SHOOT_EF);
detector.setTextConfiguration(config);
Text result = new Text();
int statusCode = detector.detect(image, result, null);

if (statusCode != 0) {
Log.e("TAG", "Failed to start engine, try restart app,");
}
if (result.getValue() != null) {
contentText.setText(result.getValue());
Log.d("TAG", result.getValue());
} else {
Log.e("TAG", "Result test value is null!");
}
}

}

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. Screenshots size should be 1440*15210 pixels.

5. Photos recommended size is 720p.

6. Refer this URL for supported Countries/Regions list.

Conclusion

In this article, we have learned how to implement HiAI Text Recognition service in android application to extract the content from screen shots and photos.

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