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:
This is not only used to get text from image as well as 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:
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.
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<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="ocr"/>
Step 3Open 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>
// 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 usingtextblockName.getComponents()

