メインコンテンツへスキップ
この例では、銀行口座開設申込書をサーバーにアップロードし、顧客データを抽出する方法を示します。 プロジェクトと関連資料をダウンロードしてください: Unattended.zip

このサンプルコードでは、次の手順を実行します。

  • サービスに接続します
  • セッションを開始します
  • プロジェクトを開きます
  • 新しいバッチを作成します
  • バッチに画像を追加します
  • バッチの処理を開始します
  • 結果を取得し、抽出されたデータを表示します
  • セッションを終了します

この例を進めるには:

  • UnattendedExample.fcproj プロジェクトファイルをサーバーにアップロードします。
  • Visual Studio 2013 以降で UnattendedExample.sln を開きます

サービスに接続する

// Webサービスクライアントのインスタンスを作成する
var service = new FlexiCapture.FlexiCaptureWebServiceSoapClient();
// ======= 基本認証 =======
// service.ClientCredentials.UserName.UserName = "username";
// service.ClientCredentials.UserName.Password = "password";

セッションを開始する

// 不正アクセスからバッチを保護するために、現在のユーザーを指定します
// まず、システムに認識されているユーザー名を取得します
var username = service.GetCurrentUserIdentity();
// 次に、FlexiCaptureのユーザー一覧から自分自身を検索します
var userId = service.FindUser(username.Name);
if (userId <= 0) throw new Exception("Current user not found");
// 新しい処理セッションを開きます
const int roleType = 12; //ユーザーのステーションにおけるOperatorロール
const int stationType = 10; //ユーザーのステーション
var sessionId = service.OpenSession(roleType, stationType);
if (sessionId <= 0) throw new Exception("Couldn't open the session");

プロジェクトを開く

// プロジェクトを取得する
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.");
}
// UnattendedExampleという名前の最初のプロジェクトを開く
var projectId = service.OpenProject(sessionId, projectGuid);
if (projectId <= 0) throw new Exception("Couldn't open the project");

新しいバッチを作成する

// 新しいバッチの名前を指定し、他のプロパティはそのままにする
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");

バッチに画像を追加する

// 画像を追加するバッチを開く
service.OpenBatch(sessionId, batchId);

256 KB未満のファイルをアップロードする

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

大きなファイルのアップロード

256 KB を超えるファイル全体を Base64 でアップロードすると、次のような欠点があります。
  • ネットワーク負荷が 33% 増加する
  • 非常に大きなリクエストは、IIS やファイアウォールによってブロックされる可能性がある
  • 接続が切れたりネットワークエラーが発生したりした場合、ファイルを再送信する必要がある
