All CategoriesAndroid App Development

How to Perform Optical Character Recognition in Android

What is Optical Character Recognition?

Optical Character Recognition (OCR) is a process of converting the printed text on images into machine-encoded text. Means it can be used for many purposes like recognizing text from images, scanning codes or numbers for particular services.
There are many libraries for performing OCR process like Mobile Vision, Tesseract, etc.
Here we are going to use Tesseract library in our Android application to run the OCR process.
Tesseract is open-source, and Apache licensed library which was created by HP and now it is developed by Google.
It provides enough accuracy to recognize the words and also gives many languages support.
Here we are going to create an application which runs the OCR process on an image which is in drawable folder and then display the detected text on the screen.
Let’s see with the help of an example.

Example with code: 

  • Create an Application OCRExampleAndroid.
  • Add tess-two library as a dependency in build.gradle file.
  • Copy the training data file eng.traineddata under app/assets/tessdata folder by creating it if not create.

build.gradle:

Open the build.gradle file and add the dependency here.

dependencies {
compile 'com.rmtheis:tess-two:5.4.1'
}

Copy Training Data file:


Like this copy the eng.traineddata file under assets / tessdata folder. This is a language data file; there are many files in Tesseract library for other supporting languages.

Add image in the drawable folder:

Here we are going to add a test image to run the OCR process in drawable folder resources. We can also implement the camera feature and give choose option pictures from gallery etc. in our application and then perform the OCR to make the application better.

This is a test image that we will add it in our application and extract the text from this image and display it on the screen.
Now, go ahead and do it programmatically.

Requirements:

activity_main.xml, MainActivity.java.
These are the two files where we can do the whole process to implement the OCR.

activity_main.xml:

Create this XML file in our application to show the UI of the main screen.

Add the code here:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:weightSum="1">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:id="@+id/ImageContainer">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/iv_test_OCRImage"
            android:src="@drawable/test_image"/>
    </RelativeLayout>
    <Button
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:text="Run OCR"
            android:id="@+id/btn_OCR"
            android:layout_gravity="center"
            android:background="@color/colorPrimary"
            android:textColor="#ffffff"
            android:onClick="runOCR"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#e4e3e3"
        android:id="@+id/OCRTextContainer"
        android:layout_marginTop="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OCR Text will appear here"
            android:id="@+id/tv_OCR_Result"
            android:textSize="18dp"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:background="#dedede"
            android:textColor="#a3a3a3" />
    </RelativeLayout>
</LinearLayout>

Here, this file shows an ImageView as a test image where we perform the OCR process on them. Next, is the Button to run the OCR process having method runOCR which is explained later and TextView which shows the result or detected text from the image on the screen.

MainActivity.java:

This is our main activity where we can integrate the OCR process by using method runOCR() and perform other operations like initialize Tesseract API, image, and datapath, etc. And check for the language data file which exists or not using method checkFile(). Also, implement a method copyFiles() which allow copying files to the device
So do all these features in this file.

Initialize:

Bitmap image;
private TessBaseAPI mTess;
String datapath = "";

Add the code in onCreate(..){..}:

image = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
String language = "eng";
datapath = getFilesDir()+ "/tesseract/";
mTess = new TessBaseAPI();
checkFile(new File(datapath + "tessdata/"));
mTess.init(datapath, language);

Here, we can see very well by using BitmapFactory.decodeResource() we can easily initialize the image and decode it from drawable folder resources into a bitmap.
TessBaseAPI object is used to access the Tesseract API.
datapath is initialized by using getFilesDir() and adding Tesseract at the end we will get the absolute path to the directory on the device filesystem.
By using init() method we will initialize the TessBaseAPI object and pass the datapath and language inside this.
Now, it will check for supplied datapath and language data file means the training data is copied or not.

private void checkFile(File dir) {
    if (!dir.exists()&& dir.mkdirs()){
        copyFiles();
    }
    if(dir.exists()) {
        String datafilepath = datapath+ "/tessdata/eng.traineddata";
        File datafile = new File(datafilepath);
        if (!datafile.exists()) {
            copyFiles();
        }
    }
}

Here, if the file does not exist then it will create with mkdirs() and if exists then it will check for exact path.

private void copyFiles() {
    try {
        String filepath = datapath + "/tessdata/eng.traineddata";
        AssetManager assetManager = getAssets();
        InputStream instream = assetManager.open("tessdata/eng.traineddata");
        OutputStream outstream = new FileOutputStream(filepath);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = instream.read(buffer)) != -1) {
            outstream.write(buffer, 0, read);
        }
        outstream.flush();
        outstream.close();
        instream.close();
        File file = new File(filepath);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

This is a method which allows us to copy the files to the device.

Run the OCR process:

public void runOCR(View view){
    String OCRresult = null;
    mTess.setImage(image);
    OCRresult = mTess.getUTF8Text();
    TextView tv_OCR_Result = (TextView) findViewById(R.id.tv_OCR_Result);
    tv_OCR_Result.setText(OCRresult);
}

Here in this method, we can see very well that TessBaseAPI object sets the image using setImage() method and then extract the text using getUTF8Text() method and put it in a string. After that display the result using TextView setText() method.

Download Source Code: GitHub

Output:

 

Conclusion:

This is a simple example of How to perform Optical Character Recognition (OCR) in Android. As I discussed above, we can use Tesseract library to implement this functionality because it is well known for its accuracy. We can easily extract the texts from images etc. It is open source and having support for many language supports. We can customize it more by implementing Camera feature or choose the image from gallery etc. and then perform the OCR process. It will also work same. This is just a simple example to show the feature of Optical Character Recognition and how it works. So, we can create as many examples using this library and make our application better. This feature is very popular and important is Android development.Android Programming Course
Keep visiting www.acadgild.com for more updates on the technical and certification courses.

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Close
Close