
Introduction
In this tutorial, we will learn how to do Optical Character Recognition in Android using Vision API. Here, we will just import the Google Vision API Library with Android Studio and implement the OCR for retrieving text from the image.
Android Mobile Vision API
The Mobile Vision API provides a framework for finding objects in photos and videos. The framework includes detectors, which locate and describe visual objects in images or video frames, and an event-driven API that tracks the position of those objects in video. The Mobile Vision API includes face, bar code, and text detectors, which can be applied separately or together.
This is not only used to get a text from images, but also for structuring the text retrieved. It will divide the captured text into the following categories.
- TextBlock - In this category, the scanned paragraph is captured.
- Line - In this category, the line of text captured from Textblock takes place.
- Element- In this category, the word captured from the line takes place.
Coding Part
Steps
I have split this part into 3 steps as in the following.
Step 1
Creating a New Project with Empty Activity and Gradle Setup.
Step 2
Setting up Manifest for OCR.
Step 3
Implementing OCR in Application.
Step 1
We will start coding for OCR. Create a New Android Project. Add the following line in your app level build.gradle file to import the library.
For Android Studio before 3.0
compile'com.google.android.gms:play-services-vision:11.8.0'
From Android Studio 3.0
implementation 'com.google.android.gms:play-services-vision:11.8.0'
Step 2
Open your Manifest file and add the following code block to instruct the app to install or download the dependencies at the time of installing the app.
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="ocr"/>
Step 3
Open your activity_main.xml file and paste the following code. It just the designer part of the application.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="15dp">
- <ImageView
- android:id="@+id/image_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:scaleType="centerInside" />
- <Button
- android:id="@+id/btnProcess"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Process" />
- <TextView
- android:id="@+id/txtView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="No Text"
- android:layout_gravity="center"
- android:textSize="25sp" />
- </LinearLayout>
Open your MainActivity.java file and initialize the widget used in your designer. Add the following code to start Optical Character Recognition.
- // To get bitmap from resource folder of the application.
- bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ocr_sample);
- // Starting Text Recognizer
- TextRecognizer txtRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
- if (!txtRecognizer.isOperational())
- {
- // Shows if your Google Play services is not up to date or OCR is not supported for the device
- txtView.setText("Detector dependencies are not yet available");
- }
- else
- {
- // Set the bitmap taken to the frame to perform OCR Operations.
- Frame frame = new Frame.Builder().setBitmap(bitmap).build();
- SparseArray items = txtRecognizer.detect(frame);
- StringBuilder strBuilder = new StringBuilder();
- for (int i = 0; i < items.size(); i++)
- {
- TextBlock item = (TextBlock)items.valueAt(i);
- strBuilder.append(item.getValue());
- strBuilder.append("/");
- // The following Process is used to show how to use lines & elements as well
- for (int i = 0; i < items.size(); i++) {
- TextBlock item = (TextBlock) items.valueAt(i);
- strBuilder.append(item.getValue());
- strBuilder.append("/");
- for (Text line : item.getComponents()) {
- //extract scanned text lines here
- Log.v("lines", line.getValue());
- for (Text element : line.getComponents()) {
- //extract scanned text words here
- Log.v("element", element.getValue());
- }
- }
- }
- }
- txtView.setText(strBuilder.toString());
- }
txtRecognizer.isOperational() is used to check the device has the support for Google Visison API. The output of the TextRecognizer can be retrieved by using SparseArray and StringBuilder.
TextBlock
I have used TextBlock to retrieve the paragraph from the image using OCR.
Lines
You can get the line from the TextBlock using
textblockName.getComponents()
Element
You can get the line from the TextBlock using
lineName.getComponents()
Demo
Download Code

You can download the full source code for this article from GitHub.

IT pediaPosted Apr 30, 2018, 2:30 PM
This OCR run on drawable image my question is how to run OCR on camera taken image?
Sagar Pandurang KapPosted Jan 3, 2018, 12:02 AM
Very interesting...Keeep posting..