image2data

Der Tech-Blog

...weitermachen, wo OCR aufhört

Texterkennung über alle Seiten (sample_3.i2dspt)

Dez 012014
// ***************************************************************
// * sample_3.i2dspt                                             *
// *                                                             *
// * - Retrieving the text from all pages of an image using OCR  *
// * - Saving the result in a textfile named "sample_result.txt" *
// *   located in the root path of the currrent drive            *
// *                                                             *
// * Press F9 to execute the code or F7/F8 to debug it           *
// *                                                             *
// * Be sure that the sample data was installed!                 *
// *                                                             *
// * Contact www.norpa.eu for more information                   *
// ***************************************************************
 
var
  // Declare some variables
  i: Integer;
  iPageCount: Integer;
 
  sText: String;
 
  oPage: TBitmap;
  oText: TStringList;
 
  rProcessSettings: Ti2dProcessSettings;
 
begin
  // Get the configured process settings. We must know the configured "in"-folder because that's
  // where the sample docs are located in!
  i2dGetProcessSettings(rProcessSettings);
 
  // Create the bitmap object
  oPage := TBitmap.Create;
 
  // Create the stringlist object
  oText := TStringList.Create;
 
  try
    // Get the number of pages from the image file
    iPageCount := i2dGetImagePageCount(rProcessSettings.InPath + 'sample_invoice_5.tif');
 
    for i := 1 to iPageCount do begin
      // Load page number i of the image into the bitmap object
      i2dLoadBitmap(rProcessSettings.InPath + 'sample_invoice_5.tif', i, oPage);
 
      // Perform a character recognition (ocr) with format preservation on the bitmap object
      sText := sText + i2dOCRBitmap(oPage, 0, True);
 
      // Insert a pageend tag for optical reasons
      sText := sText + #13#10'<page end>'#13#10;
    end;
 
    // Pass result to the stringlist object
    oText.Text := sText;
 
    // Save stringlist to file
    oText.SaveToFile('\sample_result.txt');
  finally
    // Destroy the bitmap object and release memory
    oPage.Free;
 
    // Destroy the stringlist object and release memory
    oText.Free;
  end;
end.

Atom

powered by Nibbleblog