Skip to content

Recognize image

Document Reader SDK provides the opportunity to recognize data from an image.

First of all, you need to check image browsing permissions.

Start Intent for getting image:

// creates and starts image browsing intent
// results will be handled in onActivityResult method
private void CreateImageBrowsingRequest()
{
    Intent intent = new Intent();
    intent.SetType("image/*");
    intent.PutExtra(Intent.ExtraAllowMultiple, true);
    intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), REQUEST_BROWSE_PICTURE);
}

Handle image browsing results:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (resultCode == Result.Ok)
    {
        //Image browsing intent processed successfully
        if (requestCode == REQUEST_BROWSE_PICTURE && data.Data != null)
        {
            var selectedImage = data.Data;
            var bmp = GetBitmap(selectedImage, 1920, 1080);

            loadingDialog = showDialog("Processing image");

            DocumentReader.Instance().StopScanner();
            DocumentReader.Instance().RecognizeImage(bmp, this);
        }
    }
}

Handle scan results:

public void OnCompleted(int action, DocumentReaderResults results, string error)
{
    if (action == DocReaderAction.Complete)
    {
        Toast.MakeText(this, "Completed", ToastLength.Long).Show();
    }
    else
    {
        // something happened before all results were ready
        if (action == DocReaderAction.Cancel)
        {
            Toast.MakeText(this, "Scanning was cancelled", ToastLength.Long).Show();
        }
        else if (action == DocReaderAction.Error)
        {
            Toast.MakeText(this, "Error:" + error, ToastLength.Long).Show();
        }
    }
}
void ShowScannerCompleted(RGLDocReaderAction action, RGLDocumentReaderResults result, string error)
{
    switch (action)
    {
        case RGLDocReaderAction.Cancel:
            Console.WriteLine("Cancelled by user");
            break;
        case RGLDocReaderAction.Complete:
            Console.WriteLine("Completed");;
        case RGLDocReaderAction.Error:
            Console.WriteLine("Error string: " + error);
            break;
        case RGLDocReaderAction.Process:
            Console.WriteLine("Scaning not finished. Result: " + result);
            break;
    }
}

void OnFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
{
    var image = e.Info[UIImagePickerController.OriginalImage] as UIImage;
    if (image != null)
    {
        Console.WriteLine("got the original image");

        docReader.RecognizeImage(image, false, ShowScannerCompleted);
    }
    else
    {
        Console.WriteLine("Something went wrong");
    }

    (sender as UIImagePickerController).DismissModalViewController(true);
}