OCR an Image

Show in webframe

Take the following steps to create an ASP.NET AJAX-Enabled Web Application to OCR an image. The pre-requisite for this tutorial is that you have LEADTOOLS WCF OcrService hosted.

For more information on hosting the WCF services see: How to Host LEADTOOLS Services on IIS 7 or How to Host LEADTOOLS Services on IIS 5 and 6.

  1. Choose File->New->Project... from the menu.
  2. In the New Project dialog box, choose either "Visual C# Projects" or "Visual Basic Projects" in the Projects Type List, and choose "ASP.NET Web Application" in the Templates List.
  3. Type the project name as "OcrServiceExample" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.
  4. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Service Reference" from the context menu. In the "Add Reference" dialog box, enter the URI for the OcrService.svc in the "Service Uri" field, and enter "OcrService" for the "Namespace" field.
  5. Double-click on Default.aspx in the Solution Explorer. Add a "ScriptManager" and "Update Panel" control from the toolbox.
  6. Select the "Button" control from the toolbox and place it inside the UpdatePanel.
  7. Select the "Label" control from the toolbox and place it inside the UpdatePanel.
  8. Right-click on "Default.aspx" and select "View Code". Add the following lines to the beginning of the file:

    [Visual Basic]

    
                Imports OcrServiceExample.OcrService
                
    

    [C#]

    
                using OcrServiceExample.OcrService;
                
    
  9. Switch to Design view and double click on the Button to generate an event handler. Copy and paste the code below in the Button1_Click event.

    [Visual Basic]

    
                  Dim loadData As FileBinaryData = New FileBinaryData()
                  loadData.FileName = "C:\Users\Public\Documents\LEADTOOLS Images\OCR1.tif"
                  
                  Dim saveData As RawBinaryData = New RawBinaryData()
                  
                  ' Set the converstion options.
                  Dim convertOptions As DocumentConvertOptions = New DocumentConvertOptions()
                  convertOptions.Format = OcrDocumentFormatType.TextAnsi
                  convertOptions.Source = loadData
                  convertOptions.Destination = saveData
                  
                  Dim request As RecognizeRequest = New RecognizeRequest()
                  request.ConvertOptions = convertOptions
                  
                  Dim svc As OcrService.OcrServiceClient = New OcrService.OcrServiceClient()
                  Try
                     Dim response As RecognizeResponse = svc.Recognize(request)
                     saveData = CType(IIf(TypeOf response.Destination Is RawBinaryData, response.Destination, Nothing), RawBinaryData)
                  Finally
                     CType(svc, IDisposable).Dispose()
                  End Try
                  
                  Label1.Text = "<pre>" & System.Text.Encoding.ASCII.GetString(save.Data) & "</pre>"
                
    

    [C#]

    
                  FileBinaryData loadData = new FileBinaryData();
                  loadData.FileName = @"C:\Users\Public\Documents\LEADTOOLS Images\OCR1.tif";
                  
                  RawBinaryData saveData = new RawBinaryData();
                  
                  // Set the converstion options.
                  DocumentConvertOptions convertOptions = new DocumentConvertOptions();
                  convertOptions.Format = OcrDocumentFormatType.TextAnsi;
                  convertOptions.Source = loadData;
                  convertOptions.Destination = saveData;
                  
                  RecognizeRequest request = new RecognizeRequest();
                  request.ConvertOptions = convertOptions;
                  
                  using (OcrService.OcrServiceClient svc = new OcrService.OcrServiceClient())
                  {
                     RecognizeResponse response = svc.Recognize(request);
                     saveData = response.Destination as RawBinaryData;
                  }
                  
                  Label1.Text = "<pre>" + System.Text.Encoding.ASCII.GetString(saveData.Data) + "</pre>";
                
    
  10. Build, and Run the web application to test it.

Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.