Skip to content

Direct processing API

dino.gustin edited this page Jun 14, 2016 · 5 revisions

This guide will in short present you how to process UIImage objects with BlinkID SDK, without starting the camera video capture.

With this feature you can solve various use cases like:

  • recognizing text on images in Camera roll
  • taking full resolution photo and sending it to processing
  • scanning barcodes on images in e-mail
  • etc.

This guide will be essentially the same as in the "Getting Started" guide. It also closely follows DirectAPI-sample app in the SDK repository.

DirectAPI-sample demo app here will present UIImagePickerController for taking full resolution photos, and then process it with MicroBlink SDK to get scanning results using Direct processing API.

1. Initial integration steps

The same as in "Getting Started" guide.

2. Referencing header file

The same as in "Getting Started" guide.

3. Initializing the scanning library

To initiate the scanning process, first decide where in your app you want to add scanning functionality. Usually, users of the scanning library have a button which, when tapped, starts the scanning process. Initialization code is then placed in touch handler for that button. Here we're listing the initialization code as it looks in a touch handler method.

/**
 * Method allocates and initializes the coordinator object.
 * Coordinator is initialized with settings for recognition
 * Modify this method to include only those recognizer settings you need. This will give you optimal performance
 *
 *  @param error Error object, if scanning isn't supported
 *
 *  @return initialized coordinator
 */
- (PPCoordinator *)coordinatorWithError:(NSError**)error {

    /** 1. Initialize the Scanning settings */

    // Initialize the scanner settings object. This initialize settings with all default values.
    PPSettings *settings = [[PPSettings alloc] init];


    /** 2. Setup the license key */

    // Visit www.microblink.com to get the license key for your app
    settings.licenseSettings.licenseKey = @"DD2ZSHRI-3VUZC7YS-52AY4MQX-ROSNKR7U-LS67IXF5-6ROL35C4-XX2FYPL5-AZ5MS7IM";


    /**
     * 3. Set up what is being scanned. See detailed guides for specific use cases.
     * Remove undesired recognizers (added below) for optimal performance.
     */


    { // Remove this if you're not using MRTD recognition

        // To specify we want to perform MRTD (machine readable travel document) recognition, initialize the MRTD recognizer settings
        PPMrtdRecognizerSettings *mrtdRecognizerSettings = [[PPMrtdRecognizerSettings alloc] init];

        /** You can modify the properties of mrtdRecognizerSettings to suit your use-case */

        // Add MRTD Recognizer setting to a list of used recognizer settings
        [settings.scanSettings addRecognizerSettings:mrtdRecognizerSettings];
    }

    { // Remove this if you're not using USDL recognition

        // To specify we want to perform USDL (US Driver's license) recognition, initialize the USDL recognizer settings
        PPUsdlRecognizerSettings *usdlRecognizerSettings = [[PPUsdlRecognizerSettings alloc] init];

        /** You can modify the properties of usdlRecognizerSettings to suit your use-case */

        // Add USDL Recognizer setting to a list of used recognizer settings
        [settings.scanSettings addRecognizerSettings:usdlRecognizerSettings];
    }

    /** 4. Initialize the Scanning Coordinator object */

    // Create coordinator with delegate for recognition events
    PPCoordinator *coordinator = [[PPCoordinator alloc] initWithSettings:settings delegate:self];
    
    return coordinator;
}

- (IBAction)takePhoto:(id)sender {
    NSLog(@"Take photo!");

    /** Instantiate the scanning coordinator */
    NSError *error;
    self.coordinator = [self coordinatorWithError:&error];

    /** If scanning isn't supported, present an error */
    if (self.coordinator == nil) {
        NSString *messageString = [error localizedDescription];
        [[[UIAlertView alloc] initWithTitle:@"Warning"
                                    message:messageString
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil, nil] show];

        return;
    }

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];

    // Use rear camera
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
    cameraUI.cameraDevice = UIImagePickerControllerCameraDeviceRear;

    // Displays a control that allows the user to choose only photos
    cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *)kUTTypeImage, nil];

    // Hides the controls for moving & scaling pictures, or for trimming movies.
    cameraUI.allowsEditing = NO;

    // Shows default camera control overlay over camera preview.
    cameraUI.showsCameraControls = YES;

    // set delegate
    cameraUI.delegate = self;

    // Show view
    [self presentViewController:cameraUI animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

    // Handle a still image capture
    if (CFStringCompare((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
        UIImage *originalImage = (UIImage *)[info objectForKey: UIImagePickerControllerOriginalImage];

		//Wrap UIImage into PPImage. You can also inspect image (if necessary) and adjust orientation property.
        PPImage *image = [PPImage imageWithUIImage:originalImage];

        // Process the selected image
        [self.coordinator processImage:image];
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

4. Registering for scanning events

Similar to scanning events described in "Getting Started" guide.

The difference is that DirectAPI uses PPCoordinatorDelegate, while out scanner uses PPScanningDelegate.

Conclusion

Now you've seen how to implement the Direct processing API.

In essence, this API consists of two steps:

  1. Initialization of the scanner.

  2. Call of processImage: method for each UIImage or CMSampleBufferRef you have.

Clone this wiki locally