Example #1
0
int main(int argc, char* argv[]) {
	/* path will contain path to image being recognized */
	const char* path = argv[1];
	/* this buffer will contain OCR model */
	char* ocrModel;
	/* this variable will contain OCR model buffer length in bytes */
	int ocrModelLength;
	/* this variable will contain all recognition settings (which recognizers are enabled, etc.) */
	RecognizerSettings* settings;
	/* this variable will contain MRTD recognition specific settings */
	MRTDSettings mrtdSettings;
	/* this variable will contain device information. On Mac/PC this is not usually necessary, but
	can information about available processor cores. If more than 1 processor is available, recognizers
	will try to use parallel algorithms as much as possible. */
	RecognizerDeviceInfo* deviceInfo;
	/* this variable is the global recognizer that internally contains a list of different recognizers.
	Each recognizer is an object that can perform object recognitions. For example, there are PDF417 barcode
	recognizer (Microblink's implementation for PDF417 barcodes), ZXing barcode recognizer (supports everything ZXing supports),
	Microblink's 1D barcode recognizer, etc. */
	Recognizer* recognizer;
	/* all API functions return RecognizerErrorStatus indicating the success or failure of operations */
	RecognizerErrorStatus status;
	/* recoginzer callback structure contains pointers to functions that will be called during the recognition process */
	RecognizerCallback recognizerCallback;
	/* this variable will contain list of scan results obtained from image scanning process. */
	RecognizerResultList* resultList;
	/* this variable will contain number of scan results obtained from image scanning process. */
	size_t numResults;
	/* this variable holds the image sent for image scanning process*/
	RecognizerImage* image;

	if (argc < 2) {
		printf("usage %s <img_path>\n", argv[0]);
		return -1;
	}

	/* load OCR model from file */
	status = recognizerLoadFileToBuffer("ocr_model.zzip", &ocrModel, &ocrModelLength);
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Could not load file ocr_model.zzip\n");
		return -1;
	}

	/* create recognizer settings object. Do not forget to delete it after usage. */
	recognizerSettingsCreate(&settings);

	/* create device info object. Do not forget to delete it after usage. */
	recognizerDeviceInfoCreate(&deviceInfo);
	/* define that device has 4 processors (you can use any number here - this is used to define number
	of threads library will use for its parallel operations */
	recognizerDeviceInfoSetNumberOfProcessors(deviceInfo, 4);
	/* add device info object to recognizer settings object */
	recognizerSettingsSetDeviceInfo(settings, deviceInfo);
	/* set OCR model to recognizer settings object */
	recognizerSettingsSetZicerModel(settings, ocrModel, ocrModelLength);
	
	/* add Machine Readable Travel Document recognizer settings to global recognizer settings object */
	recognizerSettingsSetMRTDSettings(settings, &mrtdSettings);

	/* insert license key and licensee */	
	recognizerSettingsSetLicenseKeyForLicensee(settings, "Add licensee here", "Add license key here");
	/* OR insert license key for licensee obtained with LicenseRequestTool (**DO NOT USE BOTH**) */
	recognizerSettingsSetLicenseKey(settings, "Add license key here");

	/* create global recognizer with settings */
	status = recognizerCreate(&recognizer, settings);
	/* as said earlier, all API functions return RecognizerErrorStatus. You can check the status for error, or you can simply ignore it like earlier in this example. */
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Error creating recognizer: %s\n", recognizerErrorToString(status));
		return -1;
	}

	/* build recognizer callback structure */
	recognizerCallback = buildRecognizerCallback();

	/* create the recognizer image object from video capture frame so we can send it to recognizer */
	status = recognizerImageCreateFromFile(&image, path);
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Error creating image from frame: %s\n", recognizerErrorToString(status));
		return -1;
	}

	/* if you do not want to receive callbacks during simply set NULL as last parameter. If you only want to receive some callbacks,
	insert non-NULL function pointers only to those events you are interested in */
	status = recognizerRecognizeFromImage(recognizer, &resultList, image, 0, NULL);
	if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
		printf("Error recognizing file %s: %s\n", path, recognizerErrorToString(status));
		return -1;
	}

	recognizerResultListGetNumOfResults(resultList, &numResults);

	if (numResults != 1u) {
		/* number of results should be 1 as there is only one recognizer configured */
		printf("Wrong number of recognizer results:" JL_SIZE_T_SPECIFIER "\n", numResults);
		return -1;
	}

	RecognizerResult* result;
	/* obtain the first (and only) result from list */
	recognizerResultListGetResultAtIndex(resultList, 0u, &result);


	int isMrtd = 0;
	/* check if it is a MRTD result */
	status = recognizerResultIsMRTDResult(result, &isMrtd);
	if (status == RECOGNIZER_ERROR_STATUS_SUCCESS && isMrtd) {
		int valid = 0;
		/* check if MRTD result is valid */
		status = recognizerResultIsResultValid(result, &valid);
		if (status == RECOGNIZER_ERROR_STATUS_SUCCESS && valid) {
			const char* doe;
			const char* issuer;
			const char* docNum;
			const char* docCode;
			const char* dob;
			const char* primID;
			const char* secID;
			const char* sex;
			const char* nat;
			const char* opt1;
			const char* opt2;
			/* obtain all fields from result */
			recognizerResultGetMRTDDateOfExpiry(result, &doe);
			recognizerResultGetMRTDIssuer(result, &issuer);
			recognizerResultGetMRTDDocumentNumber(result, &docNum);
			recognizerResultGetMRTDDocumentCode(result, &docCode);
			recognizerResultGetMRTDDateOfBirth(result, &dob);
			recognizerResultGetMRTDPrimaryID(result, &primID);
			recognizerResultGetMRTDSecondaryID(result, &secID);
			recognizerResultGetMRTDSex(result, &sex);
			recognizerResultGetMRTDNationality(result, &nat);
			recognizerResultGetMRTDOpt1(result, &opt1);
			recognizerResultGetMRTDOpt2(result, &opt2);
			/* display obtained fields */
			printf("ID is of type %s issued by %s.\nExpiration date is %s.\n", docCode, issuer, doe);
			printf("ID number is %s.\n", docNum);
			printf("ID holder is %s %s.\nDate of birth is %s.\nSex is %s.\n", primID, secID, dob, sex);
			printf("Nationality is %s.\n", nat);
			printf("Optional fields are:\nOPT1: %s\nOPT2: %s\n", opt1, opt2);

			const char* raw;
			/* obtain raw lines from result */
			recognizerResultGetMRTDRawStringData(result, &raw);
			printf("Raw result lines:\n%s\n", raw);
		}
		else {
			printf("Invalid result!\n");
		}
	}
	else {
		/* this should never happen as there is only MRTD recognizer configured */
		printf("Invalid result type!\n");
	}

	/* cleanup memory */	
	recognizerImageDelete(&image);
	recognizerResultListDelete(&resultList);
	recognizerDeviceInfoDelete(&deviceInfo);
	recognizerSettingsDelete(&settings);
	recognizerDelete(&recognizer);
	recognizerFreeFileBuffer(&ocrModel);

	return 0;
}
RecognizerErrorStatus recognizerWrapperInit(RecognizerWrapper* wrapper, const char* ocrModelPath) {

    /* all API functions return RecognizerErrorStatus indicating the success or failure of operations */
    RecognizerErrorStatus status;

    /* create recognizer settings object. Do not forget to delete it after usage. */
    status = recognizerSettingsCreate(&wrapper->settings);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot create recognizer settings: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* create device info object. Do not forget to delete it after usage. */
    status = recognizerDeviceInfoCreate(&wrapper->deviceInfo);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot create recognizer device info: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* define that device has 4 processors (you can use any number here - this is used to define number
     of threads library will use for its parallel operations */
    status = recognizerDeviceInfoSetNumberOfProcessors(wrapper->deviceInfo, 4);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot set number of processors: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* add device info object to recognizer settings object */
    status = recognizerSettingsSetDeviceInfo(wrapper->settings, wrapper->deviceInfo);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot set device info: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* Load OCR engine model */
    status = recognizerLoadFileToBuffer(ocrModelPath, &wrapper->ocrModel, &wrapper->ocrModelSize);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot load ocr model file at location %s: %s\n", ocrModelPath, recognizerErrorToString(status));
        return status;
    }

    /** Set the OCR engine model. Without OCR model, text cannot be processed */
    status = recognizerSettingsSetZicerModel(wrapper->settings, wrapper->ocrModel, wrapper->ocrModelSize);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot set ocr model: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* insert license key and licensee */
    /* this specific key is valid until 2015-07-15 */
    status = recognizerSettingsSetLicenseKey(wrapper->settings,
                                             "MicroBlink",
                                             "E4CKM73N-MIAARXO6-T3EIOCPN-2M2A7OZ3-7UVWJQEZ-WAEN3XU6-ZGPMTHWJ-T3EVYEML");
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot set license key: %s\n", recognizerErrorToString(status));
        return status;
    }

    /********* INITIALIZE RECOGNIZER SETTINGS ***********/
    /* This determines what will be scanned on images ***/

    /* create settings for MRTD recognizer */
    MRTDSettings mrtdSettings;

    /* add MRTD recognizer settings to global recognizer settings object */
    status = recognizerSettingsSetMRTDSettings(wrapper->settings, &mrtdSettings);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot set MRTD recognizer settings: %s\n", recognizerErrorToString(status));
        return status;
    }

    /********* END INITIALIZE RECOGNIZER SETTINGS *******/

    /* create global recognizer with settings */
    status = recognizerCreate(&wrapper->recognizer, wrapper->settings);
    if (status != RECOGNIZER_ERROR_STATUS_SUCCESS) {
        printf("Cannot create recognizer: %s\n", recognizerErrorToString(status));
        return status;
    }

    /* build recognizer callback structure */
    wrapper->recognizerCallback = buildRecognizerCallback();

    /* return success */
    return RECOGNIZER_ERROR_STATUS_SUCCESS;
}