Example of automatic processing

This example shows how you can upload bank account applications to the server and capture the customers' data.

Download the project and the accompanying materials: Unattended.zip

The example code performs the following steps:

  • Connects to the service
  • Opens a session
  • Opens the project
  • Creates a new batch
  • Adds images to the batch
  • Starts processing the batch
  • Gets the results and displays the captured data
  • Closes the session

To proceed with this example:

  • Upload the UnattendedExample.fcproj project file to the server.
  • Open UnattendedExample.sln in Visual Studio 2013 or later

Connecting to the service

// Create an instance of the web service client
var service = new FlexiCapture.FlexiCaptureWebServiceSoapClient();
    
// ======= BASIC AUTHENTICATION =======
// service.ClientCredentials.UserName.UserName = "username";
// service.ClientCredentials.UserName.Password = "password";
    

Opening a session

// To secure your batches against unauthorized access, specify the current user
// First, get the name of the user visible to the system
var username = service.GetCurrentUserIdentity();
    
// Next, find yourself among the FlexiCapture users
var userId = service.FindUser(username.Name);
if (userId <= 0) throw new Exception("Current user not found");
    
// Open a new processing session
const int roleType = 12; //The operator role on the user's station
const int stationType = 10; //The user's station
var sessionId = service.OpenSession(roleType, stationType);
if (sessionId <= 0) throw new Exception("Couldn't open the session");
    

Opening the project

// Get the projects
var projects = service.GetProjects();
var projectGuid = "";
if (projects != null && projects.Count > 0)
{
   foreach (var project in projects)
   {
      if (project.Name != "UnattendedExample") continue;
      projectGuid = project.Guid;
       break;
   }
}
if (string.IsNullOrEmpty(projectGuid))
{
   throw new Exception("Can't find the UnattendedExample project. You must upload this project to the server to be able to work with this example.");
}
    
// Open the first project named UnattendedExample
var projectId = service.OpenProject(sessionId, projectGuid);
if (projectId <= 0) throw new Exception("Couldn't open the project");
    

Creating a new batch

// Specify a name for the new batch and keep the other properties unchanged
var batch = new FlexiCapture.Batch { Name = "Sample API Batch" };
var batchId = service.AddNewBatch(sessionId, projectId, batch, userId);
if (batchId <= 0) throw new Exception("Couldn't create the batch");
    

Adding images to the batch

// Open the batch where to add images
service.OpenBatch(sessionId, batchId);
    

Uploading files smaller than 256 KB

service.AddNewImage(sessionId, batchId, new FlexiCapture.File()
{
   Bytes = File.ReadAllBytes(filename),
   Name = filename
});
    
    

Uploading larger files

Uploading files larger than 256 KB in their entirety in base64 has the following drawbacks:

  • Network load increases by 33%
  • Very large requests may be blocked by IIS or by the firewall
  • If connection is lost or there are network errors, you will need to resend the file

Uploading files via the file service API is much more efficient.

var doc = new FlexiCapture.Document { BatchId = batchId };
var file = new FlexiCapture.File { Name = filename };
var documentId = service.AddNewDocument(sessionId, doc, file, false, 0);
UploadFile(service, sessionId, projectId, batchId, documentId, filename).Wait();
    

Uploading a file to the server

Sending a request to the server together with a file body

Sending a request to the server without a file body

Starting and ending batch processing

// Star processing the batch
service.ProcessBatch(sessionId, batchId);
Console.WriteLine("recognition task created, waiting ");
    
// Wait for the processing to complete
var percentCompleted = 0;
while (percentCompleted < 100)
{
   Console.CursorLeft = 0;
   Console.Write(percentCompleted + "%");
   percentCompleted = service.GetBatchPercentCompleted(batchId);
   System.Threading.Thread.Sleep(500);
}
Console.CursorLeft = 0;
Console.WriteLine("complete...");
    

Getting the results and displaying the captured data

From each document, only three fields are displayed:

  • Addressing
  • First Name
  • Last Name
// Get the results
var documents = service.GetDocuments(sessionId, batchId);
if (documents == null) return;
// XML names required for parsing the XML results
var docs = XName.Get("Documents", "http://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd");
var banking = XName.Get("_Banking_eng", "http://www.abbyy.com/FlexiCapture/Schemas/Export/Banking_eng.xsd");
// Iterate all the results and display the on the screen
foreach (var document in documents)
{
   if (document.Id == 0) continue;
   // Get the XML file with the recognized data
   var attachedFile = service.LoadDocumentResult(sessionId, batchId, document.Id, "Result.xml");
   if (attachedFile.Bytes == null) continue;
   // Open the results in XML
   var xml = XDocument.Load(new MemoryStream(attachedFile.Bytes));
   var docsElement = xml.Element(docs); // Container for recognized data
   if (docsElement == null) continue;
   var result = docsElement.Element(banking);
    if (result == null) continue;
   // Get the required data
   var addressing = result.Element("_Addressing");
   var surname = result.Element("_Last_Name");
   var name = result.Element("_First_Name");
   // Display the data
   Console.WriteLine("- " +
      (addressing == null ? "" : addressing.Value) + " " +
      (name == null ? "" : name.Value) + " " +
      (surname == null ? "" : surname.Value)
   );
}
    

Closing the session

service.DeleteBatch(sessionId, batchId);
service.CloseProject(sessionId, projectId);
service.CloseSession(sessionId);
  

13.04.2021 11:12:28


Please leave your feedback about this article