Header Header

Examples
[Device handling]

Table of contents:

pr_calib - Calibration example

Show how to calibrate the device.

The needed gxsdldr.cpp loader file is included directly:

#include "prapi.h"
#include "gxsdldr.cpp"

The __lib__.h file contains some library functions used only in the sample programs, such as the FunctionStart and FunctionEnd functions that implement the simple command line interface and measures the elapsed time between the two function calls.

#include "../__lib__.h"
Lib lib;

The main function:

#ifdef GX_NAMESPACES
    using namespace gx;
    using namespace pr;
#endif

int main() {
    try {

Opening the Passport Reader system:

Note:
When the pr variable of the PasspportReader class is instantiated, the Passport Reader system is opened automaticaly:
        /* Opening the PR system */
        lib.FunctionStart("Opening system files");
        PassportReader pr;  /* Object for the PR system */
        lib.FunctionEnd();

Checking if the Passport Reader modules were opened:

        /* Validity check */
        if (!pr.IsValid()) {
            lib.Error("Failed to initialize!");
            return 0;
        }

Connecting to the first device. If no device is connected then the UseDevice() function throws an exception:

        /* Connecting to the first device */
        lib.FunctionStart("Connecting to the first device");
        pr.UseDevice(0,PR_UMODE_FULL_CONTROL);
        lib.FunctionEnd();

Displaying the calibration state of the device:

        lib.WriteLine("The device is " + string(pr.IsCalibrated(0)?"":"not ") + "calibrated.");

The calibration loop. Putting the calibration image on the device and calling the Calibrate() function to start the calibration process. In case of error the exception is catched and the error code and error message is displayed by the DisplExcp library function:

        lib.WriteLine("Put the calibration image on the document window.");

        string s = "";
        while (s != "no")   {
            s = lib.ReadLine("Do you want to (re)calibrate? (yes/no) ");

            if (s == "no") break;
            if (s != "yes") {
                lib.WriteLine(" !!! Type 'yes' or 'no' please!");
                continue;
            }

            try {
                lib.FunctionStart("Calibrating device");
                pr.Calibrate(0);
                lib.FunctionEnd();
            } catch (gxException e) {
                lib.DisplExcp(e);
            }
        }

Displaying the state of the device after the calibration process:

        /* Overall result */
        lib.WriteLine("The device is " + string(pr.IsCalibrated(0)?"":"not ") + "calibrated.");

Closing the device:

        /* Closing the device */
        lib.FunctionStart("Closing the device");
        pr.CloseDevice();
        lib.FunctionEnd();

Exception handling section, catching the exception and displaying the error code and error message:

    } catch (gxException e) {
        lib.DisplExcp(e);
        return 0;
    }

The sample program ends:

    return 1;
}

The complete source code:

#include "prapi.h"
#include "gxsdldr.cpp"

#include "../__lib__.h"
Lib lib;

#ifdef GX_NAMESPACES
    using namespace gx;
    using namespace pr;
#endif

int main() {
    try {
        /* Opening the PR system */
        lib.FunctionStart("Opening system files");
        PassportReader pr;  /* Object for the PR system */
        lib.FunctionEnd();

        /* Validity check */
        if (!pr.IsValid()) {
            lib.Error("Failed to initialize!");
            return 0;
        }

        /* Connecting to the first device */
        lib.FunctionStart("Connecting to the first device");
        pr.UseDevice(0,PR_UMODE_FULL_CONTROL);
        lib.FunctionEnd();

        lib.WriteLine("The device is " + string(pr.IsCalibrated(0)?"":"not ") + "calibrated.");
        lib.WriteLine("Put the calibration image on the document window.");

        string s = "";
        while (s != "no")   {
            s = lib.ReadLine("Do you want to (re)calibrate? (yes/no) ");

            if (s == "no") break;
            if (s != "yes") {
                lib.WriteLine(" !!! Type 'yes' or 'no' please!");
                continue;
            }

            try {
                lib.FunctionStart("Calibrating device");
                pr.Calibrate(0);
                lib.FunctionEnd();
            } catch (gxException e) {
                lib.DisplExcp(e);
            }
        }

        /* Overall result */
        lib.WriteLine("The device is " + string(pr.IsCalibrated(0)?"":"not ") + "calibrated.");

        /* Closing the device */
        lib.FunctionStart("Closing the device");
        pr.CloseDevice();
        lib.FunctionEnd();

    } catch (gxException e) {
        lib.DisplExcp(e);
        return 0;
    }

    return 1;
}

