LEADTOOLS OCR (Leadtools.Forms.Ocr assembly)
LEAD Technologies, Inc

AddDib(IntPtr,OcrProgressCallback) Method

Example 





An unmanaged pointer to the DIB (device-independent bitmap).
Optional callback to show operation progress.
Adds a page from a DIB (Device-independent bitmap) stored in unmanaged memory. .NET support WinRT support
Syntax
'Declaration
 
Overloads Function AddDib( _
   ByVal dib As IntPtr, _
   ByVal callback As OcrProgressCallback _
) As IOcrPage
'Usage
 
Dim instance As IOcrPageCollection
Dim dib As IntPtr
Dim callback As OcrProgressCallback
Dim value As IOcrPage
 
value = instance.AddDib(dib, callback)
function Leadtools.Forms.Ocr.IOcrPageCollection.AddDib(IntPtr,OcrProgressCallback)( 
   dib ,
   callback 
)

Parameters

dib
An unmanaged pointer to the DIB (device-independent bitmap).
callback
Optional callback to show operation progress.

Return Value

The IOcrPage object that define the newly added page.
Remarks

This method will add a device independent bitmap (DIB) stored in an unmanaged memory pointer to the OCR document.

This method will add the page to the end of the collection if you already have pages in this IOcrPageCollection.

You can use the OcrProgressCallback to show the operation progress or to abort it. For more information and an example, refer to OcrProgressCallback.

The LEADTOOLS OCR engine supports pages of dots per inch (DPI) values of 150 and greater. If you try to add a page with a DPI of less than 150 then the engine might be able to recognize any data from this page.

Note, the LEADTOOLS Plus OCR engine does not support image size greater than A3 paper size (11.7 by 16.5 inches at 300 dpi). Attempting to add an image that has a size greater than A3 will result in an error. For document of size greater than the maximum allowed, you must first resize the image before adding it to the LEADTOOLS Plus OCR engine. The Professional and Advantage engines do not have a restriction on the image size.

