Click or drag to resize
TesseractOcrEngineDoOcrAsync Method (TesseractOcrJobRequest, Int32, Int32, IBuffer)
Executes an instance (job) of the OCR engine over the image raw specified by pixelWidth, pixelHeight and pixelBuffer, with the specified settings present in the TesseractOcrJobRequest in an Asynchronous way, preventing blocking of the main thread.

Namespace: DevScope.Ocr.Tesseract.WindowsPhone
Assembly: DevScope.Ocr.Tesseract.WindowsPhone (in DevScope.Ocr.Tesseract.WindowsPhone.dll) Version: 255.255.255.255
Syntax
public IAsyncOperationWithProgress<TesseractOcrJobResponse, TesseractOcrProgressEventArgs> DoOcrAsync(
	TesseractOcrJobRequest request,
	int pixelWidth,
	int pixelHeight,
	IBuffer pixelBuffer
)

Parameters

request
Type: DevScope.Ocr.Tesseract.WindowsPhoneTesseractOcrJobRequest
The job request.
pixelWidth
Type: SystemInt32
Width of image in pixels.
pixelHeight
Type: SystemInt32
Height of the image in pixels.
pixelBuffer
Type: IBuffer
The direct buffer of a WriteableBitmap.

Return Value

Type: IAsyncOperationWithProgressTesseractOcrJobResponse, TesseractOcrProgressEventArgs
The results of the OCR process.
Remarks
The DoOcrAsync method’s arguments are the image dimensions and the direct buffer of a WriteableBitmap.
Examples
This sample shows how to call the DoOcrAsync method.
void RunOcrEngineByWriteableBitmapBuffer()  
{ 
   ... 
   var ocrEngine = new TesseractOcrEngine(tessdataRootFolder.Path, languageCode);

   // Create the ocr job request
   var request = new TesseractOcrJobRequest
   {
       OrientationMode = TesseractOcrOrientationMode.None,
       JobName = "MyJob " + DateTime.Now.ToString(),
       PageSegmentationMode = TesseractOcrPageSegmentationMode.AutomaticPageSegmentationNoOCR
   };

   request.ImagePreProcessing.AutoDeskew = true;
   request.ImagePreProcessing.AutoDespeckle = true;
   request.ImagePreProcessing.UseLocalAdaptiveThresholding = true;

   // Create the WriteableBitmap from an absolute Path
   StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
   var imgPath = Path.Combine(tempFolder.Path, CommonSettings.FileNameFullResImageSentToOcr);
   var imgFile = await StorageFile.GetFileFromPathAsync(imgPath);
   ImageProperties props = await imgFile.Properties.GetImagePropertiesAsync();
   var wb = new WriteableBitmap((int)props.Height, (int)props.Width);
   wb.SetSource(await imgFile.OpenReadAsync());

   // Create the doOcr operation 
   var operationDoOcr = ocrEngine.DoOcrAsync(request, wb.PixelWidth, wb.PixelHeight, wb.PixelBuffer);

   // setup progress handler
   operationDoOcr.Progress = new AsyncOperationProgressHandler<TesseractOcrJobResponse, TesseractOcrProgressEventArgs>((operation, report) = >
   {
       if (_cancelCurrentOcrJob)
           report.Cancel = true;
       Dispatcher.BeginInvoke(() = >
       {
           pbar.Value = report.Percent;
       });
   });

   // now, run the ocr and get the results
   var jobResponse = await operationDoOcr;

   // handle the results accordingly
   switch (jobResponse.Status)
   {
        case TesseractOcrResultStatus.Error:
        {
            MessageBox.Show(_ocrJobResponse.ErrorMessage, "Ocr Error", MessageBoxButton.OK);
            break;
        }
        case TesseractOcrResultStatus.InvalidLicense:
        {
            MessageBox.Show("INVALID LICENSE. Please contact Devscope Support.", "Ocr Error", MessageBoxButton.OK);
            break;
        }
        case TesseractOcrResultStatus.Ok:
        {
            textOcr.Text = jobResponse.Text;
            break;
        }
   }
}
See Also