Learn ML Kit Document Recognition by Making a Demo Project

Merve Nur TÜRK
Huawei Developers
Published in
3 min readNov 3, 2020

The document recognition service can recognize text with paragraph formats in document images. The SDK calls the on-cloud API to recognize documents only in asynchronous mode. It can extract text from document images. With the SDK, let’s make a demo project and learn how we can use it.

Before API development;

  • When you finish process of creating project, you need to get agconnect-services.json file for configurations from AppGallery Connect. Then, you have to add it into our application project level under the app folder.

Let’s learn document recognotion sdk

This demo project aims to search a keyword at the hard copy document by using device. The project has a main screen(MainActivity.java) that you can input a keyword and click search button. In addition to this, It has a scanner screen(DocumentScannerActivity.java).

  • Let’s define layout file of this activity:
  • By clicking search button, you sould get the text of edittext and send the data to scanner activity(DocumentScannerActivity.java) using intent.putExtra() method:
  • At the scanner activity, you should get the extras:
  • Define a textview on layout resource file to show the result:
  • Set the list of languages to be recognized (for now, sdk support 2 languages):
  • Create a document analyzer. You can create an analyzer using the provided custom document recognition parameter MLDocumentSetting:
  • Create a document analyzer that uses the customized configuration:
  • Request manifest from user to to use the camera and take photos. if user gave the access permission before, open the camera:
  • At request permission result, if user give access permission, open the camera:
  • Open camera with ACTION_IMAGE_CAPTURE using Intent:
  • At onActivityResult override method, get the data with bitmap and send the data to analyzeBitmap() function:
  • On analyzeBitmap function: Pass the MLFrame object to the asyncAnalyseFrame method for document recognition. Set ApiKey to MLApplication (you can find it at the agconnect.json or app gallery connect website), create task and pass the frame:
  • If task will be success, display the whole result text. In addition to this, let’s find if the result includes the keyword or not. If it includes the keyword, change text color. If it does not include the keyword show a message:
  • If task will not be success, display the failure result.

In this article, we made a demo project using HMS ML Kit Document Recognition SDK and learn its usage. Hope to see awesome projects from you. Stay with code :)

--

--