Make A Text Recognition App Using Xamarin.Android

Introduction and History

Today, I shall show you how to create text recognition app using Xamarin.Android. This app is based on Xamarin Google-Play-Services vision for text recognition. Text recognition is the way of identifying the text in pictures and video streams and perceiving the content from there. Once the text is identified, the recognizer at that point decides the real content in each square and fragments it into lines and words.

Text Structure

The text recognizer fragments the content into pieces, lines, and words.
  • A block is a bordering set of text lines; for example, a passage or segment.
  • A line is an adjoining set of words on a similar vertical axis.
  • A word is a contiguous arrangement of alphanumeric characters on a similar vertical axis.


Step 1

Open Visual Studio. Go to New Project-> Templates-> Visual C#-> Android-> Blank app. Select Blank app. Give the project a name, such as TextRecognizer.

 

Step 2

First of all, we need to add a component that is required for text recognition. Open Solution Explorer and go to Components -> Get More Components. In this way, you can move onto Xamarin Components Store and search the Vision app. Click "Add to App", as shown below.



 

Step 3

Open Solution Explorer and go to Project Name-> Resources-> Layout-> Main.axml and add the following code.

The layout will have an ImageView in order to display the preview of the image. I have added a TextView also to display the content of the recognized text.

Main.axml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:padding="15dp">  
  7.     <ImageView  
  8.         android:id="@+id/image_view"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:scaleType="centerInside" />  
  12.     <Button  
  13.         android:id="@+id/btnProcess"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="Proccess" />  
  17.     <TextView  
  18.         android:id="@+id/txtView"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="No Text"  
  22.         android:layout_gravity="center"  
  23.         android:textSize="25sp" />  
  24. </LinearLayout>  

 

Step 4

We need a permission from device. Let's open Solution Explorerand go to Properties-> AndroidManifest and add the following code inside application tags. 
  1. <meta-data android:name="com.google.android.gms.vision.DEPENDENCIES"  
  2. android:value="barcode"/>   
Step 5

Open Solution Explorer, go to Project Name-> Resources-> Drawable, and add this image to the drawable folder.
 
Step 6

Open Solution Explorer, go to Project Name-> MainActivity, and add the following code with appropriate namespaces.

Here is the complete code of Main Activity.
  1. using Android.App;  
  2. using Android.Widget;  
  3. using Android.OS;  
  4. using Android.Graphics;  
  5. using Android.Gms.Vision.Texts;  
  6. using Android.Util;  
  7. using Android.Gms.Vision;  
  8. using System.Text;  
  9. namespace TextRecognitionApp  
  10. {  
  11.     [Activity(Label = "TextRecognitionApp", MainLauncher = true)]  
  12.     public class MainActivity : Activity  
  13.     {  
  14.         private ImageView imageview;  
  15.         private Button btnProcess;  
  16.         private TextView txtView;  
  17.         protected override void OnCreate(Bundle savedInstanceState)  
  18.         {  
  19.             base.OnCreate(savedInstanceState);  
  20.             // Set our view from the "main" layout resource  
  21.             SetContentView(Resource.Layout.Main);  
  22.             imageview = FindViewById<ImageView>(Resource.Id.image_view);  
  23.             btnProcess = FindViewById<Button>(Resource.Id.btnProcess);  
  24.             txtView = FindViewById<TextView>(Resource.Id.txtView);  
  25.             Bitmap bitmap = BitmapFactory.DecodeResource(ApplicationContext.Resources, Resource.Drawable.csharpcorner);  
  26.             imageview.SetImageBitmap(bitmap);  
  27.             btnProcess.Click += delegate  
  28.              {  
  29.                  TextRecognizer txtRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();  
  30.                  if (!txtRecognizer.IsOperational)  
  31.                  {  
  32.                      Log.Error("Error""Detector dependencies are not yet available");  
  33.                  }  
  34.                  else  
  35.                  {  
  36.                      Frame frame = new Frame.Builder().SetBitmap(bitmap).Build();  
  37.                      SparseArray items = txtRecognizer.Detect(frame);  
  38.                      StringBuilder strBuilder = new StringBuilder();  
  39.                      for (int i = 0; i < items.Size(); i++)  
  40.                      {  
  41.                          TextBlock item = (TextBlock)items.ValueAt(i);  
  42.                          strBuilder.Append(item.Value);  
  43.                          strBuilder.Append("/");  
  44.                      }  
  45.                      txtView.Text = strBuilder.ToString();  
  46.                  }  
  47.              };  
  48.         }  
  49.     }  
  50. }  

Output

Running this project, you will have result like this.


Similar Articles