[Huawei Developers] Freemind R:
More articles like this, you can visit HUAWEI Developer Forum and Medium.​

How to integrate Text Recognition ML Kit ?

How to extract text from image of receipts, business cards, and documents?

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/083/299/2640060000001083299.20200630101506.54806853981082909321001451925005:50510708015910:2800:CA3D2932CE4B32DAF0505C231C2C17CEC8CC3F5632B47109A1624C634A891F6C.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/083/299/2640060000001083299.20200630101506.54806853981082909321001451925005:50510708015910:2800:CA3D2932CE4B32DAF0505C231C2C17CEC8CC3F5632B47109A1624C634A891F6C.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/083/299/2640060000001083299.20200630101505.97636027990724694821276965139788:50510708015910:2800:7115BAE5F40BE0AE5B782B8AC751FBE2BFFF8123BFCBE01802A9DA3FE733CD28.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/083/299/2640060000001083299.20200630101505.97636027990724694821276965139788:50510708015910:2800:7115BAE5F40BE0AE5B782B8AC751FBE2BFFF8123BFCBE01802A9DA3FE733CD28.jpg[/IMG]​

1.Add dependencies in build.gradle file

implementation 'com.huawei.hms:ml-computer-vision-ocr:1.0.3.300' // Import the base SDK.
implementation 'com.huawei.hms:ml-computer-vision-ocr-cn-model:1.0.3.300' // Import the Chinese and English model package.
implementation 'com.huawei.hms:ml-computer-vision-ocr-jk-model:1.0.3.300' // Import the Japanese and Korean model package.
implementation 'com.huawei.hms:ml-computer-vision-ocr-latin-model:1.0.3.300' // Import the Latin-based language model package.

2.In MainActivity.click btn_detect to AndroidCameraApi to capture photo

btn_detect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraActivity = new Intent(MainActivity.this, AndroidCameraApi.class);
startActivity(cameraActivity);
}
});

3.Create an AndroidCameraApi Activity to capture photo and get result with bytearray of image.

convert the bytearray to bitmap and save in sharedpreference pref.

Intent intent = new Intent(AndroidCameraApi.this, MainActivity.class);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
SharedPreferences.Editor editor = pref.edit();
editor.putString("data", temp);
editor.commit();
startActivity(intent);

4.Back to MainActivity.use the bitmap string and convert to bitmap by Base64 decode and set the bitmap in localAnalyzer function

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
String data = pref.getString("data", null);
if (data != null) {
byte[] encodeByte = Base64.decode(data, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
encodeByte.length);
Log.d("Bitmap", bitmap.toString());
imageView.setImageBitmap(bitmap);
localAnalyzer(bitmap);
}

5.Pass bitmap in localAnalyzer(Local text recognition). Create the text analyzer MLTextAnalyzer to recognize text in images.

You can set MLLocalTextSetting to specify languages that can be recognized.

Create an MLFrame object using android.graphics.Bitmap. Support JPG, JPEG, PNG, and BMP .

The success convert text will return in onSuccess callback.

private MLTextAnalyzer analyzer;

private void localAnalyzer(Bitmap bitmaps) {
MLLocalTextSetting setting = new MLLocalTextSetting.Factory()
.setOCRMode(MLLocalTextSetting.OCR_DETECT_MODE)
.setLanguage("en")
.create();
this.analyzer = MLAnalyzerFactory.getInstance()
.getLocalTextAnalyzer(setting);
MLFrame frame = MLFrame.fromBitmap(bitmaps);
Task<MLText> task = this.analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLText>() {
@Override
public void onSuccess(MLText text) {
MainActivity.this.displaySuccess(text);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
MainActivity.this.displayFailure();
}
});
}

6.make use the return MLText and display on your textfield.(Block,line)

private void displaySuccess(MLText mlText) {
String result = "";
List<MLText.Block> blocks = mlText.getBlocks();
for (MLText.Block block : blocks) {
for (MLText.TextLine line : block.getContents()) {
result += line.getStringValue() + "\n";
}
}
this.mTextView.setText(result);
}

7 and also permission in manifest

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

8.Text Recognition from the capture image on the Cloud Result* (more accurate!)

private void remoteAnalyzer(Bitmap bitmaps) {
MLRemoteTextSetting setting =
new MLRemoteTextSetting.Factory()
.setTextDensityScene(MLRemoteTextSetting.OCR_COMPACT_SCENE)
.setLanguageList(new ArrayList<String>() {{
this.add("zh");
this.add("en");
}})
.setBorderType(MLRemoteTextSetting.ARC)
.create();
this.analyzer = MLAnalyzerFactory.getInstance().getRemoteTextAnalyzer(setting);
MLFrame frame = MLFrame.fromBitmap(bitmaps);
Task<MLText> task = this.analyzer.asyncAnalyseFrame(frame);
task.addOnSuccessListener(new OnSuccessListener<MLText>() {
@Override
public void onSuccess(MLText text) {
MainActivity.this.remoteDisplaySuccess(text);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
MainActivity.this.displayFailure();
}
});
}

Reference:

https://developer.huawei.com/consumer/en/doc/HMSCore-Guides-V5/text-recognition-0000001050040053-V5