←Select platform

SetRasterImage Method

Summary
Updates the page image data from a RasterImage object.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void SetRasterImage( 
   RasterImage image 
) 
- (BOOL)setRasterImage:(LTRasterImage *)image error:(NSError **)error 
public void setRasterImage(RasterImage image) 
void SetRasterImage(  
   RasterImage^ image 
)  
def SetRasterImage(self,image): 

Parameters

image
A RasterImage object that contains the image data that will be copied into this IOcrPage.

Remarks

Once you add a page to an OCR document, the image data used to create the page is copied and stored inside the engine even if the original object used to create the page was a RasterImage object. To get a RasterImage that represents this page, use GetRasterImage.

The RasterImage object that the GetRasterImage method returns contains a copy of the page image data. Any modifications you make to the RasterImage object will not affect the page. To update the page image data, you must call SetRasterImage. Since the RasterImage object is a copy, you must dispose it by calling its RasterImage.Dispose method once you are done using it.

Once you obtain a RasterImage object for this page, you can use it with other parts of LEADTOOLS, for example, you can set it in the ImageViewer or RasterPictureBox controls for viewing inside your Windows Forms-based application. You can also perform your own image processing on the page as follows:

// Flip the 2nd page added to the OCR document  
// Get the page (index is zero-based)  
IOcrPage page = ocrDocument.Pages[1];  
    
// Get a RasterImage object that represents this page  
Leadtools.RasterImage image = page.GetRasterImage();  
    
// Flip it vertically  
Leadtools.ImageProcessing.FlipCommand flip = new Leadtools.ImageProcessing.FlipCommand();  
flip.Horizontal = false;  
flip.Run(image);  
    
// Set it back into the page  
page.SetRasterImage(image);  
    
// Dispose the image  
image.Dispose(); 

You can also get a RasterImage object representation of a page with the IOcrPageCollection.ExportPage method.

The RasterImage object returned from the GetRasterImage method will contain one page only. To get a multipage RasterImage object containing the image data of all or a specific range of the pages added to the OCR document, you can either create a RasterImage from the first then loop through the pages of the document and add the returned RasterImage objects to the first object with the RasterImage.AddPage method. Or you can use the IOcrPageCollection.ExportPages methods.

Example
C#
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Common; 
using Leadtools.Document.Writer; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
public void OcrPageExample() 
{ 
   string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"); 
   string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf"); 
 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // Create an OCR document 
      using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
      { 
         // Add this image to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(tifFileName, null); 
 
         // Auto-recognize the zones in the page 
         ocrPage.AutoZone(null); 
 
         // Show its information 
         Console.WriteLine("Size: {0} by {1} pixels", ocrPage.Width, ocrPage.Height); 
         Console.WriteLine("Resolution: {0} by {1} dots/inch", ocrPage.DpiX, ocrPage.DpiX); 
         Console.WriteLine("Bits/Pixel: {0}, Bytes/Line: {1}", ocrPage.BitsPerPixel, ocrPage.BytesPerLine); 
 
         byte[] palette = ocrPage.GetPalette(); 
         int paletteEntries; 
         if (palette != null) 
            paletteEntries = palette.Length / 3; 
         else 
            paletteEntries = 0; 
 
         Console.WriteLine("Number of entries in the palette: {0}", paletteEntries); 
         Console.WriteLine("Original format of this page: {0}", ocrPage.OriginalFormat); 
         Console.WriteLine("Has this page been recognized? : {0}", ocrPage.IsRecognized); 
         ShowZonesInfo(ocrPage); 
 
         // Recognize it and save it as PDF 
         ocrPage.Recognize(null); 
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null); 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown(); 
   } 
} 
 
private void ShowZonesInfo(IOcrPage ocrPage) 
{ 
   Console.WriteLine("Zones:"); 
   foreach (OcrZone ocrZone in ocrPage.Zones) 
   { 
      int index = ocrPage.Zones.IndexOf(ocrZone); 
      Console.WriteLine("Zone index: {0}", index); 
      Console.WriteLine("  Id                  {0}", ocrZone.Id); 
      Console.WriteLine("  Bounds              {0}", ocrZone.Bounds); 
      Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType); 
      Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters); 
      Console.WriteLine("----------------------------------"); 
   } 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS23\Bin\Common\OcrLEADRuntime"; 
} 
Requirements

Target Platforms

Help Version 23.0.2024.4.19
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Ocr Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.