LEADTOOLS OCR(Leadtools.Forms.Ocrアセンブリ)

フレームを表示

GetTextメソッド








ゾーンのゼロから始まるインデックス。すぐにすべてのページゾーンのためにテキストを取得するために、このパラメーターのために-1を渡します。
stringとしてこのIOcrPageでゾーンのために認識OCRデータを取得します。
構文
string GetText( 
   int zoneIndex
)
'Declaration
 
Function GetText( _
   ByVal zoneIndex As Integer _
) As String
'Usage
 
Dim instance As IOcrPage
Dim zoneIndex As Integer
Dim value As String
 
value = instance.GetText(zoneIndex)
string GetText( 
   int zoneIndex
)
- (nullable NSString *)textForZoneAtIndex:(NSInteger)index error:(NSError **)error
public String getText(int zoneIndex)
function Leadtools.Forms.Ocr.IOcrPage.GetText( 
   zoneIndex 
)
String^ GetText( 
   int zoneIndex
) 

パラメーター

動物新インデックス
ゾーンのゼロから始まるインデックス。すぐにすべてのページゾーンのためにテキストを取得するために、このパラメーターのために-1を渡します。

戻り値

見つけられる(または、ページ封じ込めの上で認識データをゾーンに分けないならば空の文字列)認識された文字を格納しているString
解説

単純なStringのドキュメント結果オブジェクトを取得するために、このメソッドを使います。テキストとして結果を取得することは、フォーム処理のために手動でゾーンを追加するとき、状況で役に立ちます。たとえば、処理しているフォームが座標100、200、400、220で利益、座標100、100の名前欄、400、120と社会保障番号の2つの領域を持っているとします。以下のようにアプリケーションを構築することができます:

  1. IOcrEngine.CreatePageを用いてフォームimageから新しいIOcrPageオブジェクトを作成します。

  2. 手動で名前ゾーンを追加します:

    
                 OcrZone nameZone = new OcrZone();
                 nameZone.ZoneType = OcrZoneType.Text;
                 nameZone.Bounds = new LogicalRectangle(100, 100, 400, 120);
                 ocrPage.Zones.Add(nameZone);
                 
    
  3. ページ(意志が認めたこの1つのゾーンだけ)を認めます:

    
                 ocrPage.Recognize();
                 
    
  4. 名前欄の値を取得します:

    
                 string name = ocrPage.GetText(0);
                 
    
  5. ページから名前ゾーンを削除します:

    
                 ocrPage.Zones.Clear();
                 
    
  6. 社会保障フィールドを取得するために、上の(2)から、手順を繰り返します。

サンプル

このサンプルは、ドキュメントから専門分野の値を取得します。

Copy Code  
Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.Forms.Ocr
Imports Leadtools.Forms
Imports Leadtools.WinForms
Imports Leadtools.Drawing

<TestMethod>
Public Sub RecognizeTextExample()
   ' Get the form file name
   Console.WriteLine("Setting up the form...")
   Dim formFileName As String = GetMyForm()
   ' Assume we get the field informations from an external source such as a database or an XML file
   Dim fieldNames As String() = {"Name", "Address", "SSN"}

   Dim fieldBounds As LogicalRectangle() = { _
      New LogicalRectangle(800, 160, 1500, 220, LogicalUnit.Pixel), _
      New LogicalRectangle(800, 560, 1500, 220, LogicalUnit.Pixel), _
      New LogicalRectangle(800, 960, 1500, 220, LogicalUnit.Pixel)}

   Dim fieldCount As Integer = fieldNames.Length
   Dim fieldValues As String() = New String(fieldCount - 1) {}

   ' Create an instance of the engine
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)
      ' Start the engine using default parameters
      Console.WriteLine("Starting up the engine...")
      ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrAdvantageRuntimeDir)

      ' Create a page from the image file
      Console.WriteLine("Creating a page...")
      Using ocrPage As IOcrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(formFileName, 1), OcrImageSharingMode.AutoDispose)
         ' Get our fields
         For i As Integer = 0 To fieldCount - 1
            ' Clear all the zones in the page
            ocrPage.Zones.Clear()

            ' Add our field zone
            Dim ocrZone As New OcrZone()
            ocrZone.ZoneType = OcrZoneType.Text
            ocrZone.Bounds = fieldBounds(i)
            ocrZone.Name = fieldNames(i)
            ' Optional
            Console.WriteLine("Adding the zone for field {0} to the page...", ocrZone.Name)
            ocrPage.Zones.Add(ocrZone)

            ' Recognize the page. This will only recognize the zone we added
            Console.WriteLine("Recognizing the page...")
            ocrPage.Recognize(Nothing)
            fieldValues(i) = ocrPage.GetText(0)
         Next
      End Using

      ' Shutdown the engine
      ' Note: calling Dispose will also automatically shutdown the engine if it has been started
      Console.WriteLine("Shutting down...")
      ocrEngine.Shutdown()
   End Using

   ' We are done, show the fields
   Console.WriteLine("-------------------------------------")
   Console.WriteLine("Done, values extracted from the form:")
   Console.WriteLine("-------------------------------------")
   For i As Integer = 0 To fieldCount - 1
      Console.WriteLine("{0} : {1}", fieldNames(i), fieldValues(i))
   Next
End Sub

Private Function GetMyForm() As String
   Dim formFileName As String = LEAD_VARS.ImagesDir + "MyForm.tif"

   ' In this example we will create the form every time
   ' This will be a TIF file 11 by 8.5 inches in size containing a name, address and social security fields
   Using image As New RasterImage( _
      RasterMemoryFlags.Conventional, _
      2544, 3294, 24, _
      RasterByteOrder.Bgr, _
      RasterViewPerspective.BottomLeft, _
      Nothing, IntPtr.Zero, 0)

      image.XResolution = 300
      image.YResolution = 300

      ' Draw our fields into this image
      Dim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image)
      Try
         Using g As Graphics = Graphics.FromHdc(hdc)
            g.FillRectangle(Brushes.White, 0, 0, image.ImageWidth - 1, image.ImageHeight - 1)

            Using f As New Font("Times New Roman", 80, FontStyle.Regular)
               Using p As New Pen(Color.Black, 4)
                  Using sf As New StringFormat()
                     sf.LineAlignment = StringAlignment.Center
                     ' Draw the fields

                     ' Name
                     g.DrawString("Name:", f, Brushes.Black, 200, 200)
                     Dim rc As New Rectangle(800, 160, 1500, 220)
                     g.DrawRectangle(p, rc)
                     Dim value As String = "John Doe"
                     g.DrawString(value, f, Brushes.Black, rc, sf)

                     ' Address
                     g.DrawString("Address:", f, Brushes.Black, 200, 600)
                     rc = New Rectangle(800, 560, 1500, 220)
                     g.DrawRectangle(p, rc)
                     value = "1234 Main Street, USA"
                     g.DrawString(value, f, Brushes.Black, rc, sf)

                     ' Social security number
                     g.DrawString("SSN:", f, Brushes.Black, 200, 1000)
                     rc = New Rectangle(800, 960, 1500, 220)
                     g.DrawRectangle(p, rc)
                     value = "123-45-6789"
                     g.DrawString(value, f, Brushes.Black, rc, sf)
                  End Using
               End Using
            End Using
         End Using
      Finally
         RasterImagePainter.DeleteLeadDC(hdc)
      End Try

      ' Save this image to disk
      Using codecs As New RasterCodecs()
         codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1)
      End Using
   End Using

   Return formFileName
End Function

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
Public Const OcrAdvantageRuntimeDir As String = "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"
End Class
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.Ocr;
using Leadtools.Forms;
using Leadtools.WinForms;
using Leadtools.Drawing;

public void RecognizeTextExample()
{
   // Get the form file name
   Console.WriteLine("Setting up the form...");
   string formFileName = GetMyForm();
   // Assume we get the field information from an external source such as a database or an XML file
   string[] fieldNames =
   {
      "Name",
      "Address",
      "SSN"
   };

   LogicalRectangle[] fieldBounds =
   {
      new LogicalRectangle(800, 160, 1500, 220, LogicalUnit.Pixel),
      new LogicalRectangle(800, 560, 1500, 220, LogicalUnit.Pixel),
      new LogicalRectangle(800, 960, 1500, 220, LogicalUnit.Pixel)
   };

   int fieldCount = fieldNames.Length;
   string[] fieldValues = new string[fieldCount];

   // Create an instance of the engine
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false))
   {
      // Start the engine using default parameters
      Console.WriteLine("Starting up the engine...");
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrAdvantageRuntimeDir);

      // Create a page from the image file
      Console.WriteLine("Creating a page...");
      using (IOcrPage ocrPage = ocrEngine.CreatePage(ocrEngine.RasterCodecsInstance.Load(formFileName, 1), OcrImageSharingMode.AutoDispose))
      {
      // Get our fields
      for (int i = 0; i < fieldCount; i++)
      {
         // Clear all the zones in the page
         ocrPage.Zones.Clear();

         // Add our field zone
         OcrZone ocrZone = new OcrZone();
         ocrZone.ZoneType = OcrZoneType.Text;
         ocrZone.Bounds = fieldBounds[i];
         ocrZone.Name = fieldNames[i]; // Optional

         Console.WriteLine("Adding the zone for field {0} to the page...", ocrZone.Name);
         ocrPage.Zones.Add(ocrZone);

         // Recognize the page. This will only recognize the zone we added
         Console.WriteLine("Recognizing the page...");
         ocrPage.Recognize(null);
         fieldValues[i] = ocrPage.GetText(0);
      }
         }

      // Shutdown the engine
      // Note: calling Dispose will also automatically shutdown the engine if it has been started
      Console.WriteLine("Shutting down...");
      ocrEngine.Shutdown();
   }

   // We are done, show the fields
   Console.WriteLine("-------------------------------------");
   Console.WriteLine("Done, values extracted from the form:");
   Console.WriteLine("-------------------------------------");
   for (int i = 0; i < fieldCount; i++)
      Console.WriteLine("{0} : {1}", fieldNames[i], fieldValues[i]);
}

private string GetMyForm()
{
   string formFileName = LEAD_VARS.ImagesDir + "MyForm.tif";

   // In this example we will create the form every time
   // This will be a TIF file 11 by 8.5 inches in size containing a name, address and social security fields
   using (RasterImage image = new RasterImage(
      RasterMemoryFlags.Conventional,
      2544,
      3294,
      24,
      RasterByteOrder.Bgr,
      RasterViewPerspective.BottomLeft,
      null,
      IntPtr.Zero,
      0))
   {
      image.XResolution = 300;
      image.YResolution = 300;

      // Draw our fields into this image
      IntPtr hdc = RasterImagePainter.CreateLeadDC(image);
      try
      {
         using (Graphics g = Graphics.FromHdc(hdc))
         {
            g.FillRectangle(Brushes.White, 0, 0, image.ImageWidth - 1, image.ImageHeight - 1);

            using (Font f = new Font("Times New Roman", 80, FontStyle.Regular))
            {
               using (Pen p = new Pen(Color.Black, 4))
               {
                  using (StringFormat sf = new StringFormat())
                  {
                     sf.LineAlignment = StringAlignment.Center;
                     // Draw the fields

                     // Name
                     g.DrawString("Name:", f, Brushes.Black, 200, 200);
                     Rectangle rc = new Rectangle(800, 160, 1500, 220);
                     g.DrawRectangle(p, rc);
                     string value = "John Doe";
                     g.DrawString(value, f, Brushes.Black, rc, sf);

                     // Address
                     g.DrawString("Address:", f, Brushes.Black, 200, 600);
                     rc = new Rectangle(800, 560, 1500, 220);
                     g.DrawRectangle(p, rc);
                     value = "1234 Main Street, USA";
                     g.DrawString(value, f, Brushes.Black, rc, sf);

                     // Social security number
                     g.DrawString("SSN:", f, Brushes.Black, 200, 1000);
                     rc = new Rectangle(800, 960, 1500, 220);
                     g.DrawRectangle(p, rc);
                     value = "123-45-6789";
                     g.DrawString(value, f, Brushes.Black, rc, sf);
                  }
               }
            }
         }
      }
      finally
      {
         RasterImagePainter.DeleteLeadDC(hdc);
      }

      // Save this image to disk
      using (RasterCodecs codecs = new RasterCodecs())
      {
         codecs.Save(image, formFileName, RasterImageFormat.CcittGroup4, 1);
      }
   }

   return formFileName;
}

static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
public const string OcrAdvantageRuntimeDir = @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime";
}
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Forms.Ocr;
using Leadtools.Forms;

      
public async Task RecognizeTextExample()
{
   // Get the form file name
   Debug.WriteLine("Setting up the form...");
   string formFileName = @"Assets\MyForm.tif";
   // Assume we get the field informations from an external source such as a database or an XML file
   string[] fieldNames =
   {
      "Name",
      "Address",
      "SSN"
   };

   LeadRect[] fieldBounds =
   {
      LeadRectHelper.Create(800, 160, 1500, 220),
      LeadRectHelper.Create(800, 560, 1500, 220),
      LeadRectHelper.Create(800, 960, 1500, 220)
   };

   int fieldCount = fieldNames.Length;
   string[] fieldValues = new string[fieldCount];

   // Create an instance of the engine
   IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);

   // Start the engine using default parameters
   Debug.WriteLine("Starting up the engine...");
   ocrEngine.Startup(null, null, String.Empty, Tools.OcrEnginePath);

   // Create a document
   IOcrDocumentManager ocrDocumentManager = ocrEngine.DocumentManager;
   IOcrDocument ocrDocument = ocrDocumentManager.CreateDocument();

   // Load the form image
   using (RasterCodecs codecs = new RasterCodecs())
   {
      StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(formFileName);
      using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)))
      {
         // Add the form to the document
         Debug.WriteLine("Adding the form to the document...");
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(image, null);

         // Get our fields
         for (int i = 0; i < fieldCount; i++)
         {
            // Clear all the zones in the page
            ocrPage.Zones.Clear();

            // Add our field zone
            OcrZone ocrZone = new OcrZone();
            ocrZone.ZoneType = OcrZoneType.Text;
            ocrZone.Bounds = fieldBounds[i];
            ocrZone.Name = fieldNames[i]; // Optional

            Debug.WriteLine("Adding the zone for field {0} to the page...", ocrZone.Name);
            ocrPage.Zones.Add(ocrZone);

            // Recognize the page. This will only recognize the zone we added
            Debug.WriteLine("Recognizing the page...");
            fieldValues[i] = ocrPage.RecognizeText(null);
         }
      }
   }

   // Shutdown the engine
   Debug.WriteLine("Shutting down...");
   ocrEngine.Shutdown();

   // We are done, show the fields
   Debug.WriteLine("-------------------------------------");
   Debug.WriteLine("Done, values extracted from the form:");
   Debug.WriteLine("-------------------------------------");
   for(int i =0; i < fieldCount; i++)
      Debug.WriteLine("{0} : {1}", fieldNames[i], fieldValues[i]);
}
必要条件

ターゲットプラットホーム

参照

参照

IOcrPageインターフェース
IOcrPageメンバ
IOcrPageCollectionインターフェース
IOcrZoneCollectionインターフェース
Recognizeメソッド
IsRecognizedプロパティ
Unrecognizeメソッド
OcrZone構造体
自動ゾーン
GetRecognizedCharactersメソッド
SetRecognizedCharactersメソッド
OcrCharacter構造体
IOcrPageCharactersインターフェース
IOcrZoneCharactersインターフェース
LEADTOOLS .NET OCRによるプログラミング
OCRページを認めること
OCR信用度の報告

Leadtools.Forms.Ocrは、認識またはDocument Imaging Suiteのライセンスと、解除キーが必要です。詳細は、以下を参照してください。LEADTOOLSツールキット機能