Example
Copy CodeCopy Code  
Public Sub AddPageFromDibExample()
      Dim tifFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif")
      Dim bmpFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.bmp")
      Dim pdfFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf")
      ' Get a DIB
      ' In this example, we will save the TIF file as BMP, then load it without the BITMAPFILEHEADER structure
      ' The result is a valid Windows DIB

      Using codecs As New RasterCodecs()
         codecs.Convert(tifFileName, bmpFileName, RasterImageFormat.Bmp, 0, 0, 1, Nothing)
      End Using

      ' From MSDN
      Dim bitmapFileHeaderStructSize As Integer = 14
      Dim dib As IntPtr = IntPtr.Zero

      Using fs As FileStream = File.OpenRead(bmpFileName)
         ' Allocate the DIB
         Dim dibSize As Integer = CType(fs.Length - bitmapFileHeaderStructSize, Integer)
         dib = Marshal.AllocHGlobal(dibSize)
         Dim dibOffset As Integer = 0

         ' Now read it
         fs.Seek(bitmapFileHeaderStructSize, SeekOrigin.Begin)

         Dim bufferSize As Integer = 1024 * 4
         Dim buffer(bufferSize - 1) As Byte
         Dim bytes As Integer = 0
         Do
            bytes = fs.Read(buffer, 0, bufferSize)
            If (bytes > 0) Then
               Dim dibPtr As New IntPtr(dib.ToInt64() + dibOffset)
               Marshal.Copy(buffer, 0, dibPtr, bytes)
               dibOffset += bytes
            End If

         Loop While (bytes > 0)
      End Using

      ' Create an instance of the engine
      Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, False)
         ' Start the engine using default parameters
         ocrEngine.Startup(Nothing, Nothing, Nothing, Nothing)

         ' Create an OCR document
         Using ocrDocument As IOcrDocument = ocrEngine.DocumentManager.CreateDocument()
            ' Add the image
            ocrDocument.Pages.AddDib(dib, Nothing)

            ' Free the DIB since we do not need it anymore
            Marshal.FreeHGlobal(dib)

            ' Recognize
            ocrDocument.Pages.Recognize(Nothing)

            ' Save
            ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, Nothing)
         End Using

         ' Shutdown the engine
         ' Note: calling Dispose will also automatically shutdown the engine if it has been started
         ocrEngine.Shutdown()
      End Using
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void AddPageFromDibExample()
   {
      string tifFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif");
      string bmpFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.bmp");
      string pdfFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.pdf");
      // Get a DIB
      // In this example, we will save the TIF file as BMP, then load it without the BITMAPFILEHEADER structure
      // The result is a valid Windows DIB

      using(RasterCodecs codecs = new RasterCodecs())
         codecs.Convert(tifFileName, bmpFileName, RasterImageFormat.Bmp, 0, 0, 1, null);


      // From MSDN
      const int bitmapFileHeaderStructSize = 14;
      IntPtr dib = IntPtr.Zero;

      using(FileStream fs = File.OpenRead(bmpFileName))
      {
         // Allocate the DIB
         int dibSize = (int)fs.Length - bitmapFileHeaderStructSize;
         dib = Marshal.AllocHGlobal(dibSize);
         int dibOffset = 0;

         // Now read it
         fs.Seek(bitmapFileHeaderStructSize, SeekOrigin.Begin);

         const int bufferSize = 1024 * 4;
         byte[] buffer = new byte[bufferSize];
         int bytes = 0;
         do
         {
            bytes = fs.Read(buffer, 0, bufferSize);
            if(bytes > 0)
            {
               IntPtr dibPtr = new IntPtr(dib.ToInt64() + dibOffset);
               Marshal.Copy(buffer, 0, dibPtr, bytes);
               dibOffset += bytes;
            }
         }
         while(bytes > 0);
      }

      // Create an instance of the engine
      using(IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Plus, false))
      {
         // Start the engine using default parameters
         ocrEngine.Startup(null, null, null, null);

         // Create an OCR document
         using(IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument())
         {
            // Add the image
            ocrDocument.Pages.AddDib(dib, null);

            // Free the DIB since we do not need it anymore
            Marshal.FreeHGlobal(dib);

            // Recognize
            ocrDocument.Pages.Recognize(null);

            // Save
            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();
      }
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
[TestMethod]
public async Task AddPageFromDibExample()
{
   string tifFileName = @"Assets\Ocr1.tif";
   string bmpFileName = "Ocr1.bmp";
   string pdfFileName = "Ocr1.pdf";
   // Get a DIB
   // In this example, we will save the TIF file as BMP, then load it without the BITMAPFILEHEADER structure
   // The result is a valid Windows DIB
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(tifFileName);
   StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(bmpFileName, CreationCollisionOption.ReplaceExisting);

   RasterCodecs codecs = new RasterCodecs();
   await codecs.ConvertAsync(LeadStreamFactory.Create(loadFile), LeadStreamFactory.Create(saveFile), RasterImageFormat.Bmp, 0, 0, 1, null);

   Windows.Storage.Streams.IBuffer dib;
   loadFile = await Tools.AppLocalFolder.GetFileAsync(bmpFileName);
   using (RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)))
   {
      dib = image.ToDib(RasterConvertToDibType.BitmapInfoHeader);
   }

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

   // Start the engine using default parameters
   ocrEngine.Startup(null, null, String.Empty, Tools.OcrEnginePath);

   // Create an OCR document
   IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument();

   // Add the image
   ocrDocument.Pages.AddDib(dib, null);

   // Recognize
   ocrDocument.Pages.Recognize(null);

   // Save
   saveFile = await Tools.AppLocalFolder.CreateFileAsync(pdfFileName, CreationCollisionOption.ReplaceExisting);
   await ocrDocument.SaveAsync(LeadStreamFactory.Create(saveFile), DocumentFormat.Pdf, null);

   // Shutdown the engine
   ocrEngine.Shutdown();
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

IOcrPageCollection Interface
IOcrPageCollection Members
Overload List
OcrEngineManager Class
OcrEngineType Enumeration
IOcrPage Interface
Programming with the LEADTOOLS .NET OCR

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.

AddDib(IntPtr,OcrProgressCallback) requires an OCR module license and unlock key. For more information, refer to: Imaging Pro/Document/Medical Features