Text Recognition for Android using Google Mobile Vision

Prakash Pun
3 min readDec 30, 2017

--

For this week’s write-up we will create a simple Android app that uses Google Mobile Vision API’s for Optical character recognition(OCR). The Mobile Vision Text API gives Android developers a powerful and reliable OCR capability that works with most Android devices.

This is a very powerful API that can also be used for live face detection and face tracking along with bar code scanning capabilities.To know more about this wonderful library, follow the link below

As always, the sample project is available here

Let’s get started by first creating a new project in Android Studio.

  1. Go to File ⇒ New Project. When it prompts you to select the default activity, select Empty Activity and proceed.
  2. Open build.gradle in (Module:app) and add Mobile Vision dependencies like this.

3. To use this library, you may need to update your installed version of Google Repository in SDK tools .Do make sure your version of Google Repository is up to date. It should be at least version 26.

4. Don’t forget to add permissions for accessing CAMERA and also meta-data for using OCR in AndroidManifest.xml file like this

5. Next, we will create a simple layout for our activity that will look like this

6. Understand Text using OCR with Mobile Vision Text API for Android

Inside the onCreate() method of the MainActivity.java, we will call a startCameraSource() method.

So what’s going on in the above code? Here are the important parts.

TextRecognizer: This object processes images and determines what text appears within them. Once it’s initialized, it can be used to detect text in all types of images. Do note that before we start using it to recognize text, we should check that it’s ready. This is done using textRecognizer.isOperational() method.

CameraSource: This is a camera manager pre-configured for Vision processing. Here we will set the resolution to 1280*1024 and turn auto-focus on, because it will help in recognizing smaller text much faster. Also, we set the cameraSource to use rear camera by default.

Detector.Processor<TextBlock> : For TextRecognizer to read text straight from the camera, we have to implement a Detector Processor, which will handle detections as often as they become available.

There are two methods be implemented here. The first, receiveDetections(), will receive TextBlock from the Detector as they become available. The second, release(), can be used to cleanly get rid of resources.

7. Final step

We take the values of TextBlock and create StringBuilder object and add the values to the textView, which will be updated every time there is a text in camera view.

8. Fire up the app

Finally, you can fire up the app and see a live view of text from camera view

And that’s it. We now have a simple OCR app ready with few lines of code!

Till next time :) Happy Learning and Happy New year to all.

If you liked this article make sure to 👏 it below, and follow me on twitter!

If you would like to support me, please consider buying me a cuppa:)

--

--

Prakash Pun

Passionate developer sharing insights, tips, and the latest trends. Turning code into creativity. 💻✨ Let's build something together