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 run OCR and extract the recognized text in an Android Java application.
Completion Time 20 minutes
Android Studio Project Download tutorial project (1 MB)
Platform Android (Java)
IDE Android Studio
Development License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project and loading an image by reviewing the Add References and Set a License and Display Images in an Image Viewer tutorials, before working on the Recognize Text from Images With OCR - Android Java tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Display Images in an Image Viewer tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. This project requires the following .JAR and .SO files:

The .JAR files can be found at: <INSTALL_DIR>\Bin\Java

The .SO files can be found at: <INSTALL_DIR>\Bin\Android

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

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. Replace the current code with the XML below.

<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); 
 
        if(!hasPermissions(this, PERMISSIONS)){ 
            ActivityCompat.requestPermissions(this, PERMISSIONS, Storage_Permission_Code); 
        } 
        // Get shared libraries path for APK 
        String sharedLibsPath = ""; 
        if (Build.VERSION.SDK_INT < 9) 
            sharedLibsPath = String.format("%s/lib/", this.getApplicationInfo().dataDir); 
        else 
            sharedLibsPath = this.getApplicationInfo().nativeLibraryDir; 
 
        // Load LEADTOOLS native libraries 
        try 
        { 
            Platform.setLibPath(sharedLibsPath); 
            Platform.loadLibrary(LTLibrary.LEADTOOLS); 
            Platform.loadLibrary(LTLibrary.CODECS); 
            Platform.loadLibrary(LTLibrary.OCR); 
            Platform.loadLibrary(LTLibrary.DOCUMENT_WRITER); 
            Platform.loadLibrary(LTLibrary.IMAGE_PROCESSING_CORE); 
        } 
        catch(Exception ex) { 
            Log.d(TAG,"Failed to load LEADTOOLS Native libraries" ); 
        } 
 
        try 
        { 
            RasterSupport.setLicense(this, getResources().openRawResource(R.raw.leadtools), "Input_Developer_Key_Here"); 
        } 
 
        catch(Exception ex) 
        { 
            Log.d(TAG, "Failed to set LEADTOOLS license"); 
            finish(); 
        } 
 
        if(RasterSupport.getKernelExpired()) 
        { 
            Log.d(TAG, "LEADTOOLS kernel is expired"); 
            finish(); 
        } 
        mViewer = findViewById(R.id.rasterimageviewer); 
        mViewer.setTouchInteractiveMode(new ImageViewerPanZoomInteractiveMode()); 
        codecs = new RasterCodecs(); 
        sb = new StringBuilder(); 
        intent = new Intent(); 
 
       String sdCardRoot = Environment.getExternalStorageDirectory().getPath(); // 
        if (!sdCardRoot.endsWith("/")) 
            sdCardRoot += "/"; 
 
        String resourceDir = sdCardRoot + "MyApp/"; 
        String ocrLanguageFileDir = resourceDir + "OCRRuntime/"; 
        String substitutionFontsDir = resourceDir + "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 an 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(); 
        } 
    } 

Note

View is required as part of the Android platform, and is used to tie in the communication for when the user selects the Run OCR button.

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 on an image. In addition, it showed how to use the RasterImageViewer, RasterImage, OcrEngine and OcrPage classes.

See Also

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

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