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 image.
Android Mobile Vision API
The Mobile Vision API provides a framework for finding objects in photos and video. 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 text from images, but also for structuring the text retrieved. It will divide the captured text in 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 line takes place.
Coding Part
Steps
I have split this part into 3 steps as in the following.
Step 1
Creating 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 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.
-
- bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ocr_sample);
-
- TextRecognizer txtRecognizer = new TextRecognizer.Builder(getApplicationContext()).build();
- if (!txtRecognizer.isOperational())
- {
-
- txtView.setText("Detector dependencies are not yet available");
- }
- else
- {
-
- 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("/");
-
- 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()) {
-
- Log.v("lines", line.getValue());
- for (Text element : line.getComponents()) {
-
- 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.