ファイルサービス API 経由でファイルをアップロードする方が、はるかに効率的です。
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();
private static async Task UploadFile(FlexiCapture.FlexiCaptureWebServiceSoapClient service, int sessionId, int projectId,
int batchId, int documentId, string filename)
{
   const int objectType = 0;
   var crc = new Crc32();
    using (var fs = File.OpenRead(filename))
   {
// ファイルを 1 MB ごとに分割してアップロード
      var buffer = new byte[0x100000];
      var readed = fs.Read(buffer, 0, buffer.Length);
      var checksum = crc.Next(buffer, 0, readed);
      if (readed < buffer.Length)
      {
         // 小さいファイルはまとめてアップロード
         var resp = await FileRequest(service, "Save", objectType, sessionId, projectId, batchId, 0, documentId, 0, filename, 0,
         new ByteArrayContent(buffer, 0, readed));
         if (resp.StatusCode != HttpStatusCode.OK) throw new Exception("Server error");
      }
      else
      {
         var offset = 0;
         while (readed > 0)
         {
            var action = offset == 0 ? "BeginSaveChunked" : "Append";
            await FileRequest(service, action, objectType, sessionId, projectId, batchId, 0, documentId, 0, filename,
                            offset, new ByteArrayContent(buffer, 0, readed));
            offset += readed;
            readed = fs.Read(buffer, 0, buffer.Length);
            checksum = crc.Next(buffer, 0, readed);
         }
         var resp = await FileRequest(service, "Commit", objectType, sessionId, projectId, batchId, 0, documentId, 0, filename);
         if (resp.StatusCode != HttpStatusCode.OK) throw new Exception("Server error");
      }
      var response = await FileRequest(service, "Checksum", objectType, sessionId, projectId, batchId, 0, documentId, 0, filename);
      var text = await response.Content.ReadAsStringAsync();
      if (uint.Parse(text, NumberStyles.HexNumber) != checksum)
      {
         throw new Exception("An error occurred when uploading the file");
      }
   }
}
private static async Task<HttpResponseMessage> FileRequest(FlexiCapture.FlexiCaptureWebServiceSoapClient service, string action,
            int objectType, int sessionId, int projectId, int batchId, int parentId, int objectId, int version, string streamName, int offset, HttpContent file)
{
   var creds = CredentialCache.DefaultNetworkCredentials;
   if (!string.IsNullOrEmpty(service.ClientCredentials.UserName.UserName))
                creds = new NetworkCredential(service.ClientCredentials.UserName.UserName, service.ClientCredentials.UserName.Password);
   using (var handler = new HttpClientHandler { Credentials = creds })
   using (var client = new HttpClient(handler))
   {
      var uri = service.Endpoint.Address.Uri;
      var content = new MultipartFormDataContent
      {
         {new StringContent(action), "Action"},
         {new StringContent(objectType.ToString()), "objectType"},
         {new StringContent(sessionId.ToString()), "sessionId"},
         {new StringContent(projectId.ToString()), "projectId"},
         {new StringContent(batchId.ToString()), "batchId"},
         {new StringContent(parentId.ToString()), "parentId"},
         {new StringContent(objectId.ToString()), "objectId"},
         {new StringContent(version.ToString()), "version"},
         {new StringContent(Convert.ToBase64String(Encoding.Unicode.GetBytes(streamName))), "streamName"},
      };
      if (offset > 0)
      {
         content.Add(new StringContent(offset.ToString()), "offset");
      }
      content.Add(file, "blob", "data.txt");
      return await client.PostAsync(uri.OriginalString.Replace("/API/v1/Soap", "/FileService/v1"), content);
   }
}
private static async Task<HttpResponseMessage> FileRequest(FlexiCapture.FlexiCaptureWebServiceSoapClient service, string action,
   int objectType, int sessionId, int projectId, int batchId, int parentId, int objectId, int version, string streamName)
{
   var creds = CredentialCache.DefaultNetworkCredentials;
   if (!string.IsNullOrEmpty(service.ClientCredentials.UserName.UserName))
      creds = new NetworkCredential(service.ClientCredentials.UserName.UserName, service.ClientCredentials.UserName.Password);
   using (var handler = new HttpClientHandler { Credentials = creds })
   using (var client = new HttpClient(handler))
   {
      var uri = service.Endpoint.Address.Uri;
      var content = new FormUrlEncodedContent(new Dictionary<string, string>
      {
         {"Action", action},
         {"objectType", objectType.ToString()},
         {"sessionId", sessionId.ToString()},
         {"projectId", projectId.ToString()},
         {"batchId", batchId.ToString()},
         {"parentId", parentId.ToString()},
         {"objectId", objectId.ToString()},
         {"version", version.ToString()},
         {"streamName", streamName},
      });
      return await client.PostAsync(uri.OriginalString.Replace("/API/v1/Soap", "/FileService/v1"), content);
   }
}

バッチ処理の開始と終了

// バッチの処理を開始する
service.ProcessBatch(sessionId, batchId);
Console.WriteLine("recognition task created, waiting ");
// 処理が完了するまで待機する
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...");

結果の取得と抽出データの表示

各ドキュメントで表示されるフィールドは、次の 3 つのみです。
  • 宛名
// 結果を取得する
var documents = service.GetDocuments(sessionId, batchId);
if (documents == null) return;
// XML結果の解析に必要なXML名
var docs = XName.Get("Documents", "https://www.abbyy.com/FlexiCapture/Schemas/Export/FormData.xsd");
var banking = XName.Get("_Banking_eng", "https://www.abbyy.com/FlexiCapture/Schemas/Export/Banking_eng.xsd");
// すべての結果を反復処理して画面に表示する
foreach (var document in documents)
{
   if (document.Id == 0) continue;
   // 認識されたデータを含むXMLファイルを取得する
   var attachedFile = service.LoadDocumentResult(sessionId, batchId, document.Id, "Result.xml");
   if (attachedFile.Bytes == null) continue;
   // 結果をXMLで開く
   var xml = XDocument.Load(new MemoryStream(attachedFile.Bytes));
   var docsElement = xml.Element(docs); // 認識されたデータのコンテナー
   if (docsElement == null) continue;
   var result = docsElement.Element(banking);
    if (result == null) continue;
   // 必要なデータを取得する
   var addressing = result.Element("_Addressing");
   var surname = result.Element("_Last_Name");
   var name = result.Element("_First_Name");
   // データを表示する
   Console.WriteLine("- " +
      (addressing == null ? "" : addressing.Value) + " " +
      (name == null ? "" : name.Value) + " " +
      (surname == null ? "" : surname.Value)
   );
}

セッションの終了

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