pr_motdet - Device controlling example

Show how to control the device.

The needed gxsdldr.cpp loader file is included directly:

#include "prapi.h"
#include "gxsdldr.cpp"

The __lib__.h file contains some library functions used only in the sample programs, such as the FunctionStart and FunctionEnd functions that implement the simple command line interface and measures the elapsed time between the two function calls.

#include "../__lib__.h"
Lib lib;

The main function:

#ifdef GX_NAMESPACES
    using namespace gx;
    using namespace pr;
#endif

int main() {
    vector<string> statxt(6);
    statxt[0] = "No document detected.";
    statxt[1] = "Document is moving.";
    statxt[2] = "Take out the document!";
    statxt[3] = "";
    statxt[4] = "Ready for reading.";
    statxt[5] = "Button is pressed.";

    try {

Opening the Passport Reader system:

Note:
When the pr variable of the PasspportReader class is instantiated, the Passport Reader system is opened automaticaly:
        /* Opening the PR system */
        lib.FunctionStart("Opening system files");
        PassportReader pr;  /* Object for the PR system */
        lib.FunctionEnd();

Checking if the Passport Reader modules were opened:

        /* Validity check */
        if (!pr.IsValid()) {
            lib.Error("Failed to initialize!");
            return 0;
        }

Connecting to the first device. If no device is connected then the UseDevice() function throws an exception:

        /* Connecting to the first device */
        lib.FunctionStart("Connecting to the first device");
        pr.UseDevice(0,PR_UMODE_FULL_CONTROL);
        lib.FunctionEnd();

Testing the power state of devices with power switch:

        /* Testing power switch */
        try {
            lib.FunctionStart("Testing power switch");
            int state = pr.TestPowerState();
            if ((state & PR_PWR_MASK) == PR_PWR_OFF)
                lib.WriteLine("Power is off! Please turn power on!");

            while ((state & PR_PWR_MASK) == PR_PWR_OFF) {
                try {
                    state = pr.TestPowerState();
                } catch (gxException e) {
                    lib.DisplExcp(e);
                }
                lib.Wait(300);
            }
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

Enabling document motion detection by setting the proper value for the freerun_mode property:

        /* Enabling motion detection */
        try {
            lib.FunctionStart("Enabling motion detection");
            pr.SetProperty("freerun_mode",PR_FRMODE_TESTDOCUMENT);
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

The main loop. In this short sample the handling of the start button and the document detection is presented. In normal operation when the start button is pressed or a document is detected on the document window, that is the document has PR_TD_IN status, the capture and recognize process can be started. The display leds of the device are used to indicate the status of the device and of the document.

        while (!lib.KbHit()) {
            try {
                /* Testing the start button */
                int state = pr.TestButton(PR_KEY_START);
                if (state == PR_KEY_PRESSED) state = 5;
                else {
                    /* If the start button is not pressed testing the document detection */
                    state = pr.TestDocument(0);

                    /* Turning the display leds depending on the status */
                    int color = PR_SLC_OFF;
                    switch(state) {
                        case PR_TD_OUT:     color = PR_SLC_GREEN; break;
                        case PR_TD_MOVE:    color = PR_SLC_ANY; break;
                        case PR_TD_NOMOVE:  color = PR_SLC_RED; break;
                    }
                    pr.SetStatusLed(0xFF, color);
                }
                lib.Write("State of the device: " + statxt[state] + "        \r");
                lib.Wait(200);
                if ((state == PR_TD_IN) || (state == 5)) lib.Wait(800);
            } catch (gxException e) {
                lib.DisplExcp(e);
            }
        }
        lib.WriteLine("");

Turning off the lights and stopping the document detection process:

        /* Turning off the lights */
        try {
            lib.FunctionStart("Turning off the lights");
            pr.SetProperty("freerun_mode", PR_FRMODE_NULL);
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

Turning off the display leds:

        /* Turning off the display leds */
        try {
            lib.FunctionStart("Turning off the display leds");
            pr.SetStatusLed(0xFF, PR_SLC_OFF);
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

Closing the device:

        /* Closing the device */
        lib.FunctionStart("Closing the device");
        pr.CloseDevice();
        lib.FunctionEnd();

Exception handling section, catching the exception and displaying the error code and error message:

    } catch (gxException e) {
        lib.DisplExcp(e);
        return 0;
    }

The sample program ends:

    return 1;
}

The complete source code:

#include "prapi.h"
#include "gxsdldr.cpp"

#include "../__lib__.h"
Lib lib;

#ifdef GX_NAMESPACES
    using namespace gx;
    using namespace pr;
#endif

int main() {
    vector<string> statxt(6);
    statxt[0] = "No document detected.";
    statxt[1] = "Document is moving.";
    statxt[2] = "Take out the document!";
    statxt[3] = "";
    statxt[4] = "Ready for reading.";
    statxt[5] = "Button is pressed.";

    try {
        /* Opening the PR system */
        lib.FunctionStart("Opening system files");
        PassportReader pr;  /* Object for the PR system */
        lib.FunctionEnd();

        /* Validity check */
        if (!pr.IsValid()) {
            lib.Error("Failed to initialize!");
            return 0;
        }

        /* Connecting to the first device */
        lib.FunctionStart("Connecting to the first device");
        pr.UseDevice(0,PR_UMODE_FULL_CONTROL);
        lib.FunctionEnd();

        /* Testing power switch */
        try {
            lib.FunctionStart("Testing power switch");
            int state = pr.TestPowerState();
            if ((state & PR_PWR_MASK) == PR_PWR_OFF)
                lib.WriteLine("Power is off! Please turn power on!");

            while ((state & PR_PWR_MASK) == PR_PWR_OFF) {
                try {
                    state = pr.TestPowerState();
                } catch (gxException e) {
                    lib.DisplExcp(e);
                }
                lib.Wait(300);
            }
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

        /* Enabling motion detection */
        try {
            lib.FunctionStart("Enabling motion detection");
            pr.SetProperty("freerun_mode",PR_FRMODE_TESTDOCUMENT);
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

        while (!lib.KbHit()) {
            try {
                /* Testing the start button */
                int state = pr.TestButton(PR_KEY_START);
                if (state == PR_KEY_PRESSED) state = 5;
                else {
                    /* If the start button is not pressed testing the document detection */
                    state = pr.TestDocument(0);

                    /* Turning the display leds depending on the status */
                    int color = PR_SLC_OFF;
                    switch(state) {
                        case PR_TD_OUT:     color = PR_SLC_GREEN; break;
                        case PR_TD_MOVE:    color = PR_SLC_ANY; break;
                        case PR_TD_NOMOVE:  color = PR_SLC_RED; break;
                    }
                    pr.SetStatusLed(0xFF, color);
                }
                lib.Write("State of the device: " + statxt[state] + "        \r");
                lib.Wait(200);
                if ((state == PR_TD_IN) || (state == 5)) lib.Wait(800);
            } catch (gxException e) {
                lib.DisplExcp(e);
            }
        }
        lib.WriteLine("");

        /* Turning off the lights */
        try {
            lib.FunctionStart("Turning off the lights");
            pr.SetProperty("freerun_mode", PR_FRMODE_NULL);
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

        /* Turning off the display leds */
        try {
            lib.FunctionStart("Turning off the display leds");
            pr.SetStatusLed(0xFF, PR_SLC_OFF);
            lib.FunctionEnd();
        } catch (gxException e) {
            lib.DisplExcp(e);
        }

        /* Closing the device */
        lib.FunctionStart("Closing the device");
        pr.CloseDevice();
        lib.FunctionEnd();

    } catch (gxException e) {
        lib.DisplExcp(e);
        return 0;
    }

    return 1;
}


Generated  for Passport Reader
(c) ARH Inc.