Recognize Text from Images with OCR - Android Java

This tutorial shows how to recognize text from an image in an Android Java application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to extract text from an image in an Android Java application.
Completion Time 20 minutes
Android Studio Project Download tutorial project (162 KB)
Platform Android (Java)
IDE Android Studio
Runtime License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License and Android - Get Started with Maven tutorials, before working on the Recognize Text from Images with OCR - Android Java tutorial.

Create the Project and add the Maven artifacts

In Android Studio, create a new Android (Java) project, and add the Artifacts.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Set the Application Layout

In the Project Explorer window, open the activity_main.xml file found in the app/src/main/res/layout directory. Below the RasterImageViewer XML code add a new RUN OCR button.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1.0" 
    android:background="@android:color/black"> 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Select Image From Gallery" 
        android:onClick="onSelectImage"/> 
    <leadtools.controls.RasterImageViewer 
        android:id="@+id/rasterimageviewer" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:layout_weight=".96" 
        android:background="@android:color/white"/> 
    <Button 
        android:id="@+id/button" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Run OCR" 
        android:onClick="ocr"/> 
</LinearLayout> 

Add the Extract Text Code

In MainActivity.java, add the following import statements before the MainActivity class:

Java
import leadtools.ocr.OcrEngine; 
import leadtools.ocr.OcrEngineManager; 
import leadtools.ocr.OcrEngineType; 
import leadtools.ocr.OcrImageSharingMode; 
import leadtools.ocr.OcrPage; 
import leadtools.ocr.OcrPageCharacters; 
import leadtools.ocr.OcrWord; 
import leadtools.ocr.OcrZoneCharacters; 

Add the following member variables to the MainActivity class:

Java
    private OcrEngine ocrEngine; 
    private StringBuilder sb; 

Update the onCreate() function as shown below:

Java
@Override 
 protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
 
     // Load LEADTOOLS native libraries 
     try{ 
         Platform.setLibPath(sharedLibsPath); 
         Platform.loadLibrary(LTLibrary.LEADTOOLS); 
         Platform.loadLibrary(LTLibrary.CODECS); 
         Platform.loadLibrary(LTLibrary.OCR); 
     } 
     catch(Exception ex) { 
         Log.d(TAG,"Failed to load LEADTOOLS Native libraries" ); 
     } 
     sb = new StringBuilder(); 
 
     String demoBaseDir = getFilesDir().toString(); 
        if (!demoBaseDir.endsWith("/")) 
            demoBaseDir += "/"; 
 
        String ocrLanguageFileDir = demoBaseDir + "/OCRRuntime/"; 
        String substitutionFontsDir = demoBaseDir + "/SubstitutionFonts/"; 
 
        if (!Utils.copyOcrRuntimeFiles(this, ocrLanguageFileDir)) { 
            Log.d(TAG, "Failed to copy OCR Language Files"); 
            finish(); 
        } 
        if (!Utils.copyAssetsFiles(this, "substitution_fonts", substitutionFontsDir)) { 
            Log.d(TAG, "Failed to copy Substitution Fonts"); 
            finish(); 
        } 
        try { 
            //Set Substitution Fonts path and startup the OCR Engine 
            RasterDefaults.setResourceDirectory(LEADResourceDirectory.FONTS, substitutionFontsDir); 
            ocrEngine = OcrEngineManager.createEngine(OcrEngineType.LEAD); 
            ocrEngine.startup(null, null, null, ocrLanguageFileDir); 
        } catch (Exception e) { 
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
        } 
 
        if (!ocrEngine.isStarted()) { 
            Toast.makeText(this, "OCR Engine was not started successfully", Toast.LENGTH_LONG).show(); 
        } 
 } 

Create a ocr(View v) function to allow the parsing of files. Add the below code:

Java
public void ocr(View v ) 
{ 
    RasterImage image = mViewer.getImage(); 
 
    if(image != null) 
    { 
        OcrPage ocrPage = ocrEngine.createPage(image, OcrImageSharingMode.AUTO_DISPOSE); 
        ocrPage.recognize(null); 
        OcrPageCharacters ocrpageCharacters = ocrPage.getRecognizedCharacters(); 
 
        for (OcrZoneCharacters ocrzonechar: ocrpageCharacters) 
        { 
            Collection<OcrWord> words = ocrzonechar.getWords(); 
            for (OcrWord word: words) 
            { 
                sb.append(word.getValue()); 
                sb.append(" "); 
            } 
        } 
        Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show(); 
    } 
    else 
    { 
        Toast.makeText(this, "No File is loaded", Toast.LENGTH_LONG).show(); 
    } 
} 

Run the Project

Press Shift + F10 to run the application. Follow the steps below to test the application.

Wrap-up

This tutorial showed how to run OCR recognition on an image.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.