GdPicture.NET.14
GdPicture14 Namespace / GdPictureOCR Class / GetCharacterCount Method
The unique result identifier of the executed OCR process obtained by the RunOCR method.
Example





In This Topic
GetCharacterCount Method (GdPictureOCR)
In This Topic
Returns the number of characters within a specified OCR result.
Syntax
'Declaration
 
Public Function GetCharacterCount( _
   ByVal OCRResultID As String _
) As Integer
public int GetCharacterCount( 
   string OCRResultID
)
public function GetCharacterCount( 
    OCRResultID: String
): Integer; 
public function GetCharacterCount( 
   OCRResultID : String
) : int;
public: int GetCharacterCount( 
   string* OCRResultID
) 
public:
int GetCharacterCount( 
   String^ OCRResultID
) 

Parameters

OCRResultID
The unique result identifier of the executed OCR process obtained by the RunOCR method.

Return Value

The number of recognized characters. Please always use the GetStat method to determine if this method has been successful.
Remarks
It is recommend to use the GetStat method to identify the specific reason for the method's failure, if any.
Example
How to find out the number of recognized characters within the OCR result and some of the character's properties.
Dim caption As String = "Example: GetCharacterCount"
Dim gdpictureOCR As GdPictureOCR = New GdPictureOCR
Dim gdpicturePDF As GdPicturePDF = New GdPicturePDF
'Load the PDF document.
If gdpicturePDF.LoadFromFile("input.pdf", False) = GdPictureStatus.OK Then
    'Select the first page.
    gdpicturePDF.SelectPage(1)
    'Render this page to a 200 DPI image.
    Dim image As Integer = gdpicturePDF.RenderPageToGdPictureImage(200, True)
    If gdpicturePDF.GetStat = GdPictureStatus.OK AndAlso
       gdpictureOCR.SetImage(image) = GdPictureStatus.OK Then
        'Setting up the image is mandatory.
        'Set the New origin for better coordinates handling.
        gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
        gdpicturePDF.SetLineColor(Color.Orchid)
        gdpicturePDF.SetLineWidth(2)
        'Set up the OCR parameters.
        gdpictureOCR.ResourcesFolder = "C:\Path\To\GdPicture.NET 14\Redist\OCR"
        gdpictureOCR.AddLanguage(OCRLanguage.English)
        gdpictureOCR.OCRMode = OCRMode.FavorAccuracy
        'Set up the OCR context and the character list.
        gdpictureOCR.Context = OCRContext.OCRContextSingleLine
        gdpictureOCR.CharacterSet = "0123456789"
        'Set up the area to be processed by the OCR.
        gdpictureOCR.SetROI(100, 100, 200, 50)
        'Run the OCR process to recognize the phone number.
        Dim resID As String = gdpictureOCR.RunOCR()
        If gdpictureOCR.GetStat = GdPictureStatus.OK Then
            Dim charCount As Integer = gdpictureOCR.GetCharacterCount(resID)
            If gdpictureOCR.GetStat() = GdPictureStatus.OK Then
                Dim message As String = "The number of recognized characters: " + charCount.ToString()
                Dim phonenumber As String = ""
                Dim left As Integer = 0, right As Integer = 0, top As Integer = 0, bottom As Integer = 0
                For i As Integer = 0 To charCount - 1
                    phonenumber = phonenumber + gdpictureOCR.GetCharacterValue(resID, i)
                    If i = 0 Then
                        left = gdpictureOCR.GetCharacterLeft(resID, i)
                        top = gdpictureOCR.GetCharacterTop(resID, i)
                    End If
                    If i = charCount - 1 Then
                        right = gdpictureOCR.GetCharacterRight(resID, i)
                        bottom = gdpictureOCR.GetCharacterBottom(resID, i)
                    End If
                Next
                message = message + vbCrLf + "The phone number is: " + phonenumber
                If gdpicturePDF.DrawRectangle(left, top, right - left, bottom - top, False, True) = GdPictureStatus.OK Then
                    message = message + "   Drawn: yes" + vbCrLf
                Else
                    message = message + "   Drawn: no" + vbCrLf
                End If
                MessageBox.Show(message, caption)
                'Continue with analyzing the result ...
                'Save the page with drawn recognized phone number to the new PDF document.
                If gdpicturePDF.SaveToFile("output.pdf") = GdPictureStatus.OK Then
                    MessageBox.Show("Done!", caption)
                Else
                    MessageBox.Show("The file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption)
                End If
            Else
                MessageBox.Show("The GetCharacterCount() method has failed with the status: " + gdpictureOCR.GetStat().ToString(), caption)
            End If
        Else
            MessageBox.Show("The error occurred when running the OCR. Status: " + gdpictureOCR.GetStat().ToString(), caption)
        End If
        'Release the image.
        GdPictureDocumentUtilities.DisposeImage(image)
    Else
        MessageBox.Show("The error occurred when creating or setting up the image. Status: " + gdpicturePDF.GetStat().ToString() + "/" + gdpictureOCR.GetStat().ToString(), caption)
    End If
    'Close the document.
    gdpicturePDF.CloseDocument()
Else
    MessageBox.Show("The file can't be loaded. Status: " + gdpicturePDF.GetStat().ToString(), caption)
End If
'Release resources.
gdpictureOCR.ReleaseOCRResults()
gdpictureOCR.Dispose()
gdpicturePDF.Dispose()
string caption = "Example: GetCharacterCount";
GdPictureOCR gdpictureOCR = new GdPictureOCR();
GdPicturePDF gdpicturePDF = new GdPicturePDF();
//Load the PDF document.
if (gdpicturePDF.LoadFromFile("input.pdf", false) == GdPictureStatus.OK)
{
    //Select the first page.
    gdpicturePDF.SelectPage(1);
    //Render this page to a 200 DPI image.
    int image = gdpicturePDF.RenderPageToGdPictureImage(200, true);
    if ((gdpicturePDF.GetStat() == GdPictureStatus.OK) &&
        (gdpictureOCR.SetImage(image) == GdPictureStatus.OK)) //Setting up the image is mandatory.
    {
        //Set the new origin for better coordinates handling.
        gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
        gdpicturePDF.SetLineColor(Color.Orchid);
        gdpicturePDF.SetLineWidth(2);
        //Set up the OCR parameters.
        gdpictureOCR.ResourcesFolder = "C:\\Path\\To\\GdPicture.NET 14\\Redist\\OCR";
        gdpictureOCR.AddLanguage(OCRLanguage.English);
        gdpictureOCR.OCRMode = OCRMode.FavorAccuracy;
        //Set up the OCR context and the character list.
        gdpictureOCR.Context = OCRContext.OCRContextSingleLine;
        gdpictureOCR.CharacterSet = "0123456789";
        //Set up the area to be processed by the OCR.
        gdpictureOCR.SetROI(100, 100, 200, 50);
        //Run the OCR process to recognize the phone number.
        string resID = gdpictureOCR.RunOCR();
        if (gdpictureOCR.GetStat() == GdPictureStatus.OK)
        {
            int charCount = gdpictureOCR.GetCharacterCount(resID);
            if (gdpictureOCR.GetStat() == GdPictureStatus.OK)
            {
                string message = "The number of recognized characters: " + charCount.ToString();
                string phonenumber = "";
                int left = 0, right = 0, top = 0, bottom = 0;
                for (int i = 0; i < charCount; i++)
                {
                    phonenumber = phonenumber + gdpictureOCR.GetCharacterValue(resID, i);
                    if (i == 0)
                    {
                        left = gdpictureOCR.GetCharacterLeft(resID, i);
                        top = gdpictureOCR.GetCharacterTop(resID, i);
                    }
                    if (i == charCount - 1)
                    {
                        right = gdpictureOCR.GetCharacterRight(resID, i);
                        bottom = gdpictureOCR.GetCharacterBottom(resID, i);
                    }
                }
                message = message + "\nThe phone number is: " + phonenumber;
                if (gdpicturePDF.DrawRectangle(left, top, right - left, bottom - top, false, true) == GdPictureStatus.OK)
                    message = message + "   Drawn: yes\n";
                else
                    message = message + "   Drawn: no\n";
                MessageBox.Show(message, caption);
                //Continue with analyzing the result ...
                //Save the page with drawn recognized phone number to the new PDF document.
                if (gdpicturePDF.SaveToFile("output.pdf") == GdPictureStatus.OK)
                    MessageBox.Show("Done!", caption);
                else
                    MessageBox.Show("The file can't be saved. Status: " + gdpicturePDF.GetStat().ToString(), caption);
            }
            else
                MessageBox.Show("The GetCharacterCount() method has failed with the status: " + gdpictureOCR.GetStat().ToString(), caption);
        }
        else
            MessageBox.Show("The error occurred when running the OCR. Status: " + gdpictureOCR.GetStat().ToString(), caption);
        //Release the image.
        GdPictureDocumentUtilities.DisposeImage(image);
    }
    else
        MessageBox.Show("The error occurred when creating or setting up the image. Status: " + gdpicturePDF.GetStat().ToString() + "/" + gdpictureOCR.GetStat().ToString(), caption);
    //Close the document.
    gdpicturePDF.CloseDocument();
}
else
    MessageBox.Show("The file can't be loaded. Status: " + gdpicturePDF.GetStat().ToString(), caption);
//Release resources.
gdpictureOCR.ReleaseOCRResults();
gdpictureOCR.Dispose();
gdpicturePDF.Dispose();
See Also