LEADTOOLS OCR

フレームを表示

OCR文書内にゾーンを追加/削除して描画する方法を示すプログラムを作成および実行するには、以下の手順を行います。チュートリアルは、迅速かつ簡単にOCRプログラムを作成する方法を提供することを目的としています。

  1. Visual Studioを開始します

  2. メニューからファイル->新規作成->プロジェクトを選択します

  3. 新しいプロジェクトダイアログボックスでは、プロジェクト型の並びで「Visual C#プロジェクト」か「Visual Basicプロジェクト」を選択して、テンプレートリストからVisual Studioバージョンに従い「Windowsアプリケーション」または「Windowsフォームアプリケーション」を選択します

  4. プロジェクト名前フィールドの「OcrTutorial1」としてプロジェクト名を入力します、そして、OKを選択します。必要であれば、プロジェクトの新しい場所を入力するか、ブラウズボタンを用いてディレクトリを選択します、そして、OKを選択します。

  5. 「ソリューションエクスプローラー」ウィンドウでは、「参照」フォルダを右クリックします、そして、コンテキストメニューから「参照の追加...」を選択します。「参照の追加」ダイアログボックスでは、「.NET」タブを選択して、LEADTOOLS for .NET「#LEADTOOLS_INSTALLDIR#\Bin\DotNet4\Win32」フォルダに閲覧して、以下のDLLを選択します:

    Leadtools.dllLeadtools.Drawing.dllLeadtools.Codecs.dllLeadtools.Controls.WinForms.dllLeadtools.Forms.dllLeadtools.Forms.DocumentWriters.dllLeadtools.Forms.Ocr.dllLeadtools.Forms.Ocr.Advantage.dllLeadtools.Codecs.Bmp.dllLeadtools.Codecs.Cmp.dllLeadtools.Codecs.Tif.dllLeadtools.Codecs.Fax.dll

    注意:追加されるLeadtools.Codecs.*.dll参照は、BMP、JPG、CMP、TIFとFAX画像ファイル形式のためです。アプリケーションで必要であるならば、任意の追加のファイルフォーマットコーデックDLLを追加します。

  6. :があるならば、Form1 codeビュー(ソリューションエクスプローラーの右クリックForm1ビューCodeをその後選択します)に切り替えて、任意だったusingあとファイルの始めに行に続くことまたはImportsセクションを追加します

    [C#]

    
                 using Leadtools;
                 using Leadtools.Codecs;
                 using Leadtools.Drawing;
                 using Leadtools.Controls;
                 using Leadtools.Forms;
                 using Leadtools.Forms.Ocr;
                 using Leadtools.Forms.DocumentWriters;
                 
    

    [Visual Basic]

    
                 Imports Leadtools
                 Imports Leadtools.Codecs
                 Imports Leadtools.Drawing
                 Imports Leadtools.Controls
                 Imports Leadtools.Forms
                 Imports Leadtools.Forms.Ocr
                 Imports Leadtools.Forms.DocumentWriters
                 
    
  7. Form1クラスに以下のプライベート変数を追加します:

    [C#]

    
                 private ImageViewer _imageViewer;
                 private IOcrEngine _ocrEngine;
                 private IOcrPage _ocrPage;
                 
    

    [Visual Basic]

    
                 Private _imageViewer As ImageViewer
                 Private _ocrEngine As IOcrEngine
                 Private _ocrPage As IOcrPage
                 
    
  8. Form1OnLoadをオーバーライドして、以下のcodeを追加します:

    [C#]

    
                 protected override void OnLoad(EventArgs e)
                 {
                    // Add the image viewer to the form
                    _imageViewer = new ImageViewer();
                    _imageViewer.Dock = DockStyle.Fill;
                    Controls.Add(_imageViewer);
                    _imageViewer.BringToFront();
                    // Show images using their true size
                    _imageViewer.UseDpi = true;
                    // Add ability to pan/zoom with the mouse, but disable double click size mode support because we will use our own
                    ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode();
                    panZoomMode.DoubleTapSizeMode = ControlSizeMode.None;
                    _imageViewer.InteractiveModes.Add(panZoomMode);
                
                    // Initialize the OCR engine
                    _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false);
                    // Startup the engine
                    _ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime");
                    // Create the OCR page from an image
                    string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif";
                    RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1);
                    _ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose);
                    // Auto-zone this page
                    _ocrPage.AutoZone(null);
                    // Add an extra zone, this is our user defined one
                    OcrZone zone = new OcrZone();
                    zone.Name = "Custom zone";
                    zone.ZoneType = OcrZoneType.Text;
                    zone.Bounds = new LogicalRectangle(10, 10, _ocrPage.Width - 20, 100, LogicalUnit.Pixel);
                    _ocrPage.Zones.Add(zone);
                    // Show this image in the viewer
                    _imageViewer.Image = _ocrPage.GetRasterImage();
                    Text = "Right click on any zone to remove it from the page, double click anywhere to save the result as a PDF file";
                
                    // Hook to the events we will use
                    _imageViewer.PostRender += _imageViewer_PostRender;
                    _imageViewer.MouseDown += _imageViewer_MouseDown;
                    _imageViewer.MouseDoubleClick += _imageViewer_MouseDoubleClick;
                
                    base.OnLoad(e);
                 }
                 
    

    Visual Basic

    
                 Protected Overrides Sub OnLoad(e As EventArgs)
                    ' Add the image viewer to the form
                    _imageViewer = New ImageViewer()
                    _imageViewer.Dock = DockStyle.Fill
                    Controls.Add(_imageViewer)
                    _imageViewer.BringToFront()
                    ' Show images using their true size
                    _imageViewer.UseDpi = True
                    ' Add ability to pan/zoom with the mouse, but disable double click size mode support because we will use our own
                    Dim panZoomMode As New ImageViewerPanZoomInteractiveMode()
                    panZoomMode.DoubleTapSizeMode = ControlSizeMode.None
                    _imageViewer.InteractiveModes.Add(panZoomMode)
                
                    ' Initialize the OCR engine
                    _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False)
                    ' Startup the engine
                    _ocrEngine.Startup(Nothing, Nothing, Nothing, "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime")
                    ' Create the OCR page from an image
                    Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"
                    Dim rasterImage As RasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1)
                    _ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose)
                    ' Auto-zone this page
                    _ocrPage.AutoZone(Nothing)
                    ' Add an extra zone, this is our user defined one
                    Dim zone As New OcrZone()
                    zone.Name = "Custom zone"
                    zone.ZoneType = OcrZoneType.Text
                    zone.Bounds = New LogicalRectangle(10, 10, _ocrPage.Width - 20, 100, LogicalUnit.Pixel)
                    _ocrPage.Zones.Add(zone)
                    ' Show this image in the viewer
                    _imageViewer.Image = _ocrPage.GetRasterImage()
                    Text = "Right click on any zone to remove it from the page, double click anywhere to save the result as a PDF file"
                
                    ' Hook to the events we will use
                    AddHandler _imageViewer.PostRender, AddressOf _imageViewer_PostRender
                    AddHandler _imageViewer.MouseDown, AddressOf _imageViewer_MouseDown
                    AddHandler _imageViewer.MouseDoubleClick, AddressOf _imageViewer_MouseDoubleClick
                
                    MyBase.OnLoad(e)
                 End Sub
                 
    
  9. Form1OnFormClosedをオーバーライドして、以下のcodeを追加します:

    [C#]

    
                 protected override void OnFormClosed(FormClosedEventArgs e)
                 {
                    // Destroy the page
                    _ocrPage.Dispose();
                 
                    // And the engine
                    _ocrEngine.Dispose();
                 
                    base.OnFormClosed(e);
                 }
                 
    

    Visual Basic

    
                 Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
                    ' Destroy the page
                    _ocrPage.Dispose()
                 
                    ' And the engine
                    _ocrEngine.Dispose()
                 
                    MyBase.OnFormClosed(e)
                 End Sub
                 
    
  10. 画像ビューワポストを処理する以下のcodeがイベントを提出すると付け加えます。ゾーンを描画します:

    [C#]

    
                 private void _imageViewer_PostRender(object sender, ImageViewerRenderEventArgs e)
                 {
                    // Draw the zones
                    foreach (OcrZone zone in _ocrPage.Zones)
                    {
                       // Get the zone boundary
                       LogicalRectangle zoneBounds = zone.Bounds;
                       // The zone bounds is a logical rectangle, it may be in units other than pixels. Convert to pixels
                       LeadRect bounds = zoneBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY);
                       // Convert the bound to what we see in the viewer
                       // Note that this demo does not have rotation; otherwise, you need to use the four corner points
                       bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds);
                 
                       // If this is our custom zone, draw its border a red pen, else use a blue pen
                       if(zone.Name == "Custom zone")
                          e.PaintEventArgs.Graphics.DrawRectangle(Pens.Red, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
                       else
                          e.PaintEventArgs.Graphics.DrawRectangle(Pens.Blue, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
                    }
                 }
                 
    

    Visual Basic

    
                 Private Sub _imageViewer_PostRender(sender As Object, e As ImageViewerRenderEventArgs)
                    ' Draw the zones
                    For Each zone As OcrZone In _ocrPage.Zones
                       ' Get the zone boundary
                       Dim zoneBounds As LogicalRectangle = zone.Bounds
                       ' The zone bounds is a logical rectangle, it may be in units other than pixels. Convert to pixels
                       Dim bounds As LeadRect = zoneBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY)
                       ' Convert the bound to what we see in the viewer
                       ' Note that this demo does not have rotation; otherwise, you need to use the four corner points
                       bounds = _imageViewer.ConvertRect(Nothing, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds)
                 
                       ' If this is our custom zone, draw its border a red pen, else use a blue pen
                       If zone.Name = "Custom zone" Then
                          e.PaintEventArgs.Graphics.DrawRectangle(Pens.Red, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1)
                       Else
                          e.PaintEventArgs.Graphics.DrawRectangle(Pens.Blue, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1)
                       End If
                    Next
                 End Sub
                 
    
  11. ビューワマウスダウンイベントを処理するために、以下のcodeを追加します。ゾーンを削除します:

    [C#]

    
                 private void _imageViewer_MouseDown(object sender, MouseEventArgs e)
                 {
                    // Check if this is a right button click
                    if (e.Button != MouseButtons.Right)
                       return;
                 
                    // Convert the point from control to image coordinate
                    LeadPoint point = new LeadPoint(e.X, e.Y);
                    point = _imageViewer.ConvertPoint(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, point);
                 
                    // Use the HitTestZone method to find the zone under the mouse button
                    int zoneIndex = _ocrPage.HitTestZone(new LogicalPoint(point.X, point.Y, LogicalUnit.Pixel));
                    if(zoneIndex != -1)
                    {
                       // Remove this zone
                       _ocrPage.Zones.RemoveAt(zoneIndex);
                       // Re-paint the viewer to show the zones left
                       _imageViewer.Invalidate();
                       _imageViewer.Update();
                       // If no zones are left, show a message
                       if(_ocrPage.Zones.Count == 0)
                          MessageBox.Show(this, "No zones left in the page, saving this document to PDF is disabled now");
                    }
                 }
                 
    

    Visual Basic

    
                 Private Sub _imageViewer_MouseDown(sender As Object, e As MouseEventArgs)
                    ' Check if this is a right button click
                    If e.Button <> MouseButtons.Right Then Return
                 
                    ' Convert the point from control to image coordinate
                    Dim point As New LeadPoint(e.X, e.Y)
                    point = _imageViewer.ConvertPoint(Nothing, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, point)
                 
                    ' Use the HitTestZone method to find the zone under the mouse button
                    Dim zoneIndex As Integer = _ocrPage.HitTestZone(New LogicalPoint(point.X, point.Y, LogicalUnit.Pixel))
                    If zoneIndex <> -1 Then
                       ' Remove this zone
                       _ocrPage.Zones.RemoveAt(zoneIndex)
                       ' Re-paint the viewer to show the zones left
                       _imageViewer.Invalidate()
                       _imageViewer.Update()
                       ' If no zones are left, show a message
                       If _ocrPage.Zones.Count = 0 Then
                          MessageBox.Show(Me, "No zones left in the page, saving this document to PDF is disabled now")
                       End If
                    End If
                 End Sub
                 
    
  12. 最後に、マウスダブルクリックを処理するために、以下のcodeを追加します。ドキュメントを保存します:

    [C#]

    
                 private void _imageViewer_MouseDoubleClick(object sender, MouseEventArgs e)
                 {
                    // Check if we have any zones in the page
                    if (_ocrPage.Zones.Count == 0)
                    {
                       MessageBox.Show(this, "No zones left in the page, saving this document to PDF is disabled");
                       return;
                    }
                 
                    // Yes, recognize
                    string pdfFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.pdf";
                 
                    // Try to delete the file if it exists. Might be open by the external application from previous operation
                    if (System.IO.File.Exists(pdfFileName))
                    {
                       try
                       {
                          System.IO.File.Delete(pdfFileName);
                       }
                       catch
                       {
                          MessageBox.Show(this, "The file is probably opened in an external viewer. Close it and try again");
                          return;
                       }
                    }
                 
                    _ocrPage.Recognize(null);
                 
                    // Create a document
                    using (IOcrDocument ocrDocument = _ocrEngine.DocumentManager.CreateDocument(null, OcrCreateDocumentOptions.AutoDeleteFile))
                    {
                       // Add the page
                       ocrDocument.Pages.Add(_ocrPage);
                       // Save as PDF
                       ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null);
                    }
                 
                    // Show the document
                    System.Diagnostics.Process.Start(pdfFileName);
                 }
                 
    

    Visual Basic

    
                 Private Sub _imageViewer_MouseDoubleClick(sender As Object, e As MouseEventArgs)
                    ' Check if we have any zones in the page
                    If _ocrPage.Zones.Count = 0 Then
                       MessageBox.Show(Me, "No zones left in the page, saving this document to PDF is disabled")
                       Return
                    End If
                 
                    ' Yes, recognize
                    Dim pdfFileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.pdf"
                 
                    ' Try to delete the file if it exists. Might be open by the external application from previous operation
                    If System.IO.File.Exists(pdfFileName) Then
                       Try
                          System.IO.File.Delete(pdfFileName)
                       Catch
                          MessageBox.Show(Me, "The file is probably opened in an external viewer. Close it and try again")
                          Return
                       End Try
                    End If
                 
                    _ocrPage.Recognize(Nothing)
                 
                    ' Create a document
                    Using ocrDocument As IOcrDocument = _ocrEngine.DocumentManager.CreateDocument(Nothing, OcrCreateDocumentOptions.AutoDeleteFile)
                       ' Add the page
                       ocrDocument.Pages.Add(_ocrPage)
                       ' Save as PDF
                       ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, Nothing)
                    End Using
                 
                    ' Show the document
                    System.Diagnostics.Process.Start(pdfFileName)
                 End Sub
                 
    
  13. テスト他のコードサンプルの用途に、このプロジェクトを保存します。

参照

参照

OCRチュートリアル - ページの操作
OCRチュートリアル - ページの認識
OCRチュートリアル - 認識結果の操作
OCRチュートリアル - 検索可能PDFのスキャン
概要
ご使用にあたって(サンプルプログラムへのガイド)
LEADTOOLSは、.NETアセンブリのOCR処理を行います
LEADTOOLS .NET OCRによるプログラミング
OCR認識モジュールの概要
OCRエンジンインスタンスの作成
OCRエンジンの開始と終了
LEADTOOLS OCRによるマルチスレッド
OCRスペル言語辞書
OCR言語の操作
OCRページの操作
OCRゾーンの操作
OCRページを認めること
OCR信用度の報告
LEADTOOLS .NET OCRのOMRを使用して
OCR言語とスペルチェック
エンジンに特有の設定のOCR処理